@@ -76,6 +76,12 @@ import utils from './utils';
76
76
* @type {boolean }
77
77
*/
78
78
let auto_reconnect : boolean = false ;
79
+
80
+ /**
81
+ * Flag to control reconnection behavior.
82
+ * @type {boolean }
83
+ */
84
+ let should_reconnect : boolean = true ;
79
85
80
86
/**
81
87
* Current count of reconnection attempts.
@@ -324,8 +330,10 @@ export class KiteTicker implements KiteTickerInterface {
324
330
this . modeFull = modeFull ;
325
331
this . modeQuote = modeQuote ;
326
332
this . modeLTP = modeLTP ;
327
-
328
- if ( ! params . reconnect ) params . reconnect = true ;
333
+ // Set reconnect to true for undefined
334
+ if ( params . reconnect === undefined ) {
335
+ params . reconnect = true ;
336
+ }
329
337
this . autoReconnect ( params . reconnect , params . max_retry as number , params . max_delay as number ) ;
330
338
}
331
339
@@ -335,7 +343,7 @@ export class KiteTicker implements KiteTickerInterface {
335
343
* @param {number } [max_delay=60]
336
344
*/
337
345
autoReconnect ( t : boolean , max_retry : number , max_delay : number ) {
338
- auto_reconnect = ( t == true ) ;
346
+ auto_reconnect = t ;
339
347
340
348
// Set default values
341
349
max_retry = max_retry || defaultReconnectMaxRetries ;
@@ -371,13 +379,13 @@ export class KiteTicker implements KiteTickerInterface {
371
379
// Set binaryType to arraybuffer
372
380
ws . binaryType = 'arraybuffer' ;
373
381
374
- ws . onopen = function ( ) {
382
+ ws . onopen = ( ) => {
375
383
// Reset last reconnect interval
376
384
last_reconnect_interval = null ;
377
385
// Reset current_reconnection_count attempt
378
386
current_reconnection_count = 0
379
387
// Store current open connection url to check for auto re-connection.
380
- if ( ! current_ws_url ) current_ws_url = this . url ;
388
+ if ( ! current_ws_url ) current_ws_url = url ;
381
389
// Trigger on connect event
382
390
trigger ( 'connect' ) ;
383
391
// If there isn't an incoming message in n seconds, assume disconnection.
@@ -398,7 +406,7 @@ export class KiteTicker implements KiteTickerInterface {
398
406
} , read_timeout * 1000 ) ;
399
407
} ;
400
408
401
- ws . onmessage = function ( e ) {
409
+ ws . onmessage = function ( e : any ) {
402
410
// Binary tick data.
403
411
if ( e . data instanceof ArrayBuffer ) {
404
412
// Trigger on message event when binary message is received
@@ -415,14 +423,14 @@ export class KiteTicker implements KiteTickerInterface {
415
423
last_read = new Date ( ) ;
416
424
} ;
417
425
418
- ws . onerror = function ( e ) {
426
+ ws . onerror = function ( e : any ) {
419
427
trigger ( 'error' , [ e ] ) ;
420
428
421
429
// Force close to avoid ghost connections
422
430
if ( this && this . readyState == this . OPEN ) this . close ( ) ;
423
431
} ;
424
432
425
- ws . onclose = ( e ) => {
433
+ ws . onclose = ( e : any ) => {
426
434
trigger ( 'close' , [ e ] ) ;
427
435
428
436
// the ws id doesn't match the current global id,
@@ -435,7 +443,8 @@ export class KiteTicker implements KiteTickerInterface {
435
443
436
444
attemptReconnection ( ) {
437
445
// Try reconnecting only so many times.
438
- if ( current_reconnection_count > reconnect_max_tries ) {
446
+ // Or if reconnection is not allowed
447
+ if ( ( current_reconnection_count > reconnect_max_tries ) || ! should_reconnect ) {
439
448
trigger ( 'noreconnect' ) ;
440
449
process . exit ( 1 ) ;
441
450
}
@@ -469,6 +478,20 @@ export class KiteTicker implements KiteTickerInterface {
469
478
if ( auto_reconnect ) this . attemptReconnection ( ) ;
470
479
}
471
480
481
+ /**
482
+ * This method closes the WebSocket connection if it is currently open.
483
+ * It checks the readyState to ensure that the connection is not
484
+ * already in the process of closing or closed.
485
+ */
486
+ disconnect ( ) : void {
487
+ if ( ws && ws . readyState !== WebSocket . CLOSING && ws . readyState !== WebSocket . CLOSED ) {
488
+ ws . close ( ) ;
489
+ ws = null ;
490
+ // Stop reconnection mechanism
491
+ should_reconnect = false ;
492
+ }
493
+ }
494
+
472
495
/**
473
496
* Checks if the WebSocket connection is currently open.
474
497
*
0 commit comments