From e824b9358465f4c0a7c0fc3a3660883aa5ab04c8 Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Fri, 18 Sep 2020 11:28:13 +0200 Subject: [PATCH] Improve error handling in ServerConnection.connect. --- static/protocol.js | 15 ++++++--------- static/sfu.js | 10 ++++++++-- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/static/protocol.js b/static/protocol.js index a49749f..87de77f 100644 --- a/static/protocol.js +++ b/static/protocol.js @@ -187,8 +187,9 @@ ServerConnection.prototype.getIceServers = async function() { * * @param {string} url - The URL to connect to. * @returns {Promise} + * @function */ -ServerConnection.prototype.connect = function(url) { +ServerConnection.prototype.connect = async function(url) { let sc = this; if(sc.socket) { sc.socket.close(1000, 'Reconnecting'); @@ -197,19 +198,15 @@ ServerConnection.prototype.connect = function(url) { if(!sc.iceServers) { try { - sc.getIceServers(); + await sc.getIceServers(); } catch(e) { - console.error(e); + console.warn(e); } } - try { - sc.socket = new WebSocket(url); - } catch(e) { - return Promise.reject(e); - } + sc.socket = new WebSocket(url); - return new Promise((resolve, reject) => { + return await new Promise((resolve, reject) => { this.socket.onerror = function(e) { reject(e); }; diff --git a/static/sfu.js b/static/sfu.js index 5380ef9..e5a0f60 100644 --- a/static/sfu.js +++ b/static/sfu.js @@ -1296,7 +1296,7 @@ window.onclick = function(event) { } }; -function serverConnect() { +async function serverConnect() { serverConnection = new ServerConnection(); serverConnection.onconnected = gotConnected; serverConnection.onclose = gotClose; @@ -1311,7 +1311,13 @@ function serverConnect() { else displayWarning(`The server said: ${message}`); } - return serverConnection.connect(`ws${location.protocol === 'https:' ? 's' : ''}://${location.host}/ws`); + let url = `ws${location.protocol === 'https:' ? 's' : ''}://${location.host}/ws`; + try { + await serverConnection.connect(url); + } catch(e) { + console.error(e); + displayError(e.message ? e.message : "Couldn't connect to " + url); + } } function start() {