Skip to content

Commit

Permalink
fix: Prevent throwing unecessary errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Sup3rFire committed Mar 29, 2024
1 parent ba3841c commit 5cde1d3
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 15 deletions.
11 changes: 10 additions & 1 deletion src/client/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import api from "../util/api";
import Room from "../room/Room";
import User from "../user/User";
import channelApi from "../util/channelApi";
import { EventEmitter } from "ws";

/**
* Represents the client.
*/
export default class Client {
export default class Client extends EventEmitter {
private readonly ws = new WebSocketManager(this);

/**
Expand Down Expand Up @@ -98,3 +99,11 @@ export default class Client {
return new User(this.ws, user_);
}
}

export default interface Client extends EventEmitter {
/** Emitted when the client violates Ribbon's protocol. */
on(eventName: "nope", listener: (reason: string) => void): this;

/** Emitted when the client gets kicked by the server. */
on(eventName: "kick", listener: (reason: string) => void): this;
}
8 changes: 4 additions & 4 deletions src/ws/WebSocketManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@ export default class WebSocketManager extends EventEmitter {

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("error", (err: string) => {
// throw new Error(err);
// });

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

this.socket.on("open", () => {
Expand Down
5 changes: 0 additions & 5 deletions src/ws/commands/error.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/ws/commands/kick.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import WebSocketManager from "../WebSocketManager";

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

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

throw new Error(reason);
ws.client.emit("nope", reason);
}

0 comments on commit 5cde1d3

Please sign in to comment.