From eabc379adf8d33f991b6882d1690b6df9ea8bc20 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Fri, 7 Mar 2025 16:29:28 +0700 Subject: [PATCH] ws test --- src/app.ts | 69 ++++++++++++++++++++++++------------------------------ 1 file changed, 30 insertions(+), 39 deletions(-) diff --git a/src/app.ts b/src/app.ts index 344c521e..d258513a 100644 --- a/src/app.ts +++ b/src/app.ts @@ -15,11 +15,29 @@ import { CommandController } from "./controllers/CommandController"; import { ProfileSalaryController } from "./controllers/ProfileSalaryController"; import { WebSocketServer } from "ws"; -import http from "http"; -export const wss = new WebSocketServer({ noServer: true, - path: "/api/v1/org-socket", - }); +export const wss = new WebSocketServer({ port: 13002, path: "/api/v1/org-socket" }); + +// การจัดการคำขออัปเกรดจาก HTTP เป็น WebSocket +wss.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"); + }); + + ws.on("error", (error: any) => { + console.error("WebSocket error:", error); + }); +}); async function main() { await AppDataSource.initialize(); @@ -89,14 +107,14 @@ async function main() { }); // app.listen(APP_PORT, APP_HOST, () => console.log(`Listening on: http://localhost:${APP_PORT}`)); - // app.listen( - // APP_PORT, - // APP_HOST, - // () => ( - // console.log(`[APP] Application is running on: http://localhost:${APP_PORT}`), - // console.log(`[APP] Swagger on: http://localhost:${APP_PORT}/api-docs`) - // ), - // ); + app.listen( + APP_PORT, + APP_HOST, + () => ( + console.log(`[APP] Application is running on: http://localhost:${APP_PORT}`), + console.log(`[APP] Swagger on: http://localhost:${APP_PORT}/api-docs`) + ), + ); async function runMessageQueue() { try { await rabbitmqInit(); @@ -108,34 +126,7 @@ async function main() { runMessageQueue(); - const server = http.createServer(app); - // การจัดการคำขออัปเกรดจาก 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"); - }); - - ws.on("error", (error:any) => { - console.error("WebSocket error:", error); - }); - }); - - server.listen(APP_PORT, APP_HOST, () => { - console.log(`[APP] Application is running on: http://localhost:${APP_PORT}`); - console.log(`[APP] Swagger on: http://localhost:${APP_PORT}/api-docs`); - console.log("[APP] HTTP Server is listening on current port"); - }); } main();