11const { client : WebSocketClient } = require ( 'websocket' ) ;
22const { SOCKET_URL } = require ( './utils/const' ) ;
3- const { percentRegex } = require ( './utils/RegexUtils' ) ;
4- const globalManager = require ( './globalManager' ) ;
5- const mailboxWindow = require ( './windows/mailbox' ) ;
6- const { processEventsQueue } = require ( './eventQueueManager' ) ;
73let client , reconnect , messageListener , socketConnection ;
84let shouldReconnect = true ;
95const reconnectDelay = 2000 ;
10- const NETWORK_STATUS = {
11- ONLINE : 'online' ,
12- OFFLINE : 'offline'
13- } ;
14- const exec = require ( 'child_process' ) . exec ;
15- const normalPingDelayMs = 15000 ;
16- const failedPingDelayMs = 5000 ;
17- let pingFailedCounter = 0 ;
18- let checkConnTimeout = null ;
19- let checkDelay = normalPingDelayMs ;
206
217const setMessageListener = mListener => ( messageListener = mListener ) ;
228
239const disconnect = ( ) => {
2410 if ( ! socketConnection ) return ;
2511 try {
26- clearInterval ( checkConnTimeout ) ;
2712 socketConnection . on ( 'close' , ( ) => {
2813 socketConnection = undefined ;
2914 } ) ;
@@ -35,29 +20,25 @@ const disconnect = () => {
3520
3621const start = ( { jwt } ) => {
3722 client = new WebSocketClient ( ) ;
38- pingFailedCounter = 0 ;
39- checkDelay = normalPingDelayMs ;
4023 client . connect (
4124 `${ SOCKET_URL } ?token=${ jwt } ` ,
4225 'criptext-protocol'
4326 ) ;
4427
4528 client . on ( 'connectFailed' , error => {
46- handleError ( error , 'Failed to connect' ) ;
4729 if ( shouldReconnect ) {
4830 reconnect ( ) ;
4931 }
32+ log ( error ) ;
5033 } ) ;
5134
5235 client . on ( 'connect' , connection => {
5336 socketConnection = connection ;
54- setConnectionStatus ( NETWORK_STATUS . ONLINE ) ;
5537 log ( 'Socket connection opened' ) ;
56- checkAlive ( ) ;
5738
5839 connection . on ( 'error' , error => {
59- handleError ( error , 'Connection Error' ) ;
6040 reconnect ( ) ;
41+ log ( error ) ;
6142 } ) ;
6243 connection . on ( 'close' , ( ) => {
6344 log ( 'Socket connection closed' ) ;
@@ -84,86 +65,8 @@ const log = message => {
8465 }
8566} ;
8667
87- const handleError = ( error , errorMessage ) => {
88- if ( error . code === 'ENOTFOUND' ) {
89- setConnectionStatus ( NETWORK_STATUS . OFFLINE ) ;
90- }
91- log ( `${ errorMessage || 'Error' } : ${ error . toString ( ) } ` ) ;
92- } ;
93-
94- const setConnectionStatus = networkStatus => {
95- const prevNetworkStatus = globalManager . internetConnection . getStatus ( ) ;
96- switch ( networkStatus ) {
97- case NETWORK_STATUS . ONLINE : {
98- if ( prevNetworkStatus === true ) return ;
99- if ( prevNetworkStatus === false ) {
100- mailboxWindow . send ( 'network-connection-established' ) ;
101- }
102- globalManager . internetConnection . setStatus ( true ) ;
103- processEventsQueue ( ) ;
104- break ;
105- }
106- case NETWORK_STATUS . OFFLINE : {
107- if ( prevNetworkStatus === false ) return ;
108- setTimeout ( ( ) => {
109- mailboxWindow . send ( 'lost-network-connection' ) ;
110- } , reconnectDelay ) ;
111- globalManager . internetConnection . setStatus ( false ) ;
112- break ;
113- }
114- default :
115- break ;
116- }
117- } ;
118-
119- /* Check alive
120- ----------------------*/
121- const isWindows = process . platform === 'win32' ;
122-
123- const getDelay = ( ) => checkDelay ;
124-
125- const checkAlive = ( ) => {
126- checkConnTimeout = setInterval ( ( ) => {
127- exec (
128- `ping -${ isWindows ? 'n' : 'c' } 1 www.criptext.com` ,
129- { encoding : 'utf8' , windowsHide : true } ,
130- ( err , stdout , stderr ) => {
131- let totalLost = 0 ;
132- if ( stdout ) {
133- const stdoutString = stdout . toString ( ) ;
134- if ( stdoutString ) {
135- const match = stdoutString . match ( percentRegex ) ;
136- if ( match ) {
137- const [ lostPackages ] = match ;
138- totalLost = Number ( lostPackages . replace ( '%' , '' ) ) ;
139- }
140- }
141- }
142- if ( err !== null || ! ! stderr || totalLost > 50 ) {
143- if ( pingFailedCounter === 0 ) {
144- checkDelay = failedPingDelayMs ;
145- clearInterval ( checkConnTimeout ) ;
146- checkAlive ( ) ;
147- } else if ( pingFailedCounter + 1 > 2 ) {
148- setConnectionStatus ( NETWORK_STATUS . OFFLINE ) ;
149- }
150- pingFailedCounter ++ ;
151- } else if ( stdout ) {
152- setConnectionStatus ( NETWORK_STATUS . ONLINE ) ;
153- if ( pingFailedCounter > 0 ) {
154- pingFailedCounter = 0 ;
155- checkDelay = normalPingDelayMs ;
156- clearInterval ( checkConnTimeout ) ;
157- checkAlive ( ) ;
158- }
159- }
160- }
161- ) ;
162- } , getDelay ( ) ) ;
163- } ;
164-
16568process . on ( 'exit' , ( ) => {
166- checkConnTimeout = null ;
69+ disconnect ( ) ;
16770} ) ;
16871
16972const restartSocket = ( { jwt } ) => {
0 commit comments