From f70ff2424ea4b71a25efbfe5aa09f6e4e52507ad Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Tue, 9 Jun 2020 01:46:17 +0200 Subject: [PATCH] Detect sends on closed websocket explicitly. It turns out that send on a closed websocket doesn't throw, so handle this case explicitly. Thanks to Giuseppe Castagna for noticing. --- static/sfu.js | 60 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 21 deletions(-) diff --git a/static/sfu.js b/static/sfu.js index 102ae39..81d06d1 100644 --- a/static/sfu.js +++ b/static/sfu.js @@ -67,10 +67,13 @@ Connection.prototype.close = function(sendit) { this.pc.close(); if(sendit) { - send({ - type: 'close', - id: this.id, - }); + try { + send({ + type: 'close', + id: this.id, + }); + } catch(e) { + } } }; @@ -526,7 +529,6 @@ function serverConnect() { return new Promise((resolve, reject) => { socket.onerror = function(e) { - console.error(e); reject(e.error ? e.error : e); }; socket.onopen = function(e) { @@ -534,14 +536,21 @@ function serverConnect() { resetChat(); setConnected(true); let up = getUserPass(); - send({ - type: 'handshake', - id: myid, - group: group, - username: up.username, - password: up.password, - }); - sendRequest(document.getElementById('requestselect').value); + try { + send({ + type: 'handshake', + id: myid, + group: group, + username: up.username, + password: up.password, + }) + sendRequest(document.getElementById('requestselect').value); + } catch(e) { + console.error(e); + displayError(e); + reject(e); + return; + } resolve(); }; socket.onclose = function(e) { @@ -730,6 +739,10 @@ async function addIceCandidates(conn) { function send(m) { if(!m) throw(new Error('Sending null message')); + if(socket.readyState !== socket.OPEN) { + // send on a closed connection doesn't throw + throw(new Error('Connection is not open')); + } return socket.send(JSON.stringify(m)); } @@ -993,14 +1006,19 @@ function handleInput() { return; } - addToChatbox(myid, username, message, me); - send({ - type: 'chat', - id: myid, - username: username, - value: message, - me: me, - }); + try { + let a = send({ + type: 'chat', + id: myid, + username: username, + value: message, + me: me, + }); + addToChatbox(myid, username, message, me); + } catch(e) { + console.error(e); + displayError(e); + } } document.getElementById('inputform').onsubmit = function(e) {