Skip to content

Commit f906e73

Browse files
author
Erika Perugachi
authored
Merge pull request #554 from JulianAdams4/fixes2
Websocket: Detect lost connection (Banner)
2 parents 15905be + 90d830a commit f906e73

File tree

6 files changed

+38
-16
lines changed

6 files changed

+38
-16
lines changed

electron_app/src/socketClient.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const SOCKET_URL =
44
process.env.NODE_ENV === 'development' ? DEV_SOCKET_URL : PROD_SOCKET_URL;
55
let client, reconnect, messageListener, socketConnection;
66
const reconnectDelay = 2000;
7+
const mailboxWindow = require('./windows/mailbox');
78
const globalManager = require('./globalManager');
89
const NETWORK_STATUS = {
910
ONLINE: 'online',
@@ -78,13 +79,20 @@ const handleError = (error, errorMessage) => {
7879
};
7980

8081
const setConnectionStatus = networkStatus => {
82+
const prevNetworkStatus = globalManager.internetConnection.getStatus();
8183
switch (networkStatus) {
8284
case NETWORK_STATUS.ONLINE: {
83-
globalManager.internetConnection.setStatus(true);
85+
if (prevNetworkStatus !== true) {
86+
globalManager.internetConnection.setStatus(true);
87+
mailboxWindow.send('network-connection-established', null);
88+
}
8489
break;
8590
}
8691
case NETWORK_STATUS.OFFLINE: {
87-
globalManager.internetConnection.setStatus(false);
92+
if (prevNetworkStatus !== false) {
93+
globalManager.internetConnection.setStatus(false);
94+
mailboxWindow.send('lost-network-connection', null);
95+
}
8896
break;
8997
}
9098
default:
@@ -100,6 +108,7 @@ const initPingParams = () => {
100108
const checkAlive = () => {
101109
if (shouldSendPing === undefined || shouldSendPing === '1') {
102110
shouldSendPing = 0;
111+
setConnectionStatus(NETWORK_STATUS.ONLINE);
103112
} else {
104113
setConnectionStatus(NETWORK_STATUS.OFFLINE);
105114
log('Error: Lost Connection. Check internet');

email_mailbox/src/components/MessageWrapper.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { Component } from 'react';
22
import PropTypes from 'prop-types';
33
import Message from './Message';
44
import { Event, addEvent, removeEvent } from '../utils/electronEventInterface';
5+
import { messagePriorities } from '../data/message';
56

67
const MESSAGE_DURATION = 5000;
78
const QUESTION_DURATION = 5 * 60 * 1000;
@@ -106,7 +107,10 @@ class MessageWrapper extends Component {
106107
displayMessage: true
107108
};
108109
this.setState(newState, () => {
109-
const duration = ask ? QUESTION_DURATION : MESSAGE_DURATION;
110+
const isAskOrNetworkError = ask || priority === messagePriorities.HIGH;
111+
const duration = isAskOrNetworkError
112+
? QUESTION_DURATION
113+
: MESSAGE_DURATION;
110114
this.hideMessageTimeout = setTimeout(() => {
111115
this.hideMessage();
112116
}, duration);

email_mailbox/src/data/message.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ const actionHandlerKeys = {
1515
},
1616
success: {
1717
emailSent: 'view-message'
18-
},
19-
error: {
20-
network: 'try-reconnect'
2118
}
2219
};
2320

@@ -112,10 +109,8 @@ const messagesContent = {
112109
description: string.messages.fetchEmails.description
113110
},
114111
network: {
115-
priority: messagePriorities.TOP,
116-
description: string.messages.network.description,
117-
action: string.messages.network.action,
118-
actionHandlerKey: actionHandlerKeys.error.network
112+
priority: messagePriorities.HIGH,
113+
description: string.messages.network.description
119114
},
120115
recoveryEmailChanged: {
121116
priority: messagePriorities.MEDIUM,
@@ -171,4 +166,4 @@ const messagesContent = {
171166
}
172167
};
173168

174-
export { messagesContent as default, actionHandlerKeys };
169+
export { messagesContent as default, actionHandlerKeys, messagePriorities };

email_mailbox/src/lang/en.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ export default {
114114
description: 'Connection reestablished'
115115
},
116116
network: {
117-
description: 'Not connected, conecting in 10s',
118-
action: 'Try Now'
117+
description: 'Not connected. Trying to reconnect'
119118
},
120119
new_device: {
121120
ask: 'Are you trying to access from'

email_mailbox/src/lang/es.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,10 @@ export default {
112112
'Falló al pedir emails. Revisa tu conexión e intenta de nuevo'
113113
},
114114
internet: {
115-
description: 'Conexión restalecida'
115+
description: 'Conexión restablecida'
116116
},
117117
network: {
118-
description: 'Sin conexión, conectando en 10s',
119-
action: 'Intentar ahora'
118+
description: 'Sin conexión. Intentando reconectar'
120119
},
121120
new_device: {
122121
ask: 'Estás tratando de acceder desde'

email_mailbox/src/utils/electronEventInterface.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,22 @@ ipcRenderer.on(
771771
}
772772
);
773773

774+
ipcRenderer.on('network-connection-established', () => {
775+
const messageData = {
776+
...Messages.establish.internet,
777+
type: MessageType.ESTABLISH
778+
};
779+
emitter.emit(Event.DISPLAY_MESSAGE, messageData);
780+
});
781+
782+
ipcRenderer.on('lost-network-connection', () => {
783+
const messageData = {
784+
...Messages.error.network,
785+
type: MessageType.ERROR
786+
};
787+
emitter.emit(Event.DISPLAY_MESSAGE, messageData);
788+
});
789+
774790
/* Window events
775791
----------------------------- */
776792
export const sendOpenEventErrorMessage = () => {

0 commit comments

Comments
 (0)