@@ -101,11 +101,17 @@ var ClusterWSClient = (function () {
101101 autoConnect : configurations . autoConnect !== false ,
102102 autoReconnect : configurations . autoReconnect || false ,
103103 autoResubscribe : configurations . autoResubscribe !== false ,
104- autoReconnectOptions : { } ,
104+ autoReconnectOptions : {
105+ attempts : configurations . autoReconnectOptions ?
106+ configurations . autoReconnectOptions . attempts || 0 : 0 ,
107+ minInterval : configurations . autoReconnectOptions ?
108+ configurations . autoReconnectOptions . maxInterval || 500 : 500 ,
109+ maxInterval : configurations . autoReconnectOptions ?
110+ configurations . autoReconnectOptions . maxInterval || 2000 : 2000
111+ } ,
105112 logger : configurations . loggerOptions && configurations . loggerOptions . logger ?
106113 configurations . loggerOptions . logger :
107- new Logger ( configurations . loggerOptions && configurations . loggerOptions . level ?
108- configurations . loggerOptions . level : LogLevel . ALL )
114+ new Logger ( configurations . loggerOptions ? configurations . loggerOptions . level || LogLevel . ALL : LogLevel . ALL )
109115 } ;
110116 if ( ! this . options . url ) {
111117 return this . options . logger . error ( 'url must be provided' ) ;
@@ -115,6 +121,20 @@ var ClusterWSClient = (function () {
115121 this . connect ( ) ;
116122 }
117123 }
124+ Object . defineProperty ( ClusterWSClient . prototype , "OPEN" , {
125+ get : function ( ) {
126+ return this . socket . OPEN ;
127+ } ,
128+ enumerable : true ,
129+ configurable : true
130+ } ) ;
131+ Object . defineProperty ( ClusterWSClient . prototype , "CLOSED" , {
132+ get : function ( ) {
133+ return this . socket . CLOSED ;
134+ } ,
135+ enumerable : true ,
136+ configurable : true
137+ } ) ;
118138 Object . defineProperty ( ClusterWSClient . prototype , "readyState" , {
119139 get : function ( ) {
120140 return this . socket ? this . socket . readyState : 0 ;
@@ -126,8 +146,8 @@ var ClusterWSClient = (function () {
126146 get : function ( ) {
127147 return this . socket . binaryType ;
128148 } ,
129- set : function ( type ) {
130- this . socket . binaryType = type ;
149+ set : function ( binaryType ) {
150+ this . socket . binaryType = binaryType ;
131151 } ,
132152 enumerable : true ,
133153 configurable : true
@@ -139,17 +159,28 @@ var ClusterWSClient = (function () {
139159 }
140160 this . isCreated = true ;
141161 this . socket = new Socket ( this . options . url ) ;
142- this . socket . binaryType = 'arraybuffer' ;
143162 this . socket . onopen = function ( ) {
144163 } ;
145- this . socket . onclose = function ( ) {
164+ this . socket . onclose = function ( codeEvent , reason ) {
165+ _this . isCreated = false ;
166+ var closeCode = typeof codeEvent === 'number' ? codeEvent : codeEvent . code ;
167+ var closeReason = typeof codeEvent === 'number' ? reason : codeEvent . reason ;
168+ _this . emitter . emit ( 'close' , closeCode , closeReason ) ;
169+ if ( _this . options . autoReconnect && closeCode !== 1000 ) {
170+ if ( _this . readyState === _this . CLOSED ) {
171+ return setTimeout ( function ( ) {
172+ _this . connect ( ) ;
173+ } , Math . floor ( Math . random ( ) * ( _this . options . autoReconnectOptions . maxInterval - _this . options . autoReconnectOptions . minInterval + 1 ) ) ) ;
174+ }
175+ }
176+ _this . emitter . removeEvents ( ) ;
146177 } ;
147178 this . socket . onmessage = function ( message ) {
148179 var messageToProcess = message ;
149180 if ( message . data ) {
150181 messageToProcess = message . data ;
151182 }
152- _this . withPing ( messageToProcess , function ( ) {
183+ _this . parsePing ( messageToProcess , function ( ) {
153184 if ( _this . emitter . exist ( 'message' ) ) {
154185 return _this . emitter . emit ( 'message' , messageToProcess ) ;
155186 }
@@ -172,10 +203,10 @@ var ClusterWSClient = (function () {
172203 } ;
173204 ClusterWSClient . prototype . processMessage = function ( message ) {
174205 } ;
175- ClusterWSClient . prototype . withPing = function ( message , next ) {
206+ ClusterWSClient . prototype . parsePing = function ( message , next ) {
176207 var _this = this ;
177208 if ( message . size === 1 || message . byteLength === 1 ) {
178- var pingProcessor_1 = function ( possiblePingMessage ) {
209+ var parser_1 = function ( possiblePingMessage ) {
179210 if ( new Uint8Array ( possiblePingMessage ) [ 0 ] === 57 ) {
180211 _this . socket . send ( PONG ) ;
181212 return _this . emitter . emit ( 'ping' ) ;
@@ -184,10 +215,10 @@ var ClusterWSClient = (function () {
184215 } ;
185216 if ( message instanceof Blob ) {
186217 var reader = new FileReader ( ) ;
187- reader . onload = function ( event ) { return pingProcessor_1 ( event . srcElement . result ) ; } ;
218+ reader . onload = function ( event ) { return parser_1 ( event . srcElement . result ) ; } ;
188219 return reader . readAsArrayBuffer ( message ) ;
189220 }
190- return pingProcessor_1 ( message ) ;
221+ return parser_1 ( message ) ;
191222 }
192223 return next ( ) ;
193224 } ;
0 commit comments