Correctly handle a websocket error

This commit is contained in:
Lucas Fialho Zawacki 2018-07-31 17:11:11 -03:00
parent 177aa04cbe
commit db4687e396
2 changed files with 18 additions and 26 deletions

View File

@ -157,9 +157,4 @@ module.exports = class BigBlueButtonGW extends EventEmitter {
setEventEmitter (emitter) {
this.emitter = emitter;
}
_onServerResponse(data) {
// Here this is the 'ws' instance
this.sendMessage(data);
}
}

View File

@ -7,25 +7,6 @@ const Logger = require('../utils/Logger');
// ID counter
let connectionIDCounter = 0;
ws.prototype.sendMessage = function(json) {
let websocket = this;
if (this._closeCode === 1000) {
Logger.error("[WebsocketConnectionManager] Websocket closed, not sending");
this._errorCallback("[WebsocketConnectionManager] Error: not opened");
}
return this.send(JSON.stringify(json), function(error) {
if(error) {
Logger.error('[WebsocketConnectionManager] Websocket error "' + error + '" on message "' + json.id + '"');
websocket._errorCallback(error);
}
});
};
module.exports = class WebsocketConnectionManager {
constructor (server, path) {
this.wss = new ws.Server({
@ -48,7 +29,7 @@ module.exports = class WebsocketConnectionManager {
const connectionId = data? data.connectionId : null;
const ws = this.webSockets[connectionId];
if (ws) {
ws.sendMessage(data);
this.sendMessage(ws, data);
}
}
@ -77,7 +58,7 @@ module.exports = class WebsocketConnectionManager {
message = JSON.parse(data);
if (message.id === 'ping') {
ws.sendMessage({id: 'pong'});
this.sendMessage(ws, {id: 'pong'});
return;
}
@ -134,4 +115,20 @@ module.exports = class WebsocketConnectionManager {
delete this.webSockets[ws.id];
}
sendMessage (ws, json) {
if (ws._closeCode === 1000) {
Logger.error("[WebsocketConnectionManager] Websocket closed, not sending");
this._onError(ws, "[WebsocketConnectionManager] Error: not opened");
}
return ws.send(JSON.stringify(json), (error) => {
if(error) {
Logger.error('[WebsocketConnectionManager] Websocket error "' + error + '" on message "' + json.id + '"');
this._onError(ws, error);
}
});
}
}