shoonya-sdk 1.1.1 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +51 -55
- package/dist/index.d.cts +4 -5
- package/dist/index.d.ts +4 -5
- package/dist/index.js +51 -55
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -141,7 +141,7 @@ var Tokens = class {
|
|
|
141
141
|
}
|
|
142
142
|
};
|
|
143
143
|
var tokens = new Tokens();
|
|
144
|
-
async function refreshAccessToken() {
|
|
144
|
+
async function refreshAccessToken(retryCount = 3) {
|
|
145
145
|
const userCred = tokens.getCred();
|
|
146
146
|
const cred = {
|
|
147
147
|
apkversion: "1.0.0",
|
|
@@ -153,14 +153,23 @@ async function refreshAccessToken() {
|
|
|
153
153
|
imei: userCred?.imei || "api",
|
|
154
154
|
source: "API"
|
|
155
155
|
};
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
156
|
+
let errorMsg = "";
|
|
157
|
+
for (let i = 0; i < retryCount; i++) {
|
|
158
|
+
try {
|
|
159
|
+
const req = await request("login", { data: cred });
|
|
160
|
+
tokens.updateAccessToken(req.susertoken);
|
|
161
|
+
return;
|
|
162
|
+
} catch (err) {
|
|
163
|
+
let errMsg = "Couldn't Refresh the Access Token. ";
|
|
164
|
+
if (err instanceof Error) {
|
|
165
|
+
errMsg += `Error: ${err.message}`;
|
|
166
|
+
}
|
|
167
|
+
errorMsg += `${errMsg}, `;
|
|
168
|
+
}
|
|
163
169
|
}
|
|
170
|
+
throw new Error(
|
|
171
|
+
`Attempted to refresh access token ${retryCount} times. but failed. Errors: ${errorMsg}`
|
|
172
|
+
);
|
|
164
173
|
}
|
|
165
174
|
__name(refreshAccessToken, "refreshAccessToken");
|
|
166
175
|
|
|
@@ -754,8 +763,6 @@ var WebsocketClient = class extends import_events.EventEmitter {
|
|
|
754
763
|
}
|
|
755
764
|
wsBaseUrl = "wss://api.shoonya.com/NorenWSTP/";
|
|
756
765
|
ws;
|
|
757
|
-
reconnectInterval;
|
|
758
|
-
reconnectTimer;
|
|
759
766
|
subscribedTokens = [];
|
|
760
767
|
logger;
|
|
761
768
|
retryCount = 0;
|
|
@@ -768,7 +775,6 @@ var WebsocketClient = class extends import_events.EventEmitter {
|
|
|
768
775
|
constructor(params) {
|
|
769
776
|
super();
|
|
770
777
|
const {
|
|
771
|
-
reconnectInterval = 3e4,
|
|
772
778
|
dailyRefreshTime = "09:13+05:30",
|
|
773
779
|
cred,
|
|
774
780
|
logging = false,
|
|
@@ -790,7 +796,6 @@ var WebsocketClient = class extends import_events.EventEmitter {
|
|
|
790
796
|
if (!storedCred.userId) {
|
|
791
797
|
tokens.updateCred(cred);
|
|
792
798
|
}
|
|
793
|
-
this.reconnectInterval = reconnectInterval;
|
|
794
799
|
if (this.validateTimezoneString(dailyRefreshTime)) {
|
|
795
800
|
const dt = this.parseDailyRefreshTime(dailyRefreshTime);
|
|
796
801
|
this.refreshEveryMorning(dt);
|
|
@@ -806,7 +811,7 @@ var WebsocketClient = class extends import_events.EventEmitter {
|
|
|
806
811
|
this.ws = new import_ws.WebSocket(this.wsBaseUrl);
|
|
807
812
|
this.ws.on("message", this.wsMessageEvent);
|
|
808
813
|
this.ws.on("open", this.wsOpenEvent);
|
|
809
|
-
this.ws.on("close", this.
|
|
814
|
+
this.ws.on("close", this.wsCloseEvent);
|
|
810
815
|
this.ws.on("error", this.wsErrorEvent);
|
|
811
816
|
}
|
|
812
817
|
wsOpenEvent = async () => {
|
|
@@ -827,11 +832,11 @@ var WebsocketClient = class extends import_events.EventEmitter {
|
|
|
827
832
|
wsMessageEvent = (msg) => {
|
|
828
833
|
const result = JSON.parse(msg.toString());
|
|
829
834
|
if (result.t === "ck" && result.s.toLowerCase() === "ok") {
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
+
setTimeout(() => {
|
|
836
|
+
this.logger.log("|WSClient| Connected with Shoonya WS.");
|
|
837
|
+
this.emit("connected");
|
|
838
|
+
this.resubscribe();
|
|
839
|
+
}, 3e3);
|
|
835
840
|
}
|
|
836
841
|
if (result.t === "dk") {
|
|
837
842
|
this.logger.log(`Subscribed to ${result.ts}.`);
|
|
@@ -858,44 +863,17 @@ var WebsocketClient = class extends import_events.EventEmitter {
|
|
|
858
863
|
this.emit("error", error);
|
|
859
864
|
this.ws.close(0);
|
|
860
865
|
};
|
|
861
|
-
wsCloseEvent = async (code) => {
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
this.logger.log("Access Token Refreshed");
|
|
866
|
-
} catch (err) {
|
|
867
|
-
err instanceof Error && this.logger.log(err.message, "ERROR");
|
|
868
|
-
}
|
|
869
|
-
this.emit("tokenRefresh");
|
|
870
|
-
this.connect();
|
|
871
|
-
return;
|
|
872
|
-
}
|
|
873
|
-
if (code !== 0) {
|
|
874
|
-
let retryAttempts = 0;
|
|
875
|
-
clearInterval(this.reconnectTimer);
|
|
876
|
-
this.reconnectTimer = setInterval(() => {
|
|
877
|
-
retryAttempts++;
|
|
878
|
-
this.logger.log(
|
|
879
|
-
`Disconnected with error code ${code}. Attempt ${retryAttempts} to reconnect....`
|
|
880
|
-
);
|
|
881
|
-
this.connect();
|
|
882
|
-
if (this.maxRetryAttempt === retryAttempts) {
|
|
883
|
-
this.logger.log("Max Retry Attempt Reached. Stopped Retrying.");
|
|
884
|
-
clearInterval(this.reconnectInterval);
|
|
885
|
-
}
|
|
886
|
-
}, this.reconnectInterval);
|
|
887
|
-
return;
|
|
888
|
-
}
|
|
889
|
-
this.logger.log("|WSClient| Connection Closed Successfully");
|
|
890
|
-
this.emit("close");
|
|
891
|
-
};
|
|
892
|
-
wsCloseEventV2 = async (code, reason) => {
|
|
866
|
+
wsCloseEvent = async (code, reason) => {
|
|
867
|
+
this.logger.log(
|
|
868
|
+
`WS Close Event: code: ${code}, reason: ${reason.toString()}`
|
|
869
|
+
);
|
|
893
870
|
if (code === 0) {
|
|
894
871
|
this.logger.log("Websocket closed gracefully");
|
|
895
872
|
return;
|
|
896
873
|
}
|
|
897
874
|
if (code === 1) {
|
|
898
875
|
try {
|
|
876
|
+
this.logger.log("Trying to refresh the Access Token");
|
|
899
877
|
await refreshAccessToken();
|
|
900
878
|
this.logger.log("Access Token Refreshed");
|
|
901
879
|
} catch (err) {
|
|
@@ -903,6 +881,7 @@ var WebsocketClient = class extends import_events.EventEmitter {
|
|
|
903
881
|
`Failed to update access token. Err: ${err.message}`,
|
|
904
882
|
"ERROR"
|
|
905
883
|
);
|
|
884
|
+
console.error("Failed to refresh access token", err);
|
|
906
885
|
}
|
|
907
886
|
this.emit("tokenRefresh");
|
|
908
887
|
this.connect();
|
|
@@ -920,7 +899,11 @@ var WebsocketClient = class extends import_events.EventEmitter {
|
|
|
920
899
|
console.log(`Attempt #${this.retryCount} to reconnect again`);
|
|
921
900
|
this.connect();
|
|
922
901
|
};
|
|
923
|
-
subscribe(scrips) {
|
|
902
|
+
subscribe(scrips, feedType = "Depth") {
|
|
903
|
+
const t = this.validateFeedType(feedType);
|
|
904
|
+
if (!t) {
|
|
905
|
+
return cc.red("Feed Type should be either Depth or Touchline");
|
|
906
|
+
}
|
|
924
907
|
const scripList = Array.isArray(scrips) ? scrips : [scrips];
|
|
925
908
|
const distinctScrips = checkTwoArrayOverlaps(
|
|
926
909
|
this.subscribedTokens,
|
|
@@ -952,7 +935,7 @@ var WebsocketClient = class extends import_events.EventEmitter {
|
|
|
952
935
|
}
|
|
953
936
|
resubscribe() {
|
|
954
937
|
if (this.subscribedTokens.length) {
|
|
955
|
-
const tokensToSub = this.subscribedTokens;
|
|
938
|
+
const tokensToSub = Array.from(new Set(this.subscribedTokens));
|
|
956
939
|
this.subscribedTokens = [];
|
|
957
940
|
this.subscribe(tokensToSub);
|
|
958
941
|
}
|
|
@@ -960,6 +943,12 @@ var WebsocketClient = class extends import_events.EventEmitter {
|
|
|
960
943
|
send(msg) {
|
|
961
944
|
if (this.ws && this.ws.readyState === this.ws.OPEN) {
|
|
962
945
|
this.ws.send(JSON.stringify(msg));
|
|
946
|
+
} else {
|
|
947
|
+
this.logger.log(
|
|
948
|
+
`didn't send the message ${JSON.stringify(
|
|
949
|
+
msg
|
|
950
|
+
)} due to ws connection being closed`
|
|
951
|
+
);
|
|
963
952
|
}
|
|
964
953
|
}
|
|
965
954
|
refreshEveryMorning = (dt) => {
|
|
@@ -971,10 +960,11 @@ var WebsocketClient = class extends import_events.EventEmitter {
|
|
|
971
960
|
const currentMin = date.getUTCMinutes();
|
|
972
961
|
const currentHour = date.getUTCHours();
|
|
973
962
|
if (lastMin !== min && hour === currentHour && min === currentMin) {
|
|
974
|
-
this.logger.log(
|
|
975
|
-
|
|
963
|
+
this.logger.log(
|
|
964
|
+
`refreshing access token at ${currentHour}:${currentMin} - ${lastMin}`
|
|
965
|
+
);
|
|
976
966
|
lastMin = min;
|
|
977
|
-
return;
|
|
967
|
+
return this.ws.close(1, "Reconnection");
|
|
978
968
|
}
|
|
979
969
|
lastMin = -1;
|
|
980
970
|
}, 5e4);
|
|
@@ -1016,6 +1006,12 @@ var WebsocketClient = class extends import_events.EventEmitter {
|
|
|
1016
1006
|
}
|
|
1017
1007
|
return true;
|
|
1018
1008
|
};
|
|
1009
|
+
validateFeedType(feedType) {
|
|
1010
|
+
if (feedType === "Depth" || feedType === "Touchline") {
|
|
1011
|
+
return feedType === "Depth" ? "d" : "t";
|
|
1012
|
+
}
|
|
1013
|
+
return null;
|
|
1014
|
+
}
|
|
1019
1015
|
};
|
|
1020
1016
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1021
1017
|
0 && (module.exports = {
|
package/dist/index.d.cts
CHANGED
|
@@ -882,6 +882,7 @@ declare class RestClient {
|
|
|
882
882
|
type LoggerType = "UNIFIED" | "SEPARATE";
|
|
883
883
|
|
|
884
884
|
type DailyTimeString = `${number}:${number}${"+" | "-"}${number}:${number}`;
|
|
885
|
+
type FeedType = "Touchline" | "Depth";
|
|
885
886
|
interface Events {
|
|
886
887
|
open: () => void;
|
|
887
888
|
connected: () => void;
|
|
@@ -903,8 +904,6 @@ declare interface WebsocketClient {
|
|
|
903
904
|
declare class WebsocketClient extends EventEmitter {
|
|
904
905
|
private wsBaseUrl;
|
|
905
906
|
private ws;
|
|
906
|
-
private reconnectInterval;
|
|
907
|
-
private reconnectTimer;
|
|
908
907
|
private subscribedTokens;
|
|
909
908
|
private logger;
|
|
910
909
|
private retryCount;
|
|
@@ -927,8 +926,7 @@ declare class WebsocketClient extends EventEmitter {
|
|
|
927
926
|
private wsMessageEvent;
|
|
928
927
|
private wsErrorEvent;
|
|
929
928
|
private wsCloseEvent;
|
|
930
|
-
|
|
931
|
-
subscribe(scrips: string | string[]): void;
|
|
929
|
+
subscribe(scrips: string | string[], feedType?: FeedType): void;
|
|
932
930
|
unsubscribe(scrips: string | string[]): void;
|
|
933
931
|
close(): void;
|
|
934
932
|
private resubscribe;
|
|
@@ -936,6 +934,7 @@ declare class WebsocketClient extends EventEmitter {
|
|
|
936
934
|
private refreshEveryMorning;
|
|
937
935
|
private parseDailyRefreshTime;
|
|
938
936
|
private validateTimezoneString;
|
|
937
|
+
private validateFeedType;
|
|
939
938
|
}
|
|
940
939
|
|
|
941
|
-
export { AddScripToWatchlist, BaseType, BaseTypeFail, BaseTypeSuccess, BaseTypeWithoutTime, BasketMargin, CancelOrder, ChangePassword, DailyTimeString, DepthSubscriptionUpdate, ExchMessage, Exchange, ExitSNOOrder, ForgotPassword, GetClientDetails, GetHSToken, GetLimitsParam, GetListOfPredefinedMW, GetListOfPredefinedMWScrip, GetSecurityInfo, GetTokenExpiry, GetUserDetails, GetWatchlist, GetWatchlistNames, HistoricData, Holdings, IndexList, Limits, Login, LoginWithDevicePin, LogoutResponse, MakeKeysRequired, ModifyOrder, OptionChain, Order, OrderBook, OrderInput, OrderMargin, Path, PlaceOrder, PositionBook, ProductConversion, ProductType, RemoveScripFromWatchlist, RestClient, ScripInfo, SearchScrip, Segment, SetDevicePin, SingleOrderHistory, SingleOrderStatus, TopIndexList, TopIndexListNames, TradeBook, UserCred, ValidateHSToken, WebsocketClient, getQuotes, paths };
|
|
940
|
+
export { AddScripToWatchlist, BaseType, BaseTypeFail, BaseTypeSuccess, BaseTypeWithoutTime, BasketMargin, CancelOrder, ChangePassword, DailyTimeString, DepthSubscriptionUpdate, ExchMessage, Exchange, ExitSNOOrder, FeedType, ForgotPassword, GetClientDetails, GetHSToken, GetLimitsParam, GetListOfPredefinedMW, GetListOfPredefinedMWScrip, GetSecurityInfo, GetTokenExpiry, GetUserDetails, GetWatchlist, GetWatchlistNames, HistoricData, Holdings, IndexList, Limits, Login, LoginWithDevicePin, LogoutResponse, MakeKeysRequired, ModifyOrder, OptionChain, Order, OrderBook, OrderInput, OrderMargin, Path, PlaceOrder, PositionBook, ProductConversion, ProductType, RemoveScripFromWatchlist, RestClient, ScripInfo, SearchScrip, Segment, SetDevicePin, SingleOrderHistory, SingleOrderStatus, TopIndexList, TopIndexListNames, TradeBook, UserCred, ValidateHSToken, WebsocketClient, getQuotes, paths };
|
package/dist/index.d.ts
CHANGED
|
@@ -882,6 +882,7 @@ declare class RestClient {
|
|
|
882
882
|
type LoggerType = "UNIFIED" | "SEPARATE";
|
|
883
883
|
|
|
884
884
|
type DailyTimeString = `${number}:${number}${"+" | "-"}${number}:${number}`;
|
|
885
|
+
type FeedType = "Touchline" | "Depth";
|
|
885
886
|
interface Events {
|
|
886
887
|
open: () => void;
|
|
887
888
|
connected: () => void;
|
|
@@ -903,8 +904,6 @@ declare interface WebsocketClient {
|
|
|
903
904
|
declare class WebsocketClient extends EventEmitter {
|
|
904
905
|
private wsBaseUrl;
|
|
905
906
|
private ws;
|
|
906
|
-
private reconnectInterval;
|
|
907
|
-
private reconnectTimer;
|
|
908
907
|
private subscribedTokens;
|
|
909
908
|
private logger;
|
|
910
909
|
private retryCount;
|
|
@@ -927,8 +926,7 @@ declare class WebsocketClient extends EventEmitter {
|
|
|
927
926
|
private wsMessageEvent;
|
|
928
927
|
private wsErrorEvent;
|
|
929
928
|
private wsCloseEvent;
|
|
930
|
-
|
|
931
|
-
subscribe(scrips: string | string[]): void;
|
|
929
|
+
subscribe(scrips: string | string[], feedType?: FeedType): void;
|
|
932
930
|
unsubscribe(scrips: string | string[]): void;
|
|
933
931
|
close(): void;
|
|
934
932
|
private resubscribe;
|
|
@@ -936,6 +934,7 @@ declare class WebsocketClient extends EventEmitter {
|
|
|
936
934
|
private refreshEveryMorning;
|
|
937
935
|
private parseDailyRefreshTime;
|
|
938
936
|
private validateTimezoneString;
|
|
937
|
+
private validateFeedType;
|
|
939
938
|
}
|
|
940
939
|
|
|
941
|
-
export { AddScripToWatchlist, BaseType, BaseTypeFail, BaseTypeSuccess, BaseTypeWithoutTime, BasketMargin, CancelOrder, ChangePassword, DailyTimeString, DepthSubscriptionUpdate, ExchMessage, Exchange, ExitSNOOrder, ForgotPassword, GetClientDetails, GetHSToken, GetLimitsParam, GetListOfPredefinedMW, GetListOfPredefinedMWScrip, GetSecurityInfo, GetTokenExpiry, GetUserDetails, GetWatchlist, GetWatchlistNames, HistoricData, Holdings, IndexList, Limits, Login, LoginWithDevicePin, LogoutResponse, MakeKeysRequired, ModifyOrder, OptionChain, Order, OrderBook, OrderInput, OrderMargin, Path, PlaceOrder, PositionBook, ProductConversion, ProductType, RemoveScripFromWatchlist, RestClient, ScripInfo, SearchScrip, Segment, SetDevicePin, SingleOrderHistory, SingleOrderStatus, TopIndexList, TopIndexListNames, TradeBook, UserCred, ValidateHSToken, WebsocketClient, getQuotes, paths };
|
|
940
|
+
export { AddScripToWatchlist, BaseType, BaseTypeFail, BaseTypeSuccess, BaseTypeWithoutTime, BasketMargin, CancelOrder, ChangePassword, DailyTimeString, DepthSubscriptionUpdate, ExchMessage, Exchange, ExitSNOOrder, FeedType, ForgotPassword, GetClientDetails, GetHSToken, GetLimitsParam, GetListOfPredefinedMW, GetListOfPredefinedMWScrip, GetSecurityInfo, GetTokenExpiry, GetUserDetails, GetWatchlist, GetWatchlistNames, HistoricData, Holdings, IndexList, Limits, Login, LoginWithDevicePin, LogoutResponse, MakeKeysRequired, ModifyOrder, OptionChain, Order, OrderBook, OrderInput, OrderMargin, Path, PlaceOrder, PositionBook, ProductConversion, ProductType, RemoveScripFromWatchlist, RestClient, ScripInfo, SearchScrip, Segment, SetDevicePin, SingleOrderHistory, SingleOrderStatus, TopIndexList, TopIndexListNames, TradeBook, UserCred, ValidateHSToken, WebsocketClient, getQuotes, paths };
|
package/dist/index.js
CHANGED
|
@@ -106,7 +106,7 @@ var Tokens = class {
|
|
|
106
106
|
}
|
|
107
107
|
};
|
|
108
108
|
var tokens = new Tokens();
|
|
109
|
-
async function refreshAccessToken() {
|
|
109
|
+
async function refreshAccessToken(retryCount = 3) {
|
|
110
110
|
const userCred = tokens.getCred();
|
|
111
111
|
const cred = {
|
|
112
112
|
apkversion: "1.0.0",
|
|
@@ -118,14 +118,23 @@ async function refreshAccessToken() {
|
|
|
118
118
|
imei: userCred?.imei || "api",
|
|
119
119
|
source: "API"
|
|
120
120
|
};
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
121
|
+
let errorMsg = "";
|
|
122
|
+
for (let i = 0; i < retryCount; i++) {
|
|
123
|
+
try {
|
|
124
|
+
const req = await request("login", { data: cred });
|
|
125
|
+
tokens.updateAccessToken(req.susertoken);
|
|
126
|
+
return;
|
|
127
|
+
} catch (err) {
|
|
128
|
+
let errMsg = "Couldn't Refresh the Access Token. ";
|
|
129
|
+
if (err instanceof Error) {
|
|
130
|
+
errMsg += `Error: ${err.message}`;
|
|
131
|
+
}
|
|
132
|
+
errorMsg += `${errMsg}, `;
|
|
133
|
+
}
|
|
128
134
|
}
|
|
135
|
+
throw new Error(
|
|
136
|
+
`Attempted to refresh access token ${retryCount} times. but failed. Errors: ${errorMsg}`
|
|
137
|
+
);
|
|
129
138
|
}
|
|
130
139
|
__name(refreshAccessToken, "refreshAccessToken");
|
|
131
140
|
|
|
@@ -726,8 +735,6 @@ var WebsocketClient = class extends EventEmitter {
|
|
|
726
735
|
}
|
|
727
736
|
wsBaseUrl = "wss://api.shoonya.com/NorenWSTP/";
|
|
728
737
|
ws;
|
|
729
|
-
reconnectInterval;
|
|
730
|
-
reconnectTimer;
|
|
731
738
|
subscribedTokens = [];
|
|
732
739
|
logger;
|
|
733
740
|
retryCount = 0;
|
|
@@ -740,7 +747,6 @@ var WebsocketClient = class extends EventEmitter {
|
|
|
740
747
|
constructor(params) {
|
|
741
748
|
super();
|
|
742
749
|
const {
|
|
743
|
-
reconnectInterval = 3e4,
|
|
744
750
|
dailyRefreshTime = "09:13+05:30",
|
|
745
751
|
cred,
|
|
746
752
|
logging = false,
|
|
@@ -762,7 +768,6 @@ var WebsocketClient = class extends EventEmitter {
|
|
|
762
768
|
if (!storedCred.userId) {
|
|
763
769
|
tokens.updateCred(cred);
|
|
764
770
|
}
|
|
765
|
-
this.reconnectInterval = reconnectInterval;
|
|
766
771
|
if (this.validateTimezoneString(dailyRefreshTime)) {
|
|
767
772
|
const dt = this.parseDailyRefreshTime(dailyRefreshTime);
|
|
768
773
|
this.refreshEveryMorning(dt);
|
|
@@ -778,7 +783,7 @@ var WebsocketClient = class extends EventEmitter {
|
|
|
778
783
|
this.ws = new WebSocket(this.wsBaseUrl);
|
|
779
784
|
this.ws.on("message", this.wsMessageEvent);
|
|
780
785
|
this.ws.on("open", this.wsOpenEvent);
|
|
781
|
-
this.ws.on("close", this.
|
|
786
|
+
this.ws.on("close", this.wsCloseEvent);
|
|
782
787
|
this.ws.on("error", this.wsErrorEvent);
|
|
783
788
|
}
|
|
784
789
|
wsOpenEvent = async () => {
|
|
@@ -799,11 +804,11 @@ var WebsocketClient = class extends EventEmitter {
|
|
|
799
804
|
wsMessageEvent = (msg) => {
|
|
800
805
|
const result = JSON.parse(msg.toString());
|
|
801
806
|
if (result.t === "ck" && result.s.toLowerCase() === "ok") {
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
+
setTimeout(() => {
|
|
808
|
+
this.logger.log("|WSClient| Connected with Shoonya WS.");
|
|
809
|
+
this.emit("connected");
|
|
810
|
+
this.resubscribe();
|
|
811
|
+
}, 3e3);
|
|
807
812
|
}
|
|
808
813
|
if (result.t === "dk") {
|
|
809
814
|
this.logger.log(`Subscribed to ${result.ts}.`);
|
|
@@ -830,44 +835,17 @@ var WebsocketClient = class extends EventEmitter {
|
|
|
830
835
|
this.emit("error", error);
|
|
831
836
|
this.ws.close(0);
|
|
832
837
|
};
|
|
833
|
-
wsCloseEvent = async (code) => {
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
this.logger.log("Access Token Refreshed");
|
|
838
|
-
} catch (err) {
|
|
839
|
-
err instanceof Error && this.logger.log(err.message, "ERROR");
|
|
840
|
-
}
|
|
841
|
-
this.emit("tokenRefresh");
|
|
842
|
-
this.connect();
|
|
843
|
-
return;
|
|
844
|
-
}
|
|
845
|
-
if (code !== 0) {
|
|
846
|
-
let retryAttempts = 0;
|
|
847
|
-
clearInterval(this.reconnectTimer);
|
|
848
|
-
this.reconnectTimer = setInterval(() => {
|
|
849
|
-
retryAttempts++;
|
|
850
|
-
this.logger.log(
|
|
851
|
-
`Disconnected with error code ${code}. Attempt ${retryAttempts} to reconnect....`
|
|
852
|
-
);
|
|
853
|
-
this.connect();
|
|
854
|
-
if (this.maxRetryAttempt === retryAttempts) {
|
|
855
|
-
this.logger.log("Max Retry Attempt Reached. Stopped Retrying.");
|
|
856
|
-
clearInterval(this.reconnectInterval);
|
|
857
|
-
}
|
|
858
|
-
}, this.reconnectInterval);
|
|
859
|
-
return;
|
|
860
|
-
}
|
|
861
|
-
this.logger.log("|WSClient| Connection Closed Successfully");
|
|
862
|
-
this.emit("close");
|
|
863
|
-
};
|
|
864
|
-
wsCloseEventV2 = async (code, reason) => {
|
|
838
|
+
wsCloseEvent = async (code, reason) => {
|
|
839
|
+
this.logger.log(
|
|
840
|
+
`WS Close Event: code: ${code}, reason: ${reason.toString()}`
|
|
841
|
+
);
|
|
865
842
|
if (code === 0) {
|
|
866
843
|
this.logger.log("Websocket closed gracefully");
|
|
867
844
|
return;
|
|
868
845
|
}
|
|
869
846
|
if (code === 1) {
|
|
870
847
|
try {
|
|
848
|
+
this.logger.log("Trying to refresh the Access Token");
|
|
871
849
|
await refreshAccessToken();
|
|
872
850
|
this.logger.log("Access Token Refreshed");
|
|
873
851
|
} catch (err) {
|
|
@@ -875,6 +853,7 @@ var WebsocketClient = class extends EventEmitter {
|
|
|
875
853
|
`Failed to update access token. Err: ${err.message}`,
|
|
876
854
|
"ERROR"
|
|
877
855
|
);
|
|
856
|
+
console.error("Failed to refresh access token", err);
|
|
878
857
|
}
|
|
879
858
|
this.emit("tokenRefresh");
|
|
880
859
|
this.connect();
|
|
@@ -892,7 +871,11 @@ var WebsocketClient = class extends EventEmitter {
|
|
|
892
871
|
console.log(`Attempt #${this.retryCount} to reconnect again`);
|
|
893
872
|
this.connect();
|
|
894
873
|
};
|
|
895
|
-
subscribe(scrips) {
|
|
874
|
+
subscribe(scrips, feedType = "Depth") {
|
|
875
|
+
const t = this.validateFeedType(feedType);
|
|
876
|
+
if (!t) {
|
|
877
|
+
return cc.red("Feed Type should be either Depth or Touchline");
|
|
878
|
+
}
|
|
896
879
|
const scripList = Array.isArray(scrips) ? scrips : [scrips];
|
|
897
880
|
const distinctScrips = checkTwoArrayOverlaps(
|
|
898
881
|
this.subscribedTokens,
|
|
@@ -924,7 +907,7 @@ var WebsocketClient = class extends EventEmitter {
|
|
|
924
907
|
}
|
|
925
908
|
resubscribe() {
|
|
926
909
|
if (this.subscribedTokens.length) {
|
|
927
|
-
const tokensToSub = this.subscribedTokens;
|
|
910
|
+
const tokensToSub = Array.from(new Set(this.subscribedTokens));
|
|
928
911
|
this.subscribedTokens = [];
|
|
929
912
|
this.subscribe(tokensToSub);
|
|
930
913
|
}
|
|
@@ -932,6 +915,12 @@ var WebsocketClient = class extends EventEmitter {
|
|
|
932
915
|
send(msg) {
|
|
933
916
|
if (this.ws && this.ws.readyState === this.ws.OPEN) {
|
|
934
917
|
this.ws.send(JSON.stringify(msg));
|
|
918
|
+
} else {
|
|
919
|
+
this.logger.log(
|
|
920
|
+
`didn't send the message ${JSON.stringify(
|
|
921
|
+
msg
|
|
922
|
+
)} due to ws connection being closed`
|
|
923
|
+
);
|
|
935
924
|
}
|
|
936
925
|
}
|
|
937
926
|
refreshEveryMorning = (dt) => {
|
|
@@ -943,10 +932,11 @@ var WebsocketClient = class extends EventEmitter {
|
|
|
943
932
|
const currentMin = date.getUTCMinutes();
|
|
944
933
|
const currentHour = date.getUTCHours();
|
|
945
934
|
if (lastMin !== min && hour === currentHour && min === currentMin) {
|
|
946
|
-
this.logger.log(
|
|
947
|
-
|
|
935
|
+
this.logger.log(
|
|
936
|
+
`refreshing access token at ${currentHour}:${currentMin} - ${lastMin}`
|
|
937
|
+
);
|
|
948
938
|
lastMin = min;
|
|
949
|
-
return;
|
|
939
|
+
return this.ws.close(1, "Reconnection");
|
|
950
940
|
}
|
|
951
941
|
lastMin = -1;
|
|
952
942
|
}, 5e4);
|
|
@@ -988,6 +978,12 @@ var WebsocketClient = class extends EventEmitter {
|
|
|
988
978
|
}
|
|
989
979
|
return true;
|
|
990
980
|
};
|
|
981
|
+
validateFeedType(feedType) {
|
|
982
|
+
if (feedType === "Depth" || feedType === "Touchline") {
|
|
983
|
+
return feedType === "Depth" ? "d" : "t";
|
|
984
|
+
}
|
|
985
|
+
return null;
|
|
986
|
+
}
|
|
991
987
|
};
|
|
992
988
|
export {
|
|
993
989
|
RestClient,
|