@@ -3,6 +3,15 @@ function SocketIO(host, port, ssl) {
33  this . port  =  port ; 
44  this . ssl  =  ssl ; 
55  this . socket  =  null ; 
6+   this . wasConnected  =  false ; 
7+   this . forceDisconnect  =  false ; 
8+   this . handlers  =  { 
9+     connect : [ ] , 
10+     reconnect : [ ] , 
11+     connectError : [ ] , 
12+     disconnect : [ ] 
13+   } ; 
14+   this . retrying  =  false ; 
615
716  /** 
817   * Creates a new socket from the provided arguments 
@@ -12,11 +21,50 @@ function SocketIO(host, port, ssl) {
1221   * @param  {int } reconnectionDelay 
1322   */ 
1423  this . connect  =  function  ( autoReconnect ,  reconnectionDelay )  { 
24+     var  self  =  this ; 
25+ 
1526    this . socket  =  window . io ( ( this . ssl  ? 'https://'  : 'http://' )  +  this . host  +  ':'  +  this . port ,  { 
1627      reconnection : autoReconnect , 
1728      reconnectionDelay : reconnectionDelay , 
1829      forceNew : true 
1930    } ) ; 
31+ 
32+     this . socket . on ( 'connect' ,  function ( )  { 
33+       if  ( self . wasConnected )  { 
34+         self . handlers . reconnect . forEach ( function ( handler )  { 
35+           handler ( ) ; 
36+         } ) ; 
37+       } 
38+       else  { 
39+         self . handlers . connect . forEach ( function ( handler )  { 
40+           handler ( ) ; 
41+         } ) ; 
42+       } 
43+ 
44+       self . wasConnected  =  true ; 
45+     } ) ; 
46+ 
47+     this . socket . on ( 'connect_error' ,  function ( error )  { 
48+       onClientNetworkError ( self ,  autoReconnect ,  reconnectionDelay ,  error ) ; 
49+     } ) ; 
50+ 
51+     this . socket . on ( 'disconnect' ,  function ( )  { 
52+       var  error ; 
53+ 
54+       if  ( self . forceDisconnect )  { 
55+         self . handlers . disconnect . forEach ( function ( handler )  { 
56+           handler ( ) ; 
57+         } ) ; 
58+       } 
59+       else  { 
60+         error  =  new  Error ( 'An error occurred, this may due that kuzzle was not ready yet' ) ; 
61+         error . status  =  500 ; 
62+ 
63+         onClientNetworkError ( self ,  autoReconnect ,  reconnectionDelay ,  error ) ; 
64+       } 
65+ 
66+       self . forceDisconnect  =  false ; 
67+     } ) ; 
2068  } ; 
2169
2270  /** 
@@ -25,31 +73,39 @@ function SocketIO(host, port, ssl) {
2573   * @param  {function } callback 
2674   */ 
2775  this . onConnect  =  function  ( callback )  { 
28-     this . socket . on ( 'connect' ,  callback ) ; 
76+     if  ( this . handlers . connect . indexOf ( callback )  ===  - 1 )  { 
77+       this . handlers . connect . push ( callback ) ; 
78+     } 
2979  } ; 
3080
3181  /** 
3282   * Fires the provided callback whenever a connection error is received 
3383   * @param  {function } callback 
3484   */ 
3585  this . onConnectError  =  function  ( callback )  { 
36-     this . socket . on ( 'connect_error' ,  callback ) ; 
86+     if  ( this . handlers . connectError . indexOf ( callback )  ===  - 1 )  { 
87+       this . handlers . connectError . push ( callback ) ; 
88+     } 
3789  } ; 
3890
3991  /** 
4092   * Fires the provided callback whenever a disconnection occurred 
4193   * @param  {function } callback 
4294   */ 
4395  this . onDisconnect  =  function  ( callback )  { 
44-     this . socket . on ( 'disconnect' ,  callback ) ; 
96+     if  ( this . handlers . disconnect . indexOf ( callback )  ===  - 1 )  { 
97+       this . handlers . disconnect . push ( callback ) ; 
98+     } 
4599  } ; 
46100
47101  /** 
48102   * Fires the provided callback whenever a connection has been reestablished 
49103   * @param  {function } callback 
50104   */ 
51105  this . onReconnect  =  function  ( callback )  { 
52-     this . socket . on ( 'reconnect' ,  callback ) ; 
106+     if  ( this . handlers . reconnect . indexOf ( callback )  ===  - 1 )  { 
107+       this . handlers . reconnect . push ( callback ) ; 
108+     } 
53109  } ; 
54110
55111  /** 
@@ -97,9 +153,34 @@ function SocketIO(host, port, ssl) {
97153   * Closes the connection 
98154   */ 
99155  this . close  =  function  ( )  { 
156+     this . forceDisconnect  =  true ; 
157+ 
100158    this . socket . close ( ) ; 
101159    this . socket  =  null ; 
102160  } ; 
103161} 
104162
163+ /** 
164+  * Called when the connection closes with an error state 
165+  * 
166+  * @param  {SocketIO } 
167+  * @param  {boolean } autoReconnect 
168+  * @param  {number } reconnectionDelay 
169+  * @param  {Error } error 
170+  */ 
171+ function  onClientNetworkError ( socketio ,  autoReconnect ,  reconnectionDelay ,  error )  { 
172+   if  ( autoReconnect  &&  ! socketio . retrying  &&  ! socketio . stopRetryingToConnect )  { 
173+     socketio . retrying  =  true ; 
174+     setTimeout ( function  ( )  { 
175+       socketio . retrying  =  false ; 
176+       socketio . connect ( autoReconnect ,  reconnectionDelay ) ; 
177+     } ,  reconnectionDelay ) ; 
178+   } 
179+ 
180+   socketio . handlers . connectError . forEach ( function ( handler )  { 
181+     handler ( error ) ; 
182+   } ) ; 
183+ } 
184+ 
185+ 
105186module . exports  =  SocketIO ; 
0 commit comments