forked from Ivasoft/mattermost-mobile
Fixes WS reconnection (#6009)
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||||
// See LICENSE.txt for license information.
|
// See LICENSE.txt for license information.
|
||||||
|
|
||||||
import {getOrCreateWebSocketClient, WebSocketClientInterface} from '@mattermost/react-native-network-client';
|
import {ClientHeaders, getOrCreateWebSocketClient, WebSocketClientInterface, WebSocketReadyState} from '@mattermost/react-native-network-client';
|
||||||
|
import {Platform} from 'react-native';
|
||||||
|
|
||||||
import {WebsocketEvents} from '@constants';
|
import {WebsocketEvents} from '@constants';
|
||||||
import DatabaseManager from '@database/manager';
|
import DatabaseManager from '@database/manager';
|
||||||
@@ -37,6 +38,7 @@ export default class WebSocketClient {
|
|||||||
private stop: boolean;
|
private stop: boolean;
|
||||||
private lastConnect: number;
|
private lastConnect: number;
|
||||||
private lastDisconnect: number;
|
private lastDisconnect: number;
|
||||||
|
private url = '';
|
||||||
|
|
||||||
private serverUrl: string;
|
private serverUrl: string;
|
||||||
|
|
||||||
@@ -63,7 +65,7 @@ export default class WebSocketClient {
|
|||||||
this.stop = false;
|
this.stop = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.conn) {
|
if (this.conn && this.conn.readyState !== WebSocketReadyState.CLOSED) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,30 +94,36 @@ export default class WebSocketClient {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let url = connectionUrl;
|
this.url = connectionUrl;
|
||||||
|
|
||||||
const reliableWebSockets = system.config.EnableReliableWebSockets === 'true';
|
const reliableWebSockets = system.config.EnableReliableWebSockets === 'true';
|
||||||
if (reliableWebSockets) {
|
if (reliableWebSockets) {
|
||||||
// Add connection id, and last_sequence_number to the query param.
|
// Add connection id, and last_sequence_number to the query param.
|
||||||
// We cannot also send it as part of the auth_challenge, because the session cookie is already sent with the request.
|
// We cannot also send it as part of the auth_challenge, because the session cookie is already sent with the request.
|
||||||
url = `${connectionUrl}?connection_id=${this.connectionId}&sequence_number=${this.serverSequence}`;
|
this.url = `${connectionUrl}?connection_id=${this.connectionId}&sequence_number=${this.serverSequence}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Manually changing protocol since getOrCreateWebsocketClient does not accept http/s
|
// Manually changing protocol since getOrCreateWebsocketClient does not accept http/s
|
||||||
if (url.startsWith('https:')) {
|
if (this.url.startsWith('https:')) {
|
||||||
url = 'wss:' + url.substr('https:'.length);
|
this.url = 'wss:' + this.url.substr('https:'.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (url.startsWith('http:')) {
|
if (this.url.startsWith('http:')) {
|
||||||
url = 'ws:' + url.substr('http:'.length);
|
this.url = 'ws:' + this.url.substr('http:'.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.connectFailCount === 0) {
|
if (this.connectFailCount === 0) {
|
||||||
console.log('websocket connecting to ' + url); //eslint-disable-line no-console
|
console.log('websocket connecting to ' + this.url); //eslint-disable-line no-console
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const {client} = await getOrCreateWebSocketClient(url, {headers: {origin}});
|
const headers: ClientHeaders = {origin};
|
||||||
|
if (Platform.OS === 'android') {
|
||||||
|
// Required to properly handled the reliableWebsocket reconnection
|
||||||
|
// iOS is using he underlying cookieJar
|
||||||
|
headers.Authorization = `Bearer ${this.token}`;
|
||||||
|
}
|
||||||
|
const {client} = await getOrCreateWebSocketClient(this.url, {headers});
|
||||||
this.conn = client;
|
this.conn = client;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return;
|
return;
|
||||||
@@ -136,14 +144,14 @@ export default class WebSocketClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this.connectFailCount > 0) {
|
if (this.connectFailCount > 0) {
|
||||||
console.log('websocket re-established connection to', url); //eslint-disable-line no-console
|
console.log('websocket re-established connection to', this.url); //eslint-disable-line no-console
|
||||||
if (!reliableWebSockets && this.reconnectCallback) {
|
if (!reliableWebSockets && this.reconnectCallback) {
|
||||||
this.reconnectCallback();
|
this.reconnectCallback();
|
||||||
} else if (reliableWebSockets && this.serverSequence && this.missedEventsCallback) {
|
} else if (reliableWebSockets && this.serverSequence && this.missedEventsCallback) {
|
||||||
this.missedEventsCallback();
|
this.missedEventsCallback();
|
||||||
}
|
}
|
||||||
} else if (this.firstConnectCallback) {
|
} else if (this.firstConnectCallback) {
|
||||||
console.log('websocket connected to', url); //eslint-disable-line no-console
|
console.log('websocket connected to', this.url); //eslint-disable-line no-console
|
||||||
this.firstConnectCallback();
|
this.firstConnectCallback();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,7 +168,7 @@ export default class WebSocketClient {
|
|||||||
this.responseSequence = 1;
|
this.responseSequence = 1;
|
||||||
|
|
||||||
if (this.connectFailCount === 0) {
|
if (this.connectFailCount === 0) {
|
||||||
console.log('websocket closed', url); //eslint-disable-line no-console
|
console.log('websocket closed', this.url); //eslint-disable-line no-console
|
||||||
}
|
}
|
||||||
|
|
||||||
this.connectFailCount++;
|
this.connectFailCount++;
|
||||||
@@ -200,13 +208,15 @@ export default class WebSocketClient {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.conn!.onError((evt: any) => {
|
this.conn!.onError((evt: any) => {
|
||||||
if (this.connectFailCount <= 1) {
|
if (evt.url === this.url) {
|
||||||
console.log('websocket error', url); //eslint-disable-line no-console
|
if (this.connectFailCount <= 1) {
|
||||||
console.log(evt); //eslint-disable-line no-console
|
console.log('websocket error', this.url); //eslint-disable-line no-console
|
||||||
}
|
console.log('WEBSOCKET ERROR EVENT', evt); //eslint-disable-line no-console
|
||||||
|
}
|
||||||
|
|
||||||
if (this.errorCallback) {
|
if (this.errorCallback) {
|
||||||
this.errorCallback(evt);
|
this.errorCallback(evt);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -225,14 +235,14 @@ export default class WebSocketClient {
|
|||||||
// We check the hello packet, which is always the first packet in a stream.
|
// We check the hello packet, which is always the first packet in a stream.
|
||||||
if (msg.event === WebsocketEvents.HELLO && this.reconnectCallback) {
|
if (msg.event === WebsocketEvents.HELLO && this.reconnectCallback) {
|
||||||
//eslint-disable-next-line no-console
|
//eslint-disable-next-line no-console
|
||||||
console.log(url, 'got connection id ', msg.data.connection_id);
|
console.log(this.url, 'got connection id ', msg.data.connection_id);
|
||||||
|
|
||||||
// If we already have a connectionId present, and server sends a different one,
|
// If we already have a connectionId present, and server sends a different one,
|
||||||
// that means it's either a long timeout, or server restart, or sequence number is not found.
|
// that means it's either a long timeout, or server restart, or sequence number is not found.
|
||||||
// Then we do the sync calls, and reset sequence number to 0.
|
// Then we do the sync calls, and reset sequence number to 0.
|
||||||
if (this.connectionId !== '' && this.connectionId !== msg.data.connection_id) {
|
if (this.connectionId !== '' && this.connectionId !== msg.data.connection_id) {
|
||||||
//eslint-disable-next-line no-console
|
//eslint-disable-next-line no-console
|
||||||
console.log(url, 'long timeout, or server restart, or sequence number is not found.');
|
console.log(this.url, 'long timeout, or server restart, or sequence number is not found.');
|
||||||
this.reconnectCallback();
|
this.reconnectCallback();
|
||||||
this.serverSequence = 0;
|
this.serverSequence = 0;
|
||||||
}
|
}
|
||||||
@@ -246,7 +256,7 @@ export default class WebSocketClient {
|
|||||||
// we just disconnect and reconnect.
|
// we just disconnect and reconnect.
|
||||||
if (msg.seq !== this.serverSequence) {
|
if (msg.seq !== this.serverSequence) {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.log(url, 'missed websocket event, act_seq=' + msg.seq + ' exp_seq=' + this.serverSequence);
|
console.log(this.url, 'missed websocket event, act_seq=' + msg.seq + ' exp_seq=' + this.serverSequence);
|
||||||
|
|
||||||
// We are not calling this.close() because we need to auto-restart.
|
// We are not calling this.close() because we need to auto-restart.
|
||||||
this.connectFailCount = 0;
|
this.connectFailCount = 0;
|
||||||
@@ -256,7 +266,7 @@ export default class WebSocketClient {
|
|||||||
}
|
}
|
||||||
} else if (msg.seq !== this.serverSequence && this.reconnectCallback) {
|
} else if (msg.seq !== this.serverSequence && this.reconnectCallback) {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.log(url, 'missed websocket event, act_seq=' + msg.seq + ' exp_seq=' + this.serverSequence);
|
console.log(this.url, 'missed websocket event, act_seq=' + msg.seq + ' exp_seq=' + this.serverSequence);
|
||||||
this.reconnectCallback();
|
this.reconnectCallback();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -301,7 +311,7 @@ export default class WebSocketClient {
|
|||||||
this.connectFailCount = 0;
|
this.connectFailCount = 0;
|
||||||
this.responseSequence = 1;
|
this.responseSequence = 1;
|
||||||
|
|
||||||
if (this.conn && this.conn.readyState === WebSocket.OPEN) {
|
if (this.conn && this.conn.readyState === WebSocketReadyState.OPEN) {
|
||||||
this.conn.close();
|
this.conn.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -318,9 +328,9 @@ export default class WebSocketClient {
|
|||||||
data,
|
data,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (this.conn && this.conn.readyState === WebSocket.OPEN) {
|
if (this.conn && this.conn.readyState === WebSocketReadyState.OPEN) {
|
||||||
this.conn.send(JSON.stringify(msg));
|
this.conn.send(JSON.stringify(msg));
|
||||||
} else if (!this.conn || this.conn.readyState === WebSocket.CLOSED) {
|
} else if (!this.conn || this.conn.readyState === WebSocketReadyState.CLOSED) {
|
||||||
this.conn = undefined;
|
this.conn = undefined;
|
||||||
this.initialize(this.token);
|
this.initialize(this.token);
|
||||||
}
|
}
|
||||||
@@ -334,6 +344,6 @@ export default class WebSocketClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public isConnected(): boolean {
|
public isConnected(): boolean {
|
||||||
return this.conn?.readyState === WebSocket.OPEN; //|| (!this.stop && this.connectFailCount <= 2);
|
return this.conn?.readyState === WebSocketReadyState.OPEN; //|| (!this.stop && this.connectFailCount <= 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ class WebsocketManager {
|
|||||||
private netConnected = false;
|
private netConnected = false;
|
||||||
private previousAppState: AppStateStatus;
|
private previousAppState: AppStateStatus;
|
||||||
private statusUpdatesIntervalIDs: Record<string, NodeJS.Timer> = {};
|
private statusUpdatesIntervalIDs: Record<string, NodeJS.Timer> = {};
|
||||||
|
private backgroundIntervalId: number | undefined;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.previousAppState = AppState.currentState;
|
this.previousAppState = AppState.currentState;
|
||||||
@@ -186,20 +187,23 @@ class WebsocketManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.cancelAllConnections();
|
this.cancelAllConnections();
|
||||||
if (appState === 'background' && !this.isBackgroundTimerRunning) {
|
if (appState !== 'active' && !this.isBackgroundTimerRunning) {
|
||||||
this.isBackgroundTimerRunning = true;
|
this.isBackgroundTimerRunning = true;
|
||||||
this.cancelAllConnections();
|
this.cancelAllConnections();
|
||||||
BackgroundTimer.runBackgroundTimer(() => {
|
this.backgroundIntervalId = BackgroundTimer.setInterval(() => {
|
||||||
this.closeAll();
|
this.closeAll();
|
||||||
BackgroundTimer.stopBackgroundTimer();
|
BackgroundTimer.clearInterval(this.backgroundIntervalId!);
|
||||||
this.isBackgroundTimerRunning = false;
|
this.isBackgroundTimerRunning = false;
|
||||||
}, WAIT_TO_CLOSE);
|
}, WAIT_TO_CLOSE);
|
||||||
|
|
||||||
this.previousAppState = appState;
|
this.previousAppState = appState;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (appState === 'active' && this.netConnected) { // Reopen the websockets only if there is connection
|
if (appState === 'active' && this.netConnected) { // Reopen the websockets only if there is connection
|
||||||
BackgroundTimer.stopBackgroundTimer();
|
if (this.backgroundIntervalId) {
|
||||||
|
BackgroundTimer.clearInterval(this.backgroundIntervalId);
|
||||||
|
}
|
||||||
this.isBackgroundTimerRunning = false;
|
this.isBackgroundTimerRunning = false;
|
||||||
this.openAll();
|
this.openAll();
|
||||||
this.previousAppState = appState;
|
this.previousAppState = appState;
|
||||||
|
|||||||
Reference in New Issue
Block a user