Skip to content

Commit

Permalink
fix(WebsocketManager): Address silent disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
Proximitynow19 committed Mar 5, 2024
1 parent b872a69 commit 4ceff0b
Showing 1 changed file with 57 additions and 20 deletions.
77 changes: 57 additions & 20 deletions src/ws/WebSocketManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,19 @@ export default class WebSocketManager extends EventEmitter {
private pack: (value: any) => Buffer | Uint8Array;
private unpack: (messagePack: Buffer | Uint8Array) => any;

private commands: Map<string, (ws: WebSocketManager, message: any) => Promise<void> | void> =
new Map(
readdirSync(join(__dirname, "commands"))
.filter((file: string) => file.endsWith(__filename.slice(__filename.length - 3)))
.map((file: string) => [
file.slice(0, -3),
require(join(__dirname, "commands", file)).default,
])
);
private commands: Map<
string,
(ws: WebSocketManager, message: any) => Promise<void> | void
> = new Map(
readdirSync(join(__dirname, "commands"))
.filter((file: string) =>
file.endsWith(__filename.slice(__filename.length - 3))
)
.map((file: string) => [
file.slice(0, -3),
require(join(__dirname, "commands", file)).default,
])
);
private lastIddCalculation = Date.now();
private spool?: any;

Expand All @@ -73,7 +77,11 @@ export default class WebSocketManager extends EventEmitter {
new Promise<any>(async (resolve, reject) => {
let spool_ = await checkSpool(spool);

if (!spool_ || !spool_.health.flags.online || spool_.health.flags.avoidDueToHighLoad)
if (
!spool_ ||
!spool_.health.flags.online ||
spool_.health.flags.avoidDueToHighLoad
)
return reject();

resolve(spool_);
Expand Down Expand Up @@ -130,21 +138,35 @@ export default class WebSocketManager extends EventEmitter {
}

public async connect(resume = false, endpoint?: string): Promise<void> {
let ribbon = await api("/server/ribbon", this.client.token, undefined, undefined, undefined, {
expire: new Date().getTime() + 15 * 60000,
key: "server_ribbon_" + this.client.token,
});
let ribbon = await api(
"/server/ribbon",
this.client.token,
undefined,
undefined,
undefined,
{
expire: new Date().getTime() + 15 * 60000,
key: "server_ribbon_" + this.client.token,
}
);

if (!this.spool) this.spool = await this.getOptimalSpool(ribbon);

if (!endpoint) ({ endpoint } = ribbon);

this.socket = new WebSocket(`wss://${this.spool.host}${endpoint}`, ribbon.spools.token);
this.socket = new WebSocket(
`wss://${this.spool.host}${endpoint}`,
ribbon.spools.token
);

this.socket.on("error", (err: string) => {
throw new Error(err);
});

this.socket.on("close", () => {
this.connect(true); // i guess temporary fix on silent disconnect
});

this.socket.on("open", () => {
if (resume) {
this.send(
Expand All @@ -163,7 +185,10 @@ export default class WebSocketManager extends EventEmitter {

this.heartbeat = setInterval(() => {
this.socket?.send(
Buffer.from([WebSocketManager.MESSAGE_TYPE.EXTENSION, WebSocketManager.EXTENSION.PING])
Buffer.from([
WebSocketManager.MESSAGE_TYPE.EXTENSION,
WebSocketManager.EXTENSION.PING,
])
);
}, 5000);
});
Expand All @@ -183,8 +208,13 @@ export default class WebSocketManager extends EventEmitter {
lengths.push(length);
}
lengths.forEach((length, i) => {
let offset = lengths.slice(0, i).reduce((a, b) => a + b, 5 + lengths.length * 4);
this.socket?.emit("message", data.subarray(offset, offset + length));
let offset = lengths
.slice(0, i)
.reduce((a, b) => a + b, 5 + lengths.length * 4);
this.socket?.emit(
"message",
data.subarray(offset, offset + length)
);
});
break;
case WebSocketManager.MESSAGE_TYPE.EXTENSION:
Expand All @@ -200,7 +230,11 @@ export default class WebSocketManager extends EventEmitter {
});
}

public send(message: any, id = true, type = WebSocketManager.MESSAGE_TYPE.STANDARD) {
public send(
message: any,
id = true,
type = WebSocketManager.MESSAGE_TYPE.STANDARD
) {
if (id) {
message.id = ++this.messageId;

Expand All @@ -212,7 +246,10 @@ export default class WebSocketManager extends EventEmitter {
0,
Math.max(
100,
Math.min(30 * (1000 / (currentCalculation - this.lastIddCalculation)), 2000)
Math.min(
30 * (1000 / (currentCalculation - this.lastIddCalculation)),
2000
)
)
);
this.lastIddCalculation = currentCalculation;
Expand Down

0 comments on commit 4ceff0b

Please sign in to comment.