openvidu-browser: reconnection fix for unexpected ws close (HttpSession expiring)

pull/178/head
pabloFuente 2019-01-14 10:18:04 +01:00
parent 05dc6e21aa
commit cb8594373e
6 changed files with 83 additions and 66 deletions

View File

@ -59,6 +59,11 @@ export class Connection {
*/ */
disposed = false; disposed = false;
/**
* @hidden
*/
rpcSessionId: string;
/** /**
* @hidden * @hidden
*/ */

View File

@ -636,7 +636,7 @@ export class OpenVidu {
* @hidden * @hidden
*/ */
closeWs(): void { closeWs(): void {
this.jsonRpcClient.close(); this.jsonRpcClient.close(4102, "Connection closed by client");
} }
/** /**
@ -678,7 +678,7 @@ export class OpenVidu {
private disconnectCallback(): void { private disconnectCallback(): void {
console.warn('Websocket connection lost'); console.warn('Websocket connection lost');
if (this.isRoomAvailable()) { if (this.isRoomAvailable()) {
this.session.onLostConnection(); this.session.onLostConnection('networkDisconnect');
} else { } else {
alert('Connection error. Please reload page.'); alert('Connection error. Please reload page.');
} }
@ -686,9 +686,7 @@ export class OpenVidu {
private reconnectingCallback(): void { private reconnectingCallback(): void {
console.warn('Websocket connection lost (reconnecting)'); console.warn('Websocket connection lost (reconnecting)');
if (this.isRoomAvailable()) { if (!this.isRoomAvailable()) {
this.session.onLostConnection();
} else {
alert('Connection error. Please reload page.'); alert('Connection error. Please reload page.');
} }
} }
@ -696,7 +694,16 @@ export class OpenVidu {
private reconnectedCallback(): void { private reconnectedCallback(): void {
console.warn('Websocket reconnected'); console.warn('Websocket reconnected');
if (this.isRoomAvailable()) { if (this.isRoomAvailable()) {
this.sendRequest('connect', { sessionId: this.session.connection.rpcSessionId }, (error, response) => {
if (!!error) {
console.error(error);
this.session.onLostConnection("networkDisconnect");
this.jsonRpcClient.close(4101, "Reconnection fault");
} else {
this.jsonRpcClient.resetPing();
this.session.onRecoveredConnection(); this.session.onRecoveredConnection();
}
});
} else { } else {
alert('Connection error. Please reload page.'); alert('Connection error. Please reload page.');
} }

View File

@ -899,22 +899,10 @@ export class Session implements EventDispatcher {
/** /**
* @hidden * @hidden
*/ */
onLostConnection(): void { onLostConnection(reason: string): void {
console.warn('Lost connection in session ' + this.sessionId + ' waiting for reconnect');
/*if (!this.connection) {
console.warn('Not connected to session: if you are not debugging, this is probably a certificate error');
const url = 'https://' + this.openvidu.getWsUri().split('wss://')[1].split('/openvidu')[0];
if (window.confirm('If you are not debugging, this is probably a certificate error at \"' + url + '\"\n\nClick OK to navigate and accept it')) {
location.assign(url + '/accept-certificate');
}
return;
}*/
console.warn('Lost connection in Session ' + this.sessionId);
if (!!this.sessionId && !this.connection.disposed) { if (!!this.sessionId && !this.connection.disposed) {
this.leave(true, 'networkDisconnect'); this.leave(true, reason);
} }
} }
@ -923,7 +911,7 @@ export class Session implements EventDispatcher {
*/ */
onRecoveredConnection(): void { onRecoveredConnection(): void {
console.warn('Recovered connection in Session ' + this.sessionId); console.warn('Recovered connection in Session ' + this.sessionId);
this.ee.emitEvent('connectionRecovered', []); // this.ee.emitEvent('connectionRecovered', []);
} }
/** /**
@ -1048,6 +1036,7 @@ export class Session implements EventDispatcher {
this.connection.connectionId = response.id; this.connection.connectionId = response.id;
this.connection.creationTime = response.createdAt; this.connection.creationTime = response.createdAt;
this.connection.data = response.metadata; this.connection.data = response.metadata;
this.connection.rpcSessionId = response.sessionId;
// Initialize remote Connections with value returned by openvidu-server // Initialize remote Connections with value returned by openvidu-server
const events = { const events = {

View File

@ -39,7 +39,7 @@ var Logger = console;
* uri : URI to conntect to, * uri : URI to conntect to,
* useSockJS : true (use SockJS) / false (use WebSocket) by default, * useSockJS : true (use SockJS) / false (use WebSocket) by default,
* onconnected : callback method to invoke when connection is successful, * onconnected : callback method to invoke when connection is successful,
* ondisconnect : callback method to invoke when the connection is lost, * ondisconnect : callback method to invoke when the connection is lost (max retries for reconnecting reached),
* onreconnecting : callback method to invoke when the client is reconnecting, * onreconnecting : callback method to invoke when the client is reconnecting,
* onreconnected : callback method to invoke when the client successfully reconnects, * onreconnected : callback method to invoke when the client successfully reconnects,
* onerror : callback method to invoke when there is an error * onerror : callback method to invoke when there is an error
@ -82,6 +82,8 @@ function JsonRpcClient(configuration) {
return; return;
} }
stopPing();
status = RECONNECTING; status = RECONNECTING;
if (onreconnecting) { if (onreconnecting) {
onreconnecting(); onreconnecting();
@ -96,9 +98,7 @@ function JsonRpcClient(configuration) {
} }
status = CONNECTED; status = CONNECTED;
enabledPings = true;
updateNotReconnectIfLessThan(); updateNotReconnectIfLessThan();
usePing();
if (onreconnected) { if (onreconnected) {
onreconnected(); onreconnected();
@ -126,6 +126,8 @@ function JsonRpcClient(configuration) {
status = DISCONNECTED; status = DISCONNECTED;
stopPing();
if (onerror) { if (onerror) {
onerror(error); onerror(error);
} }
@ -239,8 +241,16 @@ function JsonRpcClient(configuration) {
} }
} }
this.close = function() { function stopPing() {
Logger.debug("Closing jsonRpcClient explicitly by client"); clearInterval(pingInterval);
pingPongStarted = false;
enabledPings = false;
pingNextNum = -1;
rpc.cancel();
}
this.close = function (code, reason) {
Logger.debug("Closing with code: " + code + " because: " + reason);
if (pingInterval != undefined) { if (pingInterval != undefined) {
Logger.debug("Clearing ping interval"); Logger.debug("Clearing ping interval");
@ -255,10 +265,10 @@ function JsonRpcClient(configuration) {
if (error) { if (error) {
Logger.error("Error sending close message: " + JSON.stringify(error)); Logger.error("Error sending close message: " + JSON.stringify(error));
} }
ws.close(); ws.close(code, reason);
}); });
} else { } else {
ws.close(); ws.close(code, reason);
} }
} }
@ -270,6 +280,12 @@ function JsonRpcClient(configuration) {
this.reconnect = function () { this.reconnect = function () {
ws.reconnectWs(); ws.reconnectWs();
} }
this.resetPing = function () {
enabledPings = true;
pingNextNum = 0;
usePing();
}
} }

View File

@ -48,7 +48,7 @@ config = {
uri : wsUri, uri : wsUri,
useSockJS : true (use SockJS) / false (use WebSocket) by default, useSockJS : true (use SockJS) / false (use WebSocket) by default,
onconnected : callback method to invoke when connection is successful, onconnected : callback method to invoke when connection is successful,
ondisconnect : callback method to invoke when the connection is lost, ondisconnect : callback method to invoke when the connection is lost (max retries for reconnecting reached),
onreconnecting : callback method to invoke when the client is reconnecting, onreconnecting : callback method to invoke when the client is reconnecting,
onreconnected : callback method to invoke when the client successfully reconnects, onreconnected : callback method to invoke when the client successfully reconnects,
}; };

View File

@ -519,7 +519,7 @@ function RpcBuilder(packer, options, transport, onRequest)
// Prevent to receive new messages // Prevent to receive new messages
var transport = this.getTransport(); var transport = this.getTransport();
if(transport && transport.close) if(transport && transport.close)
transport.close(); transport.close(4003, "Cancel request");
// Request & processed responses // Request & processed responses
this.cancel(); this.cancel();