From 8eae2e0eab2325dd715f962a54c50c462632ca48 Mon Sep 17 00:00:00 2001 From: pabloFuente Date: Fri, 10 Sep 2021 14:41:30 +0200 Subject: [PATCH] openvidu-browser: OpenVidu.life logic --- openvidu-browser/src/OpenVidu/OpenVidu.ts | 38 +++++++++++++++++-- openvidu-browser/src/OpenVidu/Session.ts | 3 ++ .../Private/LocalConnectionOptions.ts | 1 + .../transports/webSocketWithReconnection.js | 4 ++ 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/openvidu-browser/src/OpenVidu/OpenVidu.ts b/openvidu-browser/src/OpenVidu/OpenVidu.ts index 56e29e42..ac68b5fc 100644 --- a/openvidu-browser/src/OpenVidu/OpenVidu.ts +++ b/openvidu-browser/src/OpenVidu/OpenVidu.ts @@ -109,6 +109,10 @@ export class OpenVidu { * @hidden */ mediaServer: string; + /** + * @hidden + */ + life: number = -1; /** * @hidden */ @@ -1029,15 +1033,43 @@ export class OpenVidu { logger.warn('Websocket reconnected'); if (this.isRoomAvailable()) { if (!!this.session.connection) { + // This RPC method allows checking if the WebSocket reconnected to a session where + // the user is still a participant, or the session evicted the user this.sendRequest('connect', { sessionId: this.session.connection.rpcSessionId }, (error, response) => { if (!!error) { + if (this.isMasterNodeCrashed()) { + logger.warn('Master Node has crashed!'); + } else { + logger.error(error); - logger.warn('Websocket was able to reconnect to OpenVidu Server, but your Connection was already destroyed due to timeout. You are no longer a participant of the Session and your media streams have been destroyed'); - this.session.onLostConnection("networkDisconnect"); - this.jsonRpcClient.close(4101, "Reconnection fault"); + + const notifyNetworkDisconnection = (error) => { + logger.warn('Websocket was able to reconnect to OpenVidu Server, but your Connection was already destroyed due to timeout. You are no longer a participant of the Session and your media streams have been destroyed'); + this.session.onLostConnection("networkDisconnect"); + this.jsonRpcClient.close(4101, "Reconnection fault"); + } + + if (this.life === -1) { + notifyNetworkDisconnection(error); + } else { + // This RPC method is only required to find out the reason of the disconnection: + // whether the client lost its network connection or a Master Node crashed + this.sendRequest('sessionStatus', { sessionId: this.session.sessionId }, (error, response) => { + if (this.life === response.life) { + // If the life stored in the client matches the life stored in the server, it means that the client lost its network connection + notifyNetworkDisconnection(error); + } else { + // If the life stored in the client is below the life stored in the server, it means that the Master Node has crashed + logger.warn('Websocket was able to reconnect to OpenVidu Server, but your Master Node crashed.'); + this.session.onLostConnection("nodeCrashed"); + this.jsonRpcClient.close(4101, "Reconnection fault"); + } + }); + } + } } else { this.jsonRpcClient.resetPing(); diff --git a/openvidu-browser/src/OpenVidu/Session.ts b/openvidu-browser/src/OpenVidu/Session.ts index e95c1dfe..05a317ed 100644 --- a/openvidu-browser/src/OpenVidu/Session.ts +++ b/openvidu-browser/src/OpenVidu/Session.ts @@ -1544,6 +1544,9 @@ export class Session extends EventDispatcher { forceDisconnect: this.openvidu.role === 'MODERATOR' }; logger.info("openvidu-server version: " + opts.version); + if (opts.life != null) { + this.openvidu.life = opts.life; + } if (opts.version !== this.openvidu.libraryVersion) { logger.warn('OpenVidu Server (' + opts.version + ') and OpenVidu Browser (' + this.openvidu.libraryVersion + diff --git a/openvidu-browser/src/OpenViduInternal/Interfaces/Private/LocalConnectionOptions.ts b/openvidu-browser/src/OpenViduInternal/Interfaces/Private/LocalConnectionOptions.ts index 58721ef0..5cfbe203 100644 --- a/openvidu-browser/src/OpenViduInternal/Interfaces/Private/LocalConnectionOptions.ts +++ b/openvidu-browser/src/OpenViduInternal/Interfaces/Private/LocalConnectionOptions.ts @@ -32,4 +32,5 @@ export interface LocalConnectionOptions { turnCredential: string; version: string; mediaServer: string; + life: number; } \ No newline at end of file diff --git a/openvidu-browser/src/OpenViduInternal/KurentoUtils/kurento-jsonrpc/clients/transports/webSocketWithReconnection.js b/openvidu-browser/src/OpenViduInternal/KurentoUtils/kurento-jsonrpc/clients/transports/webSocketWithReconnection.js index 4850077f..d8ff4ca4 100644 --- a/openvidu-browser/src/OpenViduInternal/KurentoUtils/kurento-jsonrpc/clients/transports/webSocketWithReconnection.js +++ b/openvidu-browser/src/OpenViduInternal/KurentoUtils/kurento-jsonrpc/clients/transports/webSocketWithReconnection.js @@ -136,6 +136,10 @@ function WebSocketWithReconnection(config) { ws.close(); }; + this.forceClose = () => { + ws.close(); + }; + this.reconnectWs = () => { Logger.debug("reconnectWs"); reconnect(MAX_RETRIES, 1);