summary refs log tree commit diff
path: root/webrtc/src/util/Heartbeat.ts
blob: 8c5e3a7a42cae52cb7ad2363fc2f046979b84f6a (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
import { WebSocket, CLOSECODES } from "@fosscord/gateway";
import { VoiceOPCodes } from "@fosscord/util";

export async function setHeartbeat(socket: WebSocket, nonce?: Number) {
	if (socket.heartbeatTimeout) clearTimeout(socket.heartbeatTimeout);

	socket.heartbeatTimeout = setTimeout(() => {
		return socket.close(CLOSECODES.Session_timed_out);
	}, 1000 * 45);

	if (!nonce) {
		socket.send(JSON.stringify({
			op: VoiceOPCodes.HELLO,
			d: {
				v: 5,
				heartbeat_interval: 13750,
			}
		}));
	}
	else {
		socket.send(JSON.stringify({ op: VoiceOPCodes.HEARTBEAT_ACK, d: nonce }));
	}
}