-
Notifications
You must be signed in to change notification settings - Fork 13
/
live_reload_server.ts
73 lines (70 loc) · 2.26 KB
/
live_reload_server.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
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
65
66
67
68
69
70
71
72
73
export class LiveReloadServer {
#socket?: WebSocket;
#listener?: Deno.Listener;
serve(
port = 35729,
) {
this.#listener = Deno.listen({ port });
// console.debug(`livereload websocket server is running on port=${port}`);
(async () => {
if (!this.#listener) return;
for await (const conn of this.#listener) {
const httpConn = Deno.serveHttp(conn);
const requestEvent = await httpConn.nextRequest();
if (requestEvent == null) return;
const { request, respondWith } = requestEvent;
const url = new URL(request.url);
switch (url.pathname) {
case "/livereload.js":
respondWith(
new Response(
`window.onload = () => { new WebSocket("ws://localhost:${port}/livereload").onmessage = () => location.reload(); };`,
),
)
.then(() => httpConn.close(), console.error);
break;
case "/livereload": {
const { response, socket } = Deno.upgradeWebSocket(request);
this.#socket = socket;
// console.debug("socket connected!");
socket.onmessage = (event) => {
// console.debug("ws:Message", event.data);
socket.send(event.data);
};
// socket.onopen = () => console.debug("ws:Open");
socket.onclose = (_event) => {
// const { code, reason } = event;
// console.debug("ws:Close", code, reason);
};
socket.onerror = (event) => {
console.error("failed to receive frame:", event);
if (this.#canCloseSocket()) {
socket.close(1000);
}
};
respondWith(response);
break;
}
default:
httpConn.close();
break;
}
}
})();
}
reload() {
if (!this.#socket) return;
// console.debug("got reload event!");
if (this.#canCloseSocket()) {
this.#socket.send(JSON.stringify({ type: "reload" }));
this.#socket.close(1000);
}
}
close() {
this.#listener?.close();
}
#canCloseSocket(): boolean {
if (!this.#socket) return false;
return this.#socket.readyState === this.#socket.OPEN;
}
}