@@ -43,19 +43,17 @@ function WSNode(host, port, ssl) {
4343 }
4444 } ) ;
4545
46- this . client . on ( 'close' , function ( ) {
47- poke ( self . listeners , 'disconnect' ) ;
48- } ) ;
49-
50- this . client . on ( 'error' , function ( ) {
51- if ( autoReconnect ) {
52- self . retrying = true ;
53- setTimeout ( function ( ) {
54- self . connect ( autoReconnect , reconnectionDelay ) ;
55- } , reconnectionDelay ) ;
46+ this . client . on ( 'close' , function ( code , message ) {
47+ if ( code === 1000 ) {
48+ poke ( self . listeners , 'disconnect' ) ;
49+ }
50+ else {
51+ onClientError . call ( self , autoReconnect , reconnectionDelay , message ) ;
5652 }
53+ } ) ;
5754
58- poke ( self . listeners , 'error' ) ;
55+ this . client . on ( 'error' , function ( error ) {
56+ onClientError . call ( self , autoReconnect , reconnectionDelay , error ) ;
5957 } ) ;
6058
6159 this . client . on ( 'message' , function ( payload ) {
@@ -237,4 +235,24 @@ function poke (listeners, roomId, payload) {
237235 }
238236}
239237
238+ /**
239+ * Called when the connection closes with an error state
240+ *
241+ * @param {boolean } autoReconnect
242+ * @param {number } reconnectionDelay
243+ * @param {string|Object } message
244+ */
245+ function onClientError ( autoReconnect , reconnectionDelay , message ) {
246+ var self = this ;
247+
248+ if ( autoReconnect ) {
249+ self . retrying = true ;
250+ setTimeout ( function ( ) {
251+ self . connect ( autoReconnect , reconnectionDelay ) ;
252+ } , reconnectionDelay ) ;
253+ }
254+
255+ poke ( self . listeners , 'error' , message ) ;
256+ }
257+
240258module . exports = WSNode ;
0 commit comments