Skip to content

Commit 773afb7

Browse files
authored
feat: add disconnect ticker method with no reconnection (#102)
1 parent 3a57004 commit 773afb7

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

lib/ticker.ts

+33-9
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ import utils from './utils';
7676
* @type {boolean}
7777
*/
7878
let auto_reconnect: boolean = false;
79+
80+
/**
81+
* Flag to control reconnection behavior.
82+
* @type {boolean}
83+
*/
84+
let should_reconnect: boolean = true;
7985

8086
/**
8187
* Current count of reconnection attempts.
@@ -324,8 +330,10 @@ export class KiteTicker implements KiteTickerInterface {
324330
this.modeFull = modeFull;
325331
this.modeQuote = modeQuote;
326332
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+
}
329337
this.autoReconnect(params.reconnect, params.max_retry as number, params.max_delay as number);
330338
}
331339

@@ -335,7 +343,7 @@ export class KiteTicker implements KiteTickerInterface {
335343
* @param {number} [max_delay=60]
336344
*/
337345
autoReconnect(t: boolean, max_retry: number, max_delay: number) {
338-
auto_reconnect = (t == true);
346+
auto_reconnect = t;
339347

340348
// Set default values
341349
max_retry = max_retry || defaultReconnectMaxRetries;
@@ -371,13 +379,13 @@ export class KiteTicker implements KiteTickerInterface {
371379
// Set binaryType to arraybuffer
372380
ws.binaryType = 'arraybuffer';
373381

374-
ws.onopen = function () {
382+
ws.onopen = () => {
375383
// Reset last reconnect interval
376384
last_reconnect_interval = null;
377385
// Reset current_reconnection_count attempt
378386
current_reconnection_count = 0
379387
// 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;
381389
// Trigger on connect event
382390
trigger('connect');
383391
// If there isn't an incoming message in n seconds, assume disconnection.
@@ -398,7 +406,7 @@ export class KiteTicker implements KiteTickerInterface {
398406
}, read_timeout * 1000);
399407
};
400408

401-
ws.onmessage = function (e) {
409+
ws.onmessage = function (e:any) {
402410
// Binary tick data.
403411
if (e.data instanceof ArrayBuffer) {
404412
// Trigger on message event when binary message is received
@@ -415,14 +423,14 @@ export class KiteTicker implements KiteTickerInterface {
415423
last_read = new Date();
416424
};
417425

418-
ws.onerror = function (e) {
426+
ws.onerror = function (e:any) {
419427
trigger('error', [e]);
420428

421429
// Force close to avoid ghost connections
422430
if (this && this.readyState == this.OPEN) this.close();
423431
};
424432

425-
ws.onclose = (e) => {
433+
ws.onclose = (e:any) => {
426434
trigger('close', [e]);
427435

428436
// the ws id doesn't match the current global id,
@@ -435,7 +443,8 @@ export class KiteTicker implements KiteTickerInterface {
435443

436444
attemptReconnection() {
437445
// 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) {
439448
trigger('noreconnect');
440449
process.exit(1);
441450
}
@@ -469,6 +478,21 @@ export class KiteTicker implements KiteTickerInterface {
469478
if (auto_reconnect) this.attemptReconnection();
470479
}
471480

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+
// Stop reconnection mechanism
489+
should_reconnect = false;
490+
// Close and clear the ws object
491+
ws.close();
492+
ws = null;
493+
}
494+
}
495+
472496
/**
473497
* Checks if the WebSocket connection is currently open.
474498
*

types/ticker.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export type Ticker = {
5858
/**
5959
* Check if the ticker is connected
6060
*/
61-
disconnect: () => boolean;
61+
disconnect: () => void;
6262
/**
6363
* Register websocket event callbacks
6464
* Available events

0 commit comments

Comments
 (0)