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;
/**
* @hidden
*/
rpcSessionId: string;
/**
* @hidden
*/

View File

@ -636,7 +636,7 @@ export class OpenVidu {
* @hidden
*/
closeWs(): void {
this.jsonRpcClient.close();
this.jsonRpcClient.close(4102, "Connection closed by client");
}
/**
@ -678,7 +678,7 @@ export class OpenVidu {
private disconnectCallback(): void {
console.warn('Websocket connection lost');
if (this.isRoomAvailable()) {
this.session.onLostConnection();
this.session.onLostConnection('networkDisconnect');
} else {
alert('Connection error. Please reload page.');
}
@ -686,9 +686,7 @@ export class OpenVidu {
private reconnectingCallback(): void {
console.warn('Websocket connection lost (reconnecting)');
if (this.isRoomAvailable()) {
this.session.onLostConnection();
} else {
if (!this.isRoomAvailable()) {
alert('Connection error. Please reload page.');
}
}
@ -696,7 +694,16 @@ export class OpenVidu {
private reconnectedCallback(): void {
console.warn('Websocket reconnected');
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();
}
});
} else {
alert('Connection error. Please reload page.');
}

View File

@ -899,22 +899,10 @@ export class Session implements EventDispatcher {
/**
* @hidden
*/
onLostConnection(): void {
/*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);
onLostConnection(reason: string): void {
console.warn('Lost connection in session ' + this.sessionId + ' waiting for reconnect');
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 {
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.creationTime = response.createdAt;
this.connection.data = response.metadata;
this.connection.rpcSessionId = response.sessionId;
// Initialize remote Connections with value returned by openvidu-server
const events = {

View File

@ -39,7 +39,7 @@ var Logger = console;
* uri : URI to conntect to,
* useSockJS : true (use SockJS) / false (use WebSocket) by default,
* 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,
* onreconnected : callback method to invoke when the client successfully reconnects,
* onerror : callback method to invoke when there is an error
@ -82,6 +82,8 @@ function JsonRpcClient(configuration) {
return;
}
stopPing();
status = RECONNECTING;
if (onreconnecting) {
onreconnecting();
@ -96,9 +98,7 @@ function JsonRpcClient(configuration) {
}
status = CONNECTED;
enabledPings = true;
updateNotReconnectIfLessThan();
usePing();
if (onreconnected) {
onreconnected();
@ -126,6 +126,8 @@ function JsonRpcClient(configuration) {
status = DISCONNECTED;
stopPing();
if (onerror) {
onerror(error);
}
@ -239,8 +241,16 @@ function JsonRpcClient(configuration) {
}
}
this.close = function() {
Logger.debug("Closing jsonRpcClient explicitly by client");
function stopPing() {
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) {
Logger.debug("Clearing ping interval");
@ -255,10 +265,10 @@ function JsonRpcClient(configuration) {
if (error) {
Logger.error("Error sending close message: " + JSON.stringify(error));
}
ws.close();
ws.close(code, reason);
});
} else {
ws.close();
ws.close(code, reason);
}
}
@ -270,6 +280,12 @@ function JsonRpcClient(configuration) {
this.reconnect = function () {
ws.reconnectWs();
}
this.resetPing = function () {
enabledPings = true;
pingNextNum = 0;
usePing();
}
}

View File

@ -48,7 +48,7 @@ config = {
uri : wsUri,
useSockJS : true (use SockJS) / false (use WebSocket) by default,
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,
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
var transport = this.getTransport();
if(transport && transport.close)
transport.close();
transport.close(4003, "Cancel request");
// Request & processed responses
this.cancel();