blob: 0a963bad37003c933ab8b9955add5223a5df471c (
plain) (
blame)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
import type { Handlers } from "./types";
const MAX_ATTEMPTS = 6;
/**
* Browser side of the gateway socket.
*
* The cookie is HttpOnly and handled by the browser; this class never reads or
* stores a credential. A transient close reconnects inside the gateway's grace
* window; a deliberate takeover stops for good.
*/
export class Gateway {
private socket: WebSocket | null = null;
private stopped = false;
private replaced = false;
private attempts = 0;
private timer: number | undefined;
constructor(
private readonly url: string,
private readonly handlers: Handlers,
) {}
connect(): void {
if (this.stopped) return;
this.clearTimer();
let socket: WebSocket;
try {
socket = new WebSocket(this.url);
} catch {
const retrying = this.scheduleReconnect();
if (!retrying) this.handlers.onClosed(false);
return;
}
this.socket = socket;
socket.addEventListener("open", () => {
this.attempts = 0;
this.handlers.onOpen();
});
socket.addEventListener("message", (event) => {
let message: any;
try {
message = JSON.parse(String(event.data));
} catch {
return;
}
switch (message.type) {
case "hello":
this.handlers.onHello(message);
break;
case "view":
this.handlers.onView(message.view);
break;
case "notice":
this.handlers.onNotice(String(message.text ?? ""));
break;
case "error":
this.handlers.onError(String(message.text ?? ""));
break;
case "replaced":
this.replaced = true;
this.handlers.onReplaced();
break;
case "pong":
this.handlers.onPong();
break;
}
});
socket.addEventListener("close", () => {
const wasThisSocket = this.socket === socket;
if (wasThisSocket) this.socket = null;
if (this.replaced || this.stopped) {
this.handlers.onClosed(false);
return;
}
const retrying = this.scheduleReconnect();
this.handlers.onClosed(retrying);
});
socket.addEventListener("error", () => {
// The close event follows; reconnect is scheduled there.
});
}
send(command: unknown): void {
if (this.socket?.readyState !== WebSocket.OPEN) return;
this.socket.send(JSON.stringify({ type: "command", command }));
}
ping(): void {
if (this.socket?.readyState === WebSocket.OPEN) {
this.socket.send(JSON.stringify({ type: "ping" }));
}
}
close(): void {
this.stopped = true;
this.clearTimer();
this.socket?.close();
}
private scheduleReconnect(): boolean {
if (this.stopped || this.replaced || this.attempts >= MAX_ATTEMPTS) return false;
this.attempts += 1;
const delay = Math.min(250 * 2 ** (this.attempts - 1), 4000);
this.timer = window.setTimeout(() => this.connect(), delay);
return true;
}
private clearTimer(): void {
if (this.timer !== undefined) {
window.clearTimeout(this.timer);
this.timer = undefined;
}
}
}
|