-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_server.ts
More file actions
49 lines (45 loc) · 1.35 KB
/
example_server.ts
File metadata and controls
49 lines (45 loc) · 1.35 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import {
serve,
isWebSocketPingEvent,
isWebSocketPongEvent,
isWebSocketCloseEvent,
} from "./mod.ts";
// Example WebSocket echo/broadcast server
const wss = serve({ port: 8080 });
console.log(`ws://localhost:8080`);
for await (const { socket, event } of wss) {
if (isWebSocketPingEvent(event) || isWebSocketPongEvent(event)) {
// ping/pong
const [type, body] = event;
console.log("ws:" + type, body);
// ping and pong frames are already handled by the server,
// thus there is no need to call `socket.pong()` manually.
} else if (isWebSocketCloseEvent(event)) {
// close
const { code, reason } = event;
console.log("ws:close", code, reason);
} else {
// text/binary message
if (typeof event === "string") {
console.log("ws:text", event);
} else if (event instanceof Uint8Array) {
console.log("ws:binary", event);
}
// echo message
socket.send(event);
// broadcast message to all clients (including the client which sent the message)
for (const sock of wss.sockets) {
if (!sock.isClosed) sock.send(event);
}
if (event === "close") {
// Close the socket that sent the message "close"
socket.close();
} else if (event === "quit") {
// Close the WebSocket server if message === "quit".
// This will make the AsyncIterableIterator finish,
// which will break the for await loop
wss.close();
}
}
}
Deno.exit(0);