You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.3 KiB

5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
  1. import express from "express";
  2. import { Server } from "ws";
  3. import http from "http";
  4. import bodyParser from "body-parser";
  5. import morgan from "morgan";
  6. import cmdRouter from "./routes/cmd";
  7. import debugRouter from "./routes/debug";
  8. import authRouter from "./routes/auth";
  9. import measureRouter from "./routes/measure";
  10. import measureAnalysisRouter from "./routes/measureAnalysis";
  11. import measureDataRouter from "./routes/measureData";
  12. // import { defaultStatus, StatusDatagram } from "./types/wsTypes";
  13. import { wsSend } from "./utils/wss";
  14. import { ContextMessage, defaultContext, defaultMeasureState } from "./types/wsTypes";
  15. const app = express();
  16. app.use(express.static("public"));
  17. app.use(bodyParser.urlencoded());
  18. app.use(bodyParser.json());
  19. app.use(morgan("dev"));
  20. const server = http.createServer(app);
  21. // 在HTTP服务器上初始化WebSocket服务器
  22. const wss = new Server({ server });
  23. wss.on("connection", ws => {
  24. console.log("Client connected");
  25. // ws.send("Welcome to the WebSocket server!");
  26. // ws.on("message", message => {
  27. // console.log(`Received message: ${message}`);
  28. // wss.clients.forEach(client => {
  29. // if (client.readyState === ws.OPEN) {
  30. // client.send(message.toString());
  31. // }
  32. // });
  33. // });
  34. // DeviceContext
  35. ws.send(
  36. JSON.stringify({
  37. messageType: "DeviceContext",
  38. data: app.locals["context"],
  39. path: "/deviceContext",
  40. })
  41. );
  42. // MeasureState
  43. ws.send(
  44. JSON.stringify({
  45. messageType: "EVENT",
  46. data: app.locals["measure"],
  47. path: "/measurement-task/get-task-state",
  48. })
  49. );
  50. ws.on("close", () => {
  51. console.log("Client disconnected");
  52. });
  53. });
  54. app.locals["wss"] = wss;
  55. app.locals["context"] = defaultContext;
  56. app.locals["measure"] = defaultMeasureState;
  57. // app.get("/", (req, res) => {
  58. // res.send("Hello World!");
  59. // });
  60. app.use("/api/debug", debugRouter);
  61. app.use("/api/cmd", cmdRouter);
  62. app.use("/api/auth", authRouter);
  63. app.use("/api/measurement-analysis", measureAnalysisRouter);
  64. app.use("/api/measurement-task", measureRouter);
  65. app.use("/api/measurement-data", measureDataRouter);
  66. //@ts-ignore
  67. app.use((err, req, res, next) => {
  68. console.error(err.stack);
  69. res.status(500).send("Something broke!");
  70. });
  71. // 监听端口
  72. const PORT = process.env.PORT || 8080;
  73. server.listen(PORT, () => {
  74. console.log(`Server is listening on port ${PORT}`);
  75. });