deno.land / x / alosaur@v1.1.1 / examples / ws / chat.handler.ts

chat.handler.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const clients = new Map<number, WebSocket>();let clientId = 0;
function dispatch(msg: string): void { for (const client of clients.values()) { client.send(msg); }}
export function ChatHandler(socket: WebSocket) { const id = ++clientId;
clients.set(id, socket);
socket.onopen = () => { console.log("socket opened"); dispatch(`Connected: [${id}]`); }; socket.onmessage = (e) => { console.log("socket message:", e.data); dispatch(`[${id}]: ${e.data}`); }; socket.onerror = (e) => console.log(`[${id}]: socket errored`); socket.onclose = () => clients.delete(id);}
alosaur

Version Info

Tagged at
a year ago