Changes for live preview - process every coordinate
This commit is contained in:
parent
2f9fbe3ff9
commit
3e62154f0e
47
src/app.ts
47
src/app.ts
|
@ -1,10 +1,13 @@
|
||||||
import WebSocket from 'ws';
|
import WebSocket from 'ws';
|
||||||
|
import Coordinates from './coordinates';
|
||||||
|
|
||||||
const wss = new WebSocket.Server({ port: 8081 });
|
const wss = new WebSocket.Server({ port: 8081 });
|
||||||
const receivers: WebSocket[] = [];
|
const receivers: WebSocket[] = [];
|
||||||
let sender: WebSocket | null = null;
|
let sender: WebSocket | null = null;
|
||||||
|
|
||||||
const paths = [] as string[];
|
const paths = [] as Coordinates[][];
|
||||||
|
|
||||||
|
let currentPath = [] as Coordinates[];
|
||||||
|
|
||||||
wss.on('connection', ws => {
|
wss.on('connection', ws => {
|
||||||
console.log('CONNECTION incoming');
|
console.log('CONNECTION incoming');
|
||||||
|
@ -18,7 +21,7 @@ wss.on('connection', ws => {
|
||||||
addReceiver(ws)
|
addReceiver(ws)
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
paths.push(str);
|
console.log('Unknown register code: ' + str);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -29,8 +32,11 @@ console.log('READY');
|
||||||
function addReceiver(ws: WebSocket) {
|
function addReceiver(ws: WebSocket) {
|
||||||
console.log('RECEIVER registered');
|
console.log('RECEIVER registered');
|
||||||
for (const path of paths) {
|
for (const path of paths) {
|
||||||
ws.send(path);
|
ws.send('START');
|
||||||
|
path.forEach(c => ws.send(JSON.stringify(c)));
|
||||||
|
ws.send('STOP');
|
||||||
}
|
}
|
||||||
|
|
||||||
receivers.push(ws);
|
receivers.push(ws);
|
||||||
|
|
||||||
ws.onclose = () => {
|
ws.onclose = () => {
|
||||||
|
@ -56,16 +62,41 @@ function setSender(ws: WebSocket) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function processMessage(message: WebSocket.Data) {
|
function processMessage(message: WebSocket.Data) {
|
||||||
const path = message.toString();
|
const text = message.toString();
|
||||||
console.log('PATH RECEIVED: ' + path);
|
switch (text) {
|
||||||
paths.push(path);
|
case 'START':
|
||||||
|
startPath();
|
||||||
|
break;
|
||||||
|
case 'STOP':
|
||||||
|
finishPath();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
processCoordinates(text);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function processCoordinates(text: string) {
|
||||||
|
console.log('COORDINATES received: ' + text);
|
||||||
|
|
||||||
console.log('SENDING to ' + receivers.length, 'clients');
|
console.log('SENDING to ' + receivers.length, 'clients');
|
||||||
receivers.forEach(r => r.send(path));
|
receivers.forEach(r => r.send(text));
|
||||||
|
|
||||||
|
currentPath.push(JSON.parse(text));
|
||||||
|
}
|
||||||
|
|
||||||
|
function startPath() {
|
||||||
|
currentPath = [];
|
||||||
|
receivers.forEach(r => r.send('START'));
|
||||||
|
}
|
||||||
|
|
||||||
|
function finishPath() {
|
||||||
|
paths.push(currentPath);
|
||||||
|
receivers.forEach(r => r.send("STOP"));
|
||||||
}
|
}
|
||||||
|
|
||||||
function clearPaths() {
|
function clearPaths() {
|
||||||
paths.splice(0, paths.length);
|
paths.splice(0, paths.length);
|
||||||
receivers.forEach(r => r.send("CLEAR"));
|
receivers.forEach(r => r.send('CLEAR'));
|
||||||
console.log('Paths have been cleared');
|
console.log('Paths have been cleared');
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
export default class Coordinates {
|
||||||
|
public x: number;
|
||||||
|
public y: number;
|
||||||
|
|
||||||
|
constructor(x: number, y: number) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue