Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions electron_app/src/reachabilityTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { SERVER_URL } = require('./utils/const');
const globalManager = require('./globalManager');
const mailboxWindow = require('./windows/mailbox');
const { processEventsQueue } = require('./eventQueueManager');
const { restartSocketSameJWT } = require('./socketClient');
const reconnectDelay = 2000;
const NETWORK_STATUS = {
ONLINE: 'online',
Expand All @@ -12,6 +13,7 @@ const normalPingDelayMs = 15000;
const failedPingDelayMs = 5000;
let pingFailedCounter = 0;
let reachabilityTask = null;
let hasFailed = false;
let checkingConnectionToServer = false;

const setConnectionStatus = networkStatus => {
Expand Down Expand Up @@ -57,7 +59,12 @@ const checkAlive = async force => {
if (prevNetworkStatus !== NETWORK_STATUS.ONLINE) {
setConnectionStatus(NETWORK_STATUS.ONLINE);
}
if (hasFailed) {
hasFailed = false;
restartSocketSameJWT();
}
} catch (ex) {
hasFailed = true;
pingFailedCounter++;
delayTime = failedPingDelayMs;
if (pingFailedCounter > 3 && prevNetworkStatus !== NETWORK_STATUS.OFFLINE) {
Expand Down
24 changes: 17 additions & 7 deletions electron_app/src/socketClient.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { client: WebSocketClient } = require('websocket');
const { SOCKET_URL } = require('./utils/const');
let client, reconnect, messageListener, socketConnection;
let client, reconnect, messageListener, socketConnection, lastJWT;
let shouldReconnect = true;
const reconnectDelay = 2000;

Expand All @@ -21,11 +21,9 @@ const disconnect = () => {
const start = ({ jwt }) => {
client = new WebSocketClient();
client.connect(`${SOCKET_URL}?token=${jwt}`, 'criptext-protocol');

lastJWT = jwt;
client.on('connectFailed', error => {
if (shouldReconnect) {
reconnect();
}
reconnect();
log(error);
});

Expand All @@ -34,7 +32,7 @@ const start = ({ jwt }) => {
log('Socket connection opened');

connection.on('error', error => {
reconnect();
restartSocketSameJWT();
log(error);
});
connection.on('close', () => {
Expand All @@ -44,6 +42,11 @@ const start = ({ jwt }) => {
const message = JSON.parse(data.utf8Data);
messageListener(message);
});
connection.socket.setTimeout(30 * 1000);
connection.socket.on('timeout', function() {
log('Socket timeout');
restartSocketSameJWT();
});
});

reconnect = () => {
Expand All @@ -67,6 +70,7 @@ process.on('exit', () => {
});

const restartSocket = ({ jwt }) => {
lastJWT = jwt;
shouldReconnect = false;
disconnect();
client = null;
Expand All @@ -76,9 +80,15 @@ const restartSocket = ({ jwt }) => {
}, reconnectDelay * 2);
};

const restartSocketSameJWT = () => {
if (!shouldReconnect) return;
restartSocket({ jwt: lastJWT });
};

module.exports = {
start,
setMessageListener,
disconnect,
restartSocket
restartSocket,
restartSocketSameJWT
};