openvidu/openvidu-browser/lib/OpenViduInternal/KurentoUtils/kurento-jsonrpc/clients/transports/webSocketWithReconnection.js

166 lines
5.3 KiB
JavaScript
Raw Normal View History

"use strict";
var BrowserWebSocket = global.WebSocket || global.MozWebSocket;
var Logger = console;
2018-04-26 15:33:47 +02:00
var MAX_RETRIES = 2000;
var RETRY_TIME_MS = 3000;
var CONNECTING = 0;
var OPEN = 1;
var CLOSING = 2;
var CLOSED = 3;
function WebSocketWithReconnection(config) {
var closing = false;
var registerMessageHandler;
var wsUri = config.uri;
var useSockJS = config.useSockJS;
var reconnecting = false;
var forcingDisconnection = false;
var ws;
if (useSockJS) {
ws = new SockJS(wsUri);
2018-04-26 15:33:47 +02:00
}
else {
ws = new WebSocket(wsUri);
}
2018-04-26 15:33:47 +02:00
ws.onopen = function () {
logConnected(ws, wsUri);
if (config.onconnected) {
config.onconnected();
}
};
2018-04-26 15:33:47 +02:00
ws.onerror = function (error) {
Logger.error("Could not connect to " + wsUri + " (invoking onerror if defined)", error);
if (config.onerror) {
config.onerror(error);
}
};
function logConnected(ws, wsUri) {
try {
Logger.debug("WebSocket connected to " + wsUri);
2018-04-26 15:33:47 +02:00
}
catch (e) {
Logger.error(e);
}
}
2018-04-26 15:33:47 +02:00
var reconnectionOnClose = function () {
if (ws.readyState === CLOSED) {
if (closing) {
Logger.debug("Connection closed by user");
2018-04-26 15:33:47 +02:00
}
else {
Logger.debug("Connection closed unexpectecly. Reconnecting...");
reconnectToSameUri(MAX_RETRIES, 1);
}
2018-04-26 15:33:47 +02:00
}
else {
Logger.debug("Close callback from previous websocket. Ignoring it");
}
};
ws.onclose = reconnectionOnClose;
function reconnectToSameUri(maxRetries, numRetries) {
Logger.debug("reconnectToSameUri (attempt #" + numRetries + ", max=" + maxRetries + ")");
if (numRetries === 1) {
if (reconnecting) {
2018-04-26 15:33:47 +02:00
Logger.warn("Trying to reconnectToNewUri when reconnecting... Ignoring this reconnection.");
return;
2018-04-26 15:33:47 +02:00
}
else {
reconnecting = true;
}
if (config.onreconnecting) {
config.onreconnecting();
}
}
if (forcingDisconnection) {
reconnectToNewUri(maxRetries, numRetries, wsUri);
2018-04-26 15:33:47 +02:00
}
else {
if (config.newWsUriOnReconnection) {
2018-04-26 15:33:47 +02:00
config.newWsUriOnReconnection(function (error, newWsUri) {
if (error) {
Logger.debug(error);
2018-04-26 15:33:47 +02:00
setTimeout(function () {
reconnectToSameUri(maxRetries, numRetries + 1);
}, RETRY_TIME_MS);
2018-04-26 15:33:47 +02:00
}
else {
reconnectToNewUri(maxRetries, numRetries, newWsUri);
}
2018-04-26 15:33:47 +02:00
});
}
else {
reconnectToNewUri(maxRetries, numRetries, wsUri);
}
}
}
function reconnectToNewUri(maxRetries, numRetries, reconnectWsUri) {
Logger.debug("Reconnection attempt #" + numRetries);
ws.close();
wsUri = reconnectWsUri || wsUri;
var newWs;
if (useSockJS) {
newWs = new SockJS(wsUri);
2018-04-26 15:33:47 +02:00
}
else {
newWs = new WebSocket(wsUri);
}
2018-04-26 15:33:47 +02:00
newWs.onopen = function () {
Logger.debug("Reconnected after " + numRetries + " attempts...");
logConnected(newWs, wsUri);
reconnecting = false;
registerMessageHandler();
if (config.onreconnected()) {
config.onreconnected();
}
newWs.onclose = reconnectionOnClose;
};
2018-04-26 15:33:47 +02:00
var onErrorOrClose = function (error) {
Logger.warn("Reconnection error: ", error);
if (numRetries === maxRetries) {
if (config.ondisconnect) {
config.ondisconnect();
}
2018-04-26 15:33:47 +02:00
}
else {
setTimeout(function () {
reconnectToSameUri(maxRetries, numRetries + 1);
}, RETRY_TIME_MS);
}
};
newWs.onerror = onErrorOrClose;
ws = newWs;
}
2018-04-26 15:33:47 +02:00
this.close = function () {
closing = true;
ws.close();
};
2018-04-26 15:33:47 +02:00
this.forceClose = function (millis) {
Logger.debug("Testing: Force WebSocket close");
if (millis) {
Logger.debug("Testing: Change wsUri for " + millis + " millis to simulate net failure");
var goodWsUri = wsUri;
wsUri = "wss://21.234.12.34.4:443/";
forcingDisconnection = true;
2018-04-26 15:33:47 +02:00
setTimeout(function () {
Logger.debug("Testing: Recover good wsUri " + goodWsUri);
wsUri = goodWsUri;
forcingDisconnection = false;
}, millis);
}
ws.close();
};
2018-04-26 15:33:47 +02:00
this.reconnectWs = function () {
Logger.debug("reconnectWs");
2018-07-23 11:37:18 +02:00
reconnectToSameUri(MAX_RETRIES, 1);
};
2018-04-26 15:33:47 +02:00
this.send = function (message) {
ws.send(message);
};
2018-04-26 15:33:47 +02:00
this.addEventListener = function (type, callback) {
registerMessageHandler = function () {
ws.addEventListener(type, callback);
};
registerMessageHandler();
};
}
module.exports = WebSocketWithReconnection;
2018-04-26 15:33:47 +02:00
//# sourceMappingURL=webSocketWithReconnection.js.map