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() {
*
* @param {string} url - The URL to connect to.
* @returns {Promise<ServerConnection>}
* @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);
};
......
......@@ -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() {
......
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