From fc9e68e5aa768b76a07f84da8e7792ca02d8a6e8 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Wed, 5 Mar 2025 11:52:58 +0700 Subject: [PATCH] webSocket test2 --- src/app.ts | 49 +++++++++++++++-------------- src/controllers/ReportController.ts | 26 +++++++-------- 2 files changed, 38 insertions(+), 37 deletions(-) diff --git a/src/app.ts b/src/app.ts index 9d14ce21..272b1a1a 100644 --- a/src/app.ts +++ b/src/app.ts @@ -12,14 +12,15 @@ import { RegisterRoutes } from "./routes"; import { OrganizationController } from "./controllers/OrganizationController"; import logMiddleware from "./middlewares/logs"; import { CommandController } from "./controllers/CommandController"; -// import { WebSocketServer } from "ws"; -// import http from "http"; -// export const wss = new WebSocketServer({ noServer: true }); +import { WebSocketServer } from "ws"; +import http from "http"; +export const wss = new WebSocketServer({ noServer: true }); async function main() { await AppDataSource.initialize(); const app = express(); + const server = http.createServer(app); app.use( cors({ @@ -69,9 +70,26 @@ async function main() { console.error("Error executing function from controller:", error); } }); + + // การจัดการคำขออัปเกรดจาก HTTP เป็น WebSocket + server.on("upgrade", (request:any, socket:any, head:any) => { + console.log("🔹 Handling upgrade request..."); + wss.handleUpgrade(request, socket, head, (ws:any) => { + console.log("🔹 WebSocket connection established"); + wss.emit("connection", ws, request); + }); + }); + wss.on("connection", (ws:any) => { + console.log("✅ Client connected to WebSocket"); + + ws.on("close", () => { + console.log("❌ Client disconnected"); + }); + }); + // app.listen(APP_PORT, APP_HOST, () => console.log(`Listening on: http://localhost:${APP_PORT}`)); - app.listen( + server.listen( APP_PORT, APP_HOST, () => ( @@ -90,29 +108,12 @@ async function main() { runMessageQueue(); - // // สร้างเซิร์ฟเวอร์ HTTP - // const server = http.createServer(app); + // สร้างเซิร์ฟเวอร์ HTTP - // // การจัดการคำขออัปเกรดจาก HTTP เป็น WebSocket - // server.on("upgrade", (request:any, socket:any, head:any) => { - // console.log("🔹 Handling upgrade request..."); - // wss.handleUpgrade(request, socket, head, (ws:any) => { - // console.log("🔹 WebSocket connection established"); - // wss.emit("connection", ws, request); - // }); - // }); - // wss.on("connection", (ws:any) => { - // console.log("✅ Client connected to WebSocket"); - - // ws.on("close", () => { - // console.log("❌ Client disconnected"); - // }); - // }); - - // // เริ่มเซิร์ฟเวอร์ที่พอร์ต 5000 + // เริ่มเซิร์ฟเวอร์ที่พอร์ตปัจจุบัน // server.listen(APP_PORT, () => { - // console.log("✅ HTTP Server is listening on port 5000"); + // console.log("[APP] HTTP Server is listening on current port"); // }); } diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index ade35489..8e23a95f 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -31,8 +31,8 @@ import { Profile } from "../entities/Profile"; import { viewRegistryOfficer } from "../entities/view/viewRegistryOfficer"; import { viewRegistryEmployee } from "../entities/view/viewRegistryEmployee"; import { EmployeeTempPosMaster } from "../entities/EmployeeTempPosMaster"; -// import { WebSocket } from "ws"; -// import { wss } from "../app"; +import { WebSocket } from "ws"; +import { wss } from "../app"; @Route("api/v1/org/report") @Tags("Report") @@ -3565,17 +3565,17 @@ export class ReportController extends Controller { } // console.log(">>",data); - // wss.clients.forEach((client: any) => { - // if (client.readyState === WebSocket.OPEN) { - // const message = JSON.stringify({ template: "report2", reportName: "report2", data: { data } }); - // console.log("📤 Sending data to client:", message); - // client.send(message, (err:any) => { - // if (err) { - // console.error("❌ Error sending message:", err); - // } - // }); - // } - // }); + wss.clients.forEach((client: any) => { + if (client.readyState === WebSocket.OPEN) { + const message = JSON.stringify({ template: "report2", reportName: "report2", data: { data } }); + console.log("📤 Sending data to client:", message); + client.send(message, (err:any) => { + if (err) { + console.error("❌ Error sending message:", err); + } + }); + } + }); return new HttpSuccess({ template: "report2", reportName: "report2", data: { data } }); }