Skip to content

Commit

Permalink
feat: add disconnect ticker method with no reconnection (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
ranjanrak authored Aug 23, 2024
1 parent 3a57004 commit 773afb7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
42 changes: 33 additions & 9 deletions lib/ticker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ import utils from './utils';
* @type {boolean}
*/
let auto_reconnect: boolean = false;

/**
* Flag to control reconnection behavior.
* @type {boolean}
*/
let should_reconnect: boolean = true;

/**
* Current count of reconnection attempts.
Expand Down Expand Up @@ -324,8 +330,10 @@ export class KiteTicker implements KiteTickerInterface {
this.modeFull = modeFull;
this.modeQuote = modeQuote;
this.modeLTP = modeLTP;

if (!params.reconnect) params.reconnect = true;
// Set reconnect to true for undefined
if (params.reconnect === undefined) {
params.reconnect = true;
}
this.autoReconnect(params.reconnect, params.max_retry as number, params.max_delay as number);
}

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

// Set default values
max_retry = max_retry || defaultReconnectMaxRetries;
Expand Down Expand Up @@ -371,13 +379,13 @@ export class KiteTicker implements KiteTickerInterface {
// Set binaryType to arraybuffer
ws.binaryType = 'arraybuffer';

ws.onopen = function () {
ws.onopen = () => {
// Reset last reconnect interval
last_reconnect_interval = null;
// Reset current_reconnection_count attempt
current_reconnection_count = 0
// Store current open connection url to check for auto re-connection.
if (!current_ws_url) current_ws_url = this.url;
if (!current_ws_url) current_ws_url = url;
// Trigger on connect event
trigger('connect');
// If there isn't an incoming message in n seconds, assume disconnection.
Expand All @@ -398,7 +406,7 @@ export class KiteTicker implements KiteTickerInterface {
}, read_timeout * 1000);
};

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

ws.onerror = function (e) {
ws.onerror = function (e:any) {
trigger('error', [e]);

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

ws.onclose = (e) => {
ws.onclose = (e:any) => {
trigger('close', [e]);

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

attemptReconnection() {
// Try reconnecting only so many times.
if (current_reconnection_count > reconnect_max_tries) {
// Or if reconnection is not allowed
if ((current_reconnection_count > reconnect_max_tries) || !should_reconnect) {
trigger('noreconnect');
process.exit(1);
}
Expand Down Expand Up @@ -469,6 +478,21 @@ export class KiteTicker implements KiteTickerInterface {
if (auto_reconnect) this.attemptReconnection();
}

/**
* This method closes the WebSocket connection if it is currently open.
* It checks the readyState to ensure that the connection is not
* already in the process of closing or closed.
*/
disconnect(): void {
if (ws && ws.readyState !== WebSocket.CLOSING && ws.readyState !== WebSocket.CLOSED) {
// Stop reconnection mechanism
should_reconnect = false;
// Close and clear the ws object
ws.close();
ws = null;
}
}

/**
* Checks if the WebSocket connection is currently open.
*
Expand Down
2 changes: 1 addition & 1 deletion types/ticker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export type Ticker = {
/**
* Check if the ticker is connected
*/
disconnect: () => boolean;
disconnect: () => void;
/**
* Register websocket event callbacks
* Available events
Expand Down

0 comments on commit 773afb7

Please sign in to comment.