ปิด web socket
This commit is contained in:
parent
0e2b534277
commit
759d42c149
3 changed files with 1089 additions and 1152 deletions
|
|
@ -14,7 +14,7 @@ import logMiddleware from "./middlewares/logs";
|
||||||
import { CommandController } from "./controllers/CommandController";
|
import { CommandController } from "./controllers/CommandController";
|
||||||
import { ProfileSalaryController } from "./controllers/ProfileSalaryController";
|
import { ProfileSalaryController } from "./controllers/ProfileSalaryController";
|
||||||
|
|
||||||
import { initWebSocket } from "./services/webSocket";
|
// import { initWebSocket } from "./services/webSocket";
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
await AppDataSource.initialize();
|
await AppDataSource.initialize();
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load diff
|
|
@ -1,43 +1,43 @@
|
||||||
import { WebSocketServer } from "ws";
|
// import { WebSocketServer } from "ws";
|
||||||
|
|
||||||
let wss: WebSocketServer;
|
// let wss: WebSocketServer;
|
||||||
|
|
||||||
export function initWebSocket() {
|
// export function initWebSocket() {
|
||||||
wss = new WebSocketServer({ port: 13002, path: "/api/v1/org-socket" });
|
// wss = new WebSocketServer({ port: 13002, path: "/api/v1/org-socket" });
|
||||||
|
|
||||||
// การจัดการคำขออัปเกรดจาก HTTP เป็น WebSocket
|
// // การจัดการคำขออัปเกรดจาก HTTP เป็น WebSocket
|
||||||
wss.on("upgrade", (request: any, socket: any, head: any) => {
|
// wss.on("upgrade", (request: any, socket: any, head: any) => {
|
||||||
console.log("🔹 Handling upgrade request...");
|
// console.log("🔹 Handling upgrade request...");
|
||||||
wss.handleUpgrade(request, socket, head, (ws: any) => {
|
// wss.handleUpgrade(request, socket, head, (ws: any) => {
|
||||||
console.log("🔹 WebSocket connection established");
|
// console.log("🔹 WebSocket connection established");
|
||||||
wss.emit("connection", ws, request);
|
// wss.emit("connection", ws, request);
|
||||||
});
|
// });
|
||||||
});
|
// });
|
||||||
|
|
||||||
wss.on("connection", (ws: any) => {
|
// wss.on("connection", (ws: any) => {
|
||||||
console.log("✅ Client connected to WebSocket");
|
// console.log("✅ Client connected to WebSocket");
|
||||||
|
|
||||||
ws.on("close", () => {
|
// ws.on("close", () => {
|
||||||
console.log("❌ Client disconnected");
|
// console.log("❌ Client disconnected");
|
||||||
});
|
// });
|
||||||
|
|
||||||
ws.on("error", (error: any) => {
|
// ws.on("error", (error: any) => {
|
||||||
console.error("WebSocket error:", error);
|
// console.error("WebSocket error:", error);
|
||||||
});
|
// });
|
||||||
});
|
// });
|
||||||
}
|
// }
|
||||||
|
|
||||||
export async function sendWebSocket(data: any) {
|
// export async function sendWebSocket(data: any) {
|
||||||
if (!wss) initWebSocket();
|
// if (!wss) initWebSocket();
|
||||||
wss.clients.forEach((client: any) => {
|
// wss.clients.forEach((client: any) => {
|
||||||
if (client.readyState === WebSocket.OPEN) {
|
// if (client.readyState === WebSocket.OPEN) {
|
||||||
const message = JSON.stringify(data);
|
// const message = JSON.stringify(data);
|
||||||
console.log("📤 Sending data to client:", message);
|
// console.log("📤 Sending data to client:", message);
|
||||||
client.send(message, (err: any) => {
|
// client.send(message, (err: any) => {
|
||||||
if (err) {
|
// if (err) {
|
||||||
console.error("❌ Error sending message:", err);
|
// console.error("❌ Error sending message:", err);
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
}
|
// }
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue