diff --git a/lib/client.js b/lib/client.js index 5b81ff1..394caec 100644 --- a/lib/client.js +++ b/lib/client.js @@ -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 + ")");