ปิด web socket

This commit is contained in:
kittapath 2025-03-11 13:07:59 +07:00
parent 0e2b534277
commit 759d42c149
3 changed files with 1089 additions and 1152 deletions

View file

@ -14,7 +14,7 @@ import logMiddleware from "./middlewares/logs";
import { CommandController } from "./controllers/CommandController";
import { ProfileSalaryController } from "./controllers/ProfileSalaryController";
import { initWebSocket } from "./services/webSocket";
// import { initWebSocket } from "./services/webSocket";
async function main() {
await AppDataSource.initialize();

File diff suppressed because it is too large Load diff

View file

@ -1,43 +1,43 @@
import { WebSocketServer } from "ws";
// import { WebSocketServer } from "ws";
let wss: WebSocketServer;
// let wss: WebSocketServer;
export function initWebSocket() {
wss = new WebSocketServer({ port: 13002, path: "/api/v1/org-socket" });
// export function initWebSocket() {
// 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);
});
});
// // การจัดการคำขออัปเกรดจาก 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");
// wss.on("connection", (ws: any) => {
// console.log("✅ Client connected to WebSocket");
ws.on("close", () => {
console.log("❌ Client disconnected");
});
// ws.on("close", () => {
// console.log("❌ Client disconnected");
// });
ws.on("error", (error: any) => {
console.error("WebSocket error:", error);
});
});
}
// ws.on("error", (error: any) => {
// console.error("WebSocket error:", error);
// });
// });
// }
export async function sendWebSocket(data: any) {
if (!wss) initWebSocket();
wss.clients.forEach((client: any) => {
if (client.readyState === WebSocket.OPEN) {
const message = JSON.stringify(data);
console.log("📤 Sending data to client:", message);
client.send(message, (err: any) => {
if (err) {
console.error("❌ Error sending message:", err);
}
});
}
});
}
// export async function sendWebSocket(data: any) {
// if (!wss) initWebSocket();
// wss.clients.forEach((client: any) => {
// if (client.readyState === WebSocket.OPEN) {
// const message = JSON.stringify(data);
// console.log("📤 Sending data to client:", message);
// client.send(message, (err: any) => {
// if (err) {
// console.error("❌ Error sending message:", err);
// }
// });
// }
// });
// }