Commit e824b935 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Improve error handling in ServerConnection.connect.

parent b2f01a52
...@@ -187,8 +187,9 @@ ServerConnection.prototype.getIceServers = async function() { ...@@ -187,8 +187,9 @@ ServerConnection.prototype.getIceServers = async function() {
* *
* @param {string} url - The URL to connect to. * @param {string} url - The URL to connect to.
* @returns {Promise<ServerConnection>} * @returns {Promise<ServerConnection>}
* @function
*/ */
ServerConnection.prototype.connect = function(url) { ServerConnection.prototype.connect = async function(url) {
let sc = this; let sc = this;
if(sc.socket) { if(sc.socket) {
sc.socket.close(1000, 'Reconnecting'); sc.socket.close(1000, 'Reconnecting');
...@@ -197,19 +198,15 @@ ServerConnection.prototype.connect = function(url) { ...@@ -197,19 +198,15 @@ ServerConnection.prototype.connect = function(url) {
if(!sc.iceServers) { if(!sc.iceServers) {
try { try {
sc.getIceServers(); await sc.getIceServers();
} catch(e) { } catch(e) {
console.error(e); console.warn(e);
} }
} }
try { sc.socket = new WebSocket(url);
sc.socket = new WebSocket(url);
} catch(e) {
return Promise.reject(e);
}
return new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
this.socket.onerror = function(e) { this.socket.onerror = function(e) {
reject(e); reject(e);
}; };
......
...@@ -1296,7 +1296,7 @@ window.onclick = function(event) { ...@@ -1296,7 +1296,7 @@ window.onclick = function(event) {
} }
}; };
function serverConnect() { async function serverConnect() {
serverConnection = new ServerConnection(); serverConnection = new ServerConnection();
serverConnection.onconnected = gotConnected; serverConnection.onconnected = gotConnected;
serverConnection.onclose = gotClose; serverConnection.onclose = gotClose;
...@@ -1311,7 +1311,13 @@ function serverConnect() { ...@@ -1311,7 +1311,13 @@ function serverConnect() {
else else
displayWarning(`The server said: ${message}`); 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() { function start() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment