Skip to content

Commit

Permalink
fix: Add mayReconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
Sup3rFire committed Mar 29, 2024
1 parent 226bf41 commit 11a4eb1
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 58 deletions.
1 change: 1 addition & 0 deletions src/client/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export default class Client {
}

public logout(): void {
this.ws.mayReconnect = false;
this.ws.send({ command: "die" }, false);
clearInterval(this.ws.heartbeat);
this.ws.socket?.close();
Expand Down
76 changes: 22 additions & 54 deletions src/ws/WebSocketManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +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;

public mayReconnect = true;
public heartbeat?: NodeJS.Timeout;
public socket?: WebSocket;
public client: Client;
Expand All @@ -77,11 +74,7 @@ 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 @@ -138,33 +131,23 @@ 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.mayReconnect && this.connect(true); // i guess temporary fix on silent disconnect
});

this.socket.on("open", () => {
Expand All @@ -185,10 +168,7 @@ 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 @@ -208,13 +188,8 @@ 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 @@ -230,11 +205,7 @@ 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 @@ -246,10 +217,7 @@ 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
1 change: 1 addition & 0 deletions src/ws/commands/kick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import WebSocketManager from "../WebSocketManager";

export default function (ws: WebSocketManager, { data: { reason } }: any) {
clearInterval(ws.heartbeat);
ws.mayReconnect = false;
ws.socket?.close();
throw new Error(reason);
}
7 changes: 3 additions & 4 deletions src/ws/commands/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import WebSocketManager from "../WebSocketManager";

export default async function (
ws: WebSocketManager,
{ data: { endpoint } }: any
) {
export default async function (ws: WebSocketManager, { data: { endpoint } }: any) {
clearInterval(ws.heartbeat);
ws.mayReconnect = false;
ws.socket?.close();
await ws.connect(true, endpoint);
ws.mayReconnect = true;
}
1 change: 1 addition & 0 deletions src/ws/commands/nope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import WebSocketManager from "../WebSocketManager";

export default function (ws: WebSocketManager, { reason }: any) {
clearInterval(ws.heartbeat);
ws.mayReconnect = false;
ws.socket?.close();

throw new Error(reason);
Expand Down

0 comments on commit 11a4eb1

Please sign in to comment.