Skip to content

Commit

Permalink
fix: ws
Browse files Browse the repository at this point in the history
  • Loading branch information
whes1015 committed Nov 30, 2024
1 parent 2cc1a5b commit 6578e09
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Server {

this.urls = urls;
this.ws = null;
this.reconnect = true;

this.config = config.getConfig();

Expand All @@ -31,6 +32,8 @@ class Server {
}

connect() {
if (!this.reconnect) return;
if (this.ws) this.ws.terminate();
this.ws = null;
const url = `wss://${this.urls[Math.floor(Math.random() * this.urls.length)]}/websocket`;
this.ws = new WebSocket(url);
Expand All @@ -42,6 +45,8 @@ class Server {
this.ws.onclose = () => {
this.ws = null;
logger.info("WebSocket close");

setTimeout(this.connect, 3000);
};

this.ws.onerror = (error) => {
Expand All @@ -62,9 +67,16 @@ class Server {
this.send(this.wsConfig);
break;
}
case "info":{
if (json.data.code == 401) {
this.reconnect = false;
logger.info("WebSocket close -> 401");
this.ws.close();
}
break;
}
default:{
logger.info("Received message:", json);

}
}
};
Expand Down

0 comments on commit 6578e09

Please sign in to comment.