Skip to content

Commit

Permalink
remove try catch websocket ^& add onError method
Browse files Browse the repository at this point in the history
  • Loading branch information
pantyetta committed Oct 8, 2023
1 parent a78eacf commit 42868a5
Showing 1 changed file with 35 additions and 35 deletions.
70 changes: 35 additions & 35 deletions chrome extension/public/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,46 +217,46 @@
if (!settings.url || !settings.uid) return;

let init = false;
try {
this.connection = new WebSocket(settings.url);
this.connection.binaryType = "arraybuffer";

this.connection.onclose = () => {
this.connection = null;
this.status = false;
this.onChangeMenu(false);
}

this.connection.onopen = async () => {
this.status = true;
this.send("init", "request");
await this.wait(1000);
if (!init) {
this.close();
return;
}
this.send("rename", settings.uid);
this.onChangeMenu(true);

while (this.isStatus()) {
this.send("ping", "ping");
await this.wait(5000);
}
this.connection = new WebSocket(settings.url);
this.connection.binaryType = "arraybuffer";

this.connection.onerror = (e) => {
console.warn(e);
}

this.connection.onclose = async () => {
this.connection = null;
this.status = false;
this.onChangeMenu(false);
}

this.connection.onopen = async () => {
this.status = true;
this.send("init", "request");
await this.wait(1000);
if (!init) {
this.close();
return;
}
this.send("rename", settings.uid);
this.onChangeMenu(true);

this.connection.onmessage = (e) => {
const json = JSON.parse(e.data);
if (json.type === "tell") {
this.onTellmsg(json);
} else if (json.type === "get") {
this.onGetmsg(json);
} else if (json.type === "init" && json.msg === "success") {
init = true;
}
while (this.isStatus()) {
this.send("ping", "ping");
await this.wait(5000);
}
}

} catch (error) {
console.warn(error);
this.connection.onmessage = (e) => {
const json = JSON.parse(e.data);
if (json.type === "tell") {
this.onTellmsg(json);
} else if (json.type === "get") {
this.onGetmsg(json);
} else if (json.type === "init" && json.msg === "success") {
init = true;
}
}
}

Expand Down

0 comments on commit 42868a5

Please sign in to comment.