1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-23 00:55:58 +01:00

Improve error handling in ServerConnection.connect.

This commit is contained in:
Juliusz Chroboczek 2020-09-18 11:28:13 +02:00
parent b2f01a528e
commit e824b93584
2 changed files with 14 additions and 11 deletions

View file

@ -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) {
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);
}; };

View file

@ -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() {
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() {