-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (52 loc) · 2.18 KB
/
index.js
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import html from "./index.html";
let connections = 0;
export default {
async fetch(request, env) {
const isRoot = new URL(request.url).pathname === "/";
if (!isRoot) return new Response("Not found", { status: 404 });
const { searchParams } = new URL(request.url);
const delay = searchParams.has("delay") ? parseInt(searchParams.get("delay")) : 1000;
const isWebSocket = request.headers.get("upgrade") === "websocket";
if (!isWebSocket) {
return new Response(
html.replace("{{DELAY}}", delay).replace("{{WS_URL}}", request.url.replace("http", "ws")),
{ status: 200, headers: { "content-type": "text/html" }});
}
connections++;
console.log(`${new Date().toISOString()} worker: new connection (delay ${delay}ms); now ${connections}`);
// simulate figuring out which DO to use
if (delay > 0) await new Promise(resolve => setTimeout(resolve, delay));
const id = env.DO.idFromName("default");
const stub = env.DO.get(id);
const response = await stub.fetch(request);
connections--;
console.log(`${new Date().toISOString()} worker: connected; now ${connections}`);
return response;
},
};
export class DO {
constructor(state, env) {
this.env = env;
this.websockets = new Set();
setInterval(() => {
const emoji = String.fromCodePoint(0x1F600 + Math.floor(Math.random() * 69));
for (const ws of this.websockets) {
ws.send(emoji);
}
}, 1000);
}
async fetch(request) {
const {0: clientSocket, 1: serverSocket} = new WebSocketPair();
serverSocket.accept();
serverSocket.addEventListener("message", () => {
serverSocket.close(1000, "OK");
});
serverSocket.addEventListener("close", () => {
this.websockets.delete(serverSocket);
console.log(`${new Date().toISOString()} DO: websocket closed; now ${this.websockets.size}`);
});
this.websockets.add(serverSocket);
console.log(`${new Date().toISOString()} DO: new websocket; now ${this.websockets.size}`);
return new Response(null, { status: 101, webSocket: clientSocket });
}
}