webSocket สาธุ99

This commit is contained in:
AdisakKanthawilang 2025-03-06 14:41:41 +07:00
parent a828693d2e
commit 0f639826c3
3 changed files with 27 additions and 37 deletions

16
src/services/webSocket.ts Normal file
View file

@ -0,0 +1,16 @@
import { WebSocket } from "ws";
import { wss } from "../app";
export async function sendWebSocket(data:any){
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);
}
});
}
});
}