-
Notifications
You must be signed in to change notification settings - Fork 137
/
reconnect.ts
45 lines (38 loc) · 1.25 KB
/
reconnect.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
import { RoomEvent } from '../../room/events';
import { ConnectionState } from '../../room/Room';
import { Checker } from './Checker';
export class ReconnectCheck extends Checker {
get description(): string {
return 'Resuming connection after interruption';
}
async perform(): Promise<void> {
const room = await this.connect();
let reconnectingTriggered = false;
let reconnected = false;
let reconnectResolver: (value: unknown) => void;
const reconnectTimeout = new Promise((resolve) => {
setTimeout(resolve, 5000);
reconnectResolver = resolve;
});
room
.on(RoomEvent.Reconnecting, () => {
reconnectingTriggered = true;
})
.on(RoomEvent.Reconnected, () => {
reconnected = true;
reconnectResolver(true);
});
room.engine.client.ws?.close();
const onClose = room.engine.client.onClose;
if (onClose) {
onClose('');
}
await reconnectTimeout;
if (!reconnectingTriggered) {
throw new Error('Did not attempt to reconnect');
} else if (!reconnected || room.state !== ConnectionState.Connected) {
this.appendWarning('reconnection is only possible in Redis-based configurations');
throw new Error('Not able to reconnect');
}
}
}