Skip to content

Commit

Permalink
push server.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Lampese committed Jul 28, 2023
1 parent 8ad0747 commit 16e4412
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,4 @@ package-lock.json
scripts/src/pureeval
test/node_modules
test/*
!test/server.js
!test/package.json
!test/index.html
!test/texture.png
!webviewer/server.js
78 changes: 78 additions & 0 deletions webviewer/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import * as ws from 'ws';
import readline from 'readline';
import { stdin, stdout } from 'process';
import express from 'express';
import path, { resolve } from 'path';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import {
Vec3,
vec3,
put,
view,
Exp,
Generator,
Transform,
Lineamp,
LSystem,
IFS,
DLA,
Turtle2D,
Turtle3D
} from '../index.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

function pack(bs) {
return {
op: 'put',
raw: bs.map((v) => [v.x, v.y, v.z])
};
}

function handleWebSocket() {
const wss = new ws.WebSocketServer({ port: 2333 });

wss.on('error', (e) => {
console.log(e);
});

wss.on('connection', (socket) => {
const rl = readline.createInterface({ input: stdin, output: stdout });
rl.on('line', handleLine(socket));
});

function handleLine(socket) {
return (s) => {
try {
if (s === 'clear') sendClear(socket);
else sendPack(socket, s);
} catch (e) {
console.log(e);
}
};
}

function sendClear(socket) {
socket.send(JSON.stringify({ op: 'clear' }));
}

function sendPack(socket, s) {
socket.send(JSON.stringify(pack(eval(s))));
}
}

function handleHttpServer() {
const app = express();

app.use(express.static(path.join(__dirname, 'public')));
app.use('/node_modules', express.static(path.join(resolve(__dirname, '../'), 'node_modules')));

app.listen(8080, () => {
console.log('Server is running. You can access it at http://localhost:8080.');
});
}

handleWebSocket();
handleHttpServer();

0 comments on commit 16e4412

Please sign in to comment.