lib/client: catch handleMessage errors

This commit is contained in:
Simon Ser 2021-11-21 13:48:07 +01:00
parent 4f927b5536
commit bc3abbec32

View file

@ -115,7 +115,15 @@ export default class Client extends EventTarget {
return;
}
this.ws.addEventListener("open", this.handleOpen.bind(this));
this.ws.addEventListener("message", this.handleMessage.bind(this));
this.ws.addEventListener("message", (event) => {
try {
this.handleMessage(event);
} catch (err) {
this.dispatchEvent(new CustomEvent("error", { detail: err }));
this.disconnect();
}
});
this.ws.addEventListener("close", (event) => {
console.log("Connection closed (code: " + event.code + ")");