-
Notifications
You must be signed in to change notification settings - Fork 46
/
closeSocket.ts
57 lines (36 loc) · 1.87 KB
/
closeSocket.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
/**
* This script terminates websockets.
*/
import socketUtility from "./socketUtility.js";
import { removeConnectionFromConnectionLists, unsubSocketFromAllSubs } from "./socketManager.js";
// @ts-ignore
import wsutil from "../../client/scripts/esm/util/wsutil.js";
// Type Definitions ---------------------------------------------------------------------------
import type { CustomWebSocket } from "./socketUtility.js";
// Functions ---------------------------------------------------------------------------
function onclose(ws: CustomWebSocket, code: number, reason: Buffer) {
const reasonString = reason.toString();
// Delete connection from object.
removeConnectionFromConnectionLists(ws, code, reasonString);
// What if the code is 1000, and reason is "Connection closed by client"?
// I then immediately want to delete their invite.
// But what other reasons could it close... ?
// Code 1006, Message "" is just a network failure.
// True if client had no power over the closure,
// DON'T COUNT this as a disconnection!
// They would want to keep their invite, AND remain in their game!
const closureNotByChoice = wsutil.wasSocketClosureNotByTheirChoice(code, reasonString);
// Unsubscribe them from all. NO LIST. It doesn't matter if they want to keep their invite or remain
// connected to their game, without a websocket to send updates to, there's no point in any SUBSCRIPTION service!
// Unsubbing them from their game will start their auto-resignation timer.
unsubSocketFromAllSubs(ws, closureNotByChoice);
cancelRenewConnectionTimer(ws);
if (reasonString === 'No echo heard') console.log(`Socket closed from no echo heard. ${socketUtility.stringifySocketMetadata(ws)}`);
}
function cancelRenewConnectionTimer(ws: CustomWebSocket) {
clearTimeout(ws.metadata.renewConnectionTimeoutID);
ws.metadata.renewConnectionTimeoutID = undefined;
}
export {
onclose,
};