3 changed files with 128 additions and 41 deletions
@ -0,0 +1,70 @@ |
|||
import express from "express"; |
|||
import { delay } from "../utils/helper"; |
|||
const router = express.Router(); |
|||
|
|||
import points from "../utils/measure.json"; |
|||
import { WsProxy } from "../utils/wss"; |
|||
let ptIndex = 0; |
|||
let intervalId1: ReturnType<typeof setInterval>; |
|||
let intervalId2: ReturnType<typeof setInterval>; |
|||
|
|||
router.post("/startMeasure", (req, res) => { |
|||
setTimeout(() => { |
|||
ptIndex = 0; |
|||
WsProxy.sendFunc({ |
|||
func: "measureTaskEvent", |
|||
data: { event: "START_RECORD_LEFT" }, |
|||
}); |
|||
}, 10); |
|||
intervalId1 = setInterval(() => { |
|||
if (ptIndex >= points.length / 2) { |
|||
clearInterval(intervalId1); |
|||
WsProxy.sendFunc({ |
|||
func: "measureTaskEvent", |
|||
data: { event: "FINISH_RECORD_LEFT" }, |
|||
}); |
|||
setTimeout(() => { |
|||
WsProxy.sendFunc({ |
|||
func: "measureTaskEvent", |
|||
data: { event: "START_RECORD_RIGHT" }, |
|||
}); |
|||
intervalId2 = setInterval(() => { |
|||
if (ptIndex >= points.length) { |
|||
clearInterval(intervalId2); |
|||
ptIndex = 0; |
|||
WsProxy.sendFunc({ |
|||
func: "measureTaskEvent", |
|||
data: { event: "FINISH_RECORD_RIGHT" }, |
|||
}); |
|||
return; |
|||
} |
|||
WsProxy.sendFunc({ |
|||
func: "measurePointEvent", |
|||
data: { |
|||
x: points[ptIndex].x, |
|||
y: points[ptIndex].y + 1, |
|||
side: "right", |
|||
}, |
|||
}); |
|||
ptIndex = ptIndex + 2; |
|||
}, 10); |
|||
}, 2000); |
|||
return; |
|||
} |
|||
// 从中心向左边画
|
|||
const tempIdx = Math.max(Math.floor(points.length / 2) - ptIndex, 0); |
|||
WsProxy.sendFunc({ |
|||
func: "measurePointEvent", |
|||
data: { |
|||
x: points[tempIdx].x, |
|||
y: points[tempIdx].y + 1, |
|||
side: "left", |
|||
}, |
|||
}); |
|||
ptIndex = ptIndex + 2; |
|||
}, 10); |
|||
|
|||
res.json({ success: true }); |
|||
}); |
|||
|
|||
export default router; |
@ -1,50 +1,65 @@ |
|||
import http from 'http'; |
|||
import { Server, WebSocket } from 'ws'; |
|||
import { Datagram } from '../types/wsTypes'; |
|||
import http from "http"; |
|||
import { Server, WebSocket } from "ws"; |
|||
import { Datagram } from "../types/wsTypes"; |
|||
|
|||
export class WsProxy { |
|||
public static server: Server; |
|||
public static server: Server; |
|||
|
|||
private static actionsWhenClientConnect: ((ws: WebSocket) => void)[] = []; |
|||
private static actionsWhenClientConnect: ((ws: WebSocket) => void)[] = []; |
|||
|
|||
static addActionForClientConnect(act: (ws: WebSocket) => void) { |
|||
this.actionsWhenClientConnect.push(act); |
|||
} |
|||
static init(server: http.Server) { |
|||
this.server = new Server({ server }); |
|||
this.onConnection(); |
|||
} |
|||
static addActionForClientConnect(act: (ws: WebSocket) => void) { |
|||
this.actionsWhenClientConnect.push(act); |
|||
} |
|||
static init(server: http.Server) { |
|||
this.server = new Server({ server }); |
|||
this.onConnection(); |
|||
} |
|||
|
|||
private static onConnection() { |
|||
this.server.on("connection", ws => { |
|||
console.log("Client connected"); |
|||
// ws.send("Welcome to the WebSocket server!");
|
|||
// ws.on("message", message => {
|
|||
// console.log(`Received message: ${message}`);
|
|||
// wss.clients.forEach(client => {
|
|||
// if (client.readyState === ws.OPEN) {
|
|||
// client.send(message.toString());
|
|||
// }
|
|||
// });
|
|||
// });
|
|||
this.actionsWhenClientConnect.forEach(act => act(ws)); |
|||
|
|||
private static onConnection() { |
|||
this.server.on('connection', ws => { |
|||
console.log('Client connected'); |
|||
// ws.send("Welcome to the WebSocket server!");
|
|||
// ws.on("message", message => {
|
|||
// console.log(`Received message: ${message}`);
|
|||
// wss.clients.forEach(client => {
|
|||
// if (client.readyState === ws.OPEN) {
|
|||
// client.send(message.toString());
|
|||
// }
|
|||
// });
|
|||
// });
|
|||
this.actionsWhenClientConnect.forEach(act => act(ws)); |
|||
ws.on("close", () => { |
|||
console.log("Client disconnected"); |
|||
}); |
|||
}); |
|||
} |
|||
|
|||
ws.on('close', () => { |
|||
console.log('Client disconnected'); |
|||
}); |
|||
}); |
|||
} |
|||
public static send(data: Datagram, ws?: WebSocket) { |
|||
// DeviceContext
|
|||
if (ws) { |
|||
ws.send(JSON.stringify(data)); |
|||
} else { |
|||
this.server.clients.forEach(ws => { |
|||
if (ws.readyState === ws.OPEN) { |
|||
ws.send(JSON.stringify(data)); |
|||
} |
|||
}); |
|||
} |
|||
} |
|||
|
|||
public static send(data: Datagram, ws?: WebSocket) { |
|||
// DeviceContext
|
|||
if (ws) { |
|||
ws.send(JSON.stringify(data)); |
|||
} else { |
|||
this.server.clients.forEach(ws => { |
|||
if (ws.readyState === ws.OPEN) { |
|||
ws.send(JSON.stringify(data)); |
|||
public static sendFunc( |
|||
data: { func: string; data: Record<string, any> | any[] }, |
|||
ws?: WebSocket |
|||
) { |
|||
if (ws) { |
|||
ws.send(JSON.stringify(data)); |
|||
} else { |
|||
this.server.clients.forEach(ws => { |
|||
if (ws.readyState === ws.OPEN) { |
|||
ws.send(JSON.stringify(data)); |
|||
} |
|||
}); |
|||
} |
|||
}); |
|||
} |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue