-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathchat-server.deno.mjs
45 lines (37 loc) · 1.2 KB
/
chat-server.deno.mjs
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
// See ./README.md for instructions on how to run this benchmark.
const port = Deno.env.get("PORT") || 4001;
const CLIENTS_TO_WAIT_FOR = parseInt(Deno.env.get("CLIENTS_COUNT") || "", 10) || 32;
var clients = [];
async function reqHandler(req) {
if (req.headers.get("upgrade") != "websocket") {
return new Response(null, { status: 501 });
}
const { socket: client, response } = Deno.upgradeWebSocket(req);
clients.push(client);
const name = new URL(req.url).searchParams.get("name");
console.log(`${name} connected (${CLIENTS_TO_WAIT_FOR - clients.length} remain)`);
client.onmessage = event => {
const msg = `${name}: ${event.data}`;
for (let client of clients) {
client.send(msg);
}
};
client.onclose = () => {
clients.splice(clients.indexOf(client), 1);
};
if (clients.length === CLIENTS_TO_WAIT_FOR) {
sendReadyMessage();
}
return response;
}
function sendReadyMessage() {
console.log("All clients connected");
setTimeout(() => {
console.log("Starting benchmark");
for (let client of clients) {
client.send(`ready`);
}
}, 100);
}
console.log(`Waiting for ${CLIENTS_TO_WAIT_FOR} clients to connect..`);
Deno.serve({ port }, reqHandler);