fyers-api-v3 1.3.4 → 1.4.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/HSM/datasocket.min.js +1 -1
- package/HSM/mapper.js +220 -220
- package/HSM_Package/hslib.js +4 -4
- package/README.md +171 -171
- package/apiService/apiService.js +1026 -995
- package/config/config.js +30 -29
- package/errorHandler/errorHandler.js +36 -36
- package/index.js +11 -9
- package/logger/log.js +61 -61
- package/obfuscate.js +24 -24
- package/ordersocket/fyersSocket.js +325 -325
- package/ordersocket/mapper.js +78 -78
- package/package.json +25 -24
- package/sample/api.js +45 -45
- package/sample/datasocket.js +24 -24
- package/sample/ordersocket.js +29 -29
- package/sample/tbtsocket.js +25 -0
- package/tbtsocket/models.js +172 -0
- package/tbtsocket/msg.js +5163 -0
- package/tbtsocket/tbtSocket.js +339 -0
|
@@ -1,326 +1,326 @@
|
|
|
1
|
-
const WebSocket = require('ws');
|
|
2
|
-
let Logger = require("../logger/log.js")
|
|
3
|
-
let { Config } = require("../config/config");
|
|
4
|
-
let mapper =require("./mapper.js")
|
|
5
|
-
var reconnectiontries = 0
|
|
6
|
-
const ordstatobj={11: 4, 12: 4, 20: 4, 21: 4, 22: 6, 23: 6, 24: 6, 25: 6, 26: 6, 90: 2,
|
|
7
|
-
91: 1, 92: 5, 93: 5, 94: 5, 51: 6, 52: 6, 53: 6, 54: 6, 55: 6, 61: 6, 62: 6, 63: 6,
|
|
8
|
-
64: 6, 71: 6, 72: 6, 73: 1}
|
|
9
|
-
function datamapper(a,b){
|
|
10
|
-
const result = {};
|
|
11
|
-
|
|
12
|
-
for (const key of Object.keys(b)) {
|
|
13
|
-
if (a.hasOwnProperty(key)) {
|
|
14
|
-
result[b[key]] = a[key];
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return result;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Class to access order update websocket.
|
|
22
|
-
* @class
|
|
23
|
-
*/
|
|
24
|
-
class FyersOrderSocket {
|
|
25
|
-
|
|
26
|
-
constructor(authorizationKey, logpath = undefined ,loggingFlag=true) {
|
|
27
|
-
this.url = Config.Order_SOCKET;
|
|
28
|
-
this.authorizationKey = authorizationKey;
|
|
29
|
-
this.ws = null;
|
|
30
|
-
this.onErrorCallback = null;
|
|
31
|
-
this.onCloseCallback = null;
|
|
32
|
-
this.onMessageCallback = null;
|
|
33
|
-
this.onOpenCallback = null;
|
|
34
|
-
this.orderscallback = null;
|
|
35
|
-
this.LogPath = logpath;
|
|
36
|
-
this.Logger = new Logger(this.LogPath,loggingFlag);
|
|
37
|
-
this.positionscallback = null;
|
|
38
|
-
this.tradescallback = null;
|
|
39
|
-
this.isPingEnabled = true;
|
|
40
|
-
this.pingInterval = null;
|
|
41
|
-
this.autoreconnectinterval = null;
|
|
42
|
-
this.orderUpdates = "orders"
|
|
43
|
-
this.tradeUpdates = "trades"
|
|
44
|
-
this.positionUpdates = "positions"
|
|
45
|
-
this.edis="edis"
|
|
46
|
-
this.pricealerts="pricealerts"
|
|
47
|
-
this.isUserClosed=false
|
|
48
|
-
this.autoreconnectflag=false
|
|
49
|
-
this.maxreconnectiontries = 0
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* called to connect to the order socket
|
|
54
|
-
*/
|
|
55
|
-
connect() {
|
|
56
|
-
const funcname = "connect"
|
|
57
|
-
const logger = this.Logger
|
|
58
|
-
try {
|
|
59
|
-
logger.debug("initalizing connection to socket", { "accesstoken": this.authorizationKey }, funcname)
|
|
60
|
-
this.ws = new WebSocket(this.url, {
|
|
61
|
-
headers: {
|
|
62
|
-
Authorization: this.authorizationKey,
|
|
63
|
-
},
|
|
64
|
-
});
|
|
65
|
-
this.ws.binaryType = 'arraybuffer';
|
|
66
|
-
this.ws.on('error', (error) => {
|
|
67
|
-
if (this.onErrorCallback) {
|
|
68
|
-
this.onErrorCallback(error);
|
|
69
|
-
}
|
|
70
|
-
else {
|
|
71
|
-
console.log("error occoured", error)
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
this.ws.on('close', (event) => {
|
|
76
|
-
this.stopPing()
|
|
77
|
-
if (this.onCloseCallback) {
|
|
78
|
-
this.onCloseCallback(event);
|
|
79
|
-
}
|
|
80
|
-
else {
|
|
81
|
-
console.log("ws closed", event)
|
|
82
|
-
if(!this.isUserClosed&&(reconnectiontries<this.maxreconnectiontries)){
|
|
83
|
-
this._autoreconnect()
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
this.ws.on('message', (message) => {
|
|
89
|
-
const data = message.toString();
|
|
90
|
-
if (data === 'pong') {
|
|
91
|
-
return
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
var parseddata = JSON.parse(data)
|
|
95
|
-
if (parseddata.hasOwnProperty("orders")) {
|
|
96
|
-
var orderdata=datamapper(parseddata["orders"],mapper.orders)
|
|
97
|
-
orderdata.status=ordstatobj[orderdata.status]
|
|
98
|
-
orderdata['orderNumStatus']=orderdata.id+':'+orderdata.status
|
|
99
|
-
if (this.orderscallback) {
|
|
100
|
-
this.orderscallback({"s":"ok","orders":orderdata})
|
|
101
|
-
}
|
|
102
|
-
else {
|
|
103
|
-
console.log("Orders_internal", {"s":"ok","orders":orderdata})
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
else if (parseddata.hasOwnProperty("positions")) {
|
|
107
|
-
var positiondata=datamapper(parseddata["positions"],mapper.position)
|
|
108
|
-
if (this.positionscallback) {
|
|
109
|
-
this.positionscallback({"s":"ok","positions":positiondata})
|
|
110
|
-
}
|
|
111
|
-
else {
|
|
112
|
-
console.log("positions_internal", {"s":"ok","positions":positiondata})
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
else if (parseddata.hasOwnProperty("trades")) {
|
|
116
|
-
var tradebookdata=datamapper(parseddata["trades"],mapper.tradebook)
|
|
117
|
-
if (this.tradescallback) {
|
|
118
|
-
this.tradescallback({"s":"ok","trades":tradebookdata})
|
|
119
|
-
}
|
|
120
|
-
else {
|
|
121
|
-
console.log("trades_internal", {"s":"ok","trades":tradebookdata})
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
else {
|
|
125
|
-
if (this.onMessageCallback) {
|
|
126
|
-
this.onMessageCallback(parseddata);
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
|
-
console.log("message_internal", parseddata)
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
}
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
this.ws.on('open', () => {
|
|
137
|
-
reconnectiontries = 0
|
|
138
|
-
if (this.onOpenCallback) {
|
|
139
|
-
this.onOpenCallback();
|
|
140
|
-
}
|
|
141
|
-
else {
|
|
142
|
-
console.log("connected")
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
if (this.isPingEnabled) {
|
|
146
|
-
this.startPing();
|
|
147
|
-
}
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
catch (error) {
|
|
151
|
-
console.log(`Error occured ${funcname}: ${error}`)
|
|
152
|
-
logger.error("unexpected error on connect function", error, funcname)
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* used to define onmessage,onerror,onopen,onclose for websocket.
|
|
158
|
-
* @param {string} onwhat - defines for what the callback function is.
|
|
159
|
-
* @param {Function} callback - the callback function.
|
|
160
|
-
* @throws error message if onwhat is not valid.
|
|
161
|
-
*/
|
|
162
|
-
on(onwhat, callback) {
|
|
163
|
-
const funcname = "on"
|
|
164
|
-
const logger = this.Logger
|
|
165
|
-
try {
|
|
166
|
-
if (onwhat === 'error') {
|
|
167
|
-
this.onErrorCallback = callback;
|
|
168
|
-
}
|
|
169
|
-
else if (onwhat === 'general') {
|
|
170
|
-
this.onMessageCallback = callback;
|
|
171
|
-
}
|
|
172
|
-
else if (onwhat === 'connect') {
|
|
173
|
-
this.onOpenCallback = callback;
|
|
174
|
-
}
|
|
175
|
-
else if (onwhat === 'close') {
|
|
176
|
-
const socketonclose = function (event){
|
|
177
|
-
callback(event)
|
|
178
|
-
if(!this.isUserClosed&&(reconnectiontries<this.maxreconnectiontries)){
|
|
179
|
-
this._autoreconnect()
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
this.onCloseCallback = socketonclose;
|
|
183
|
-
}
|
|
184
|
-
else if (onwhat === 'orders') {
|
|
185
|
-
this.orderscallback = callback;
|
|
186
|
-
}
|
|
187
|
-
else if (onwhat === 'trades') {
|
|
188
|
-
this.tradescallback = callback;
|
|
189
|
-
}
|
|
190
|
-
else if (onwhat === 'positions') {
|
|
191
|
-
this.positionscallback = callback;
|
|
192
|
-
}
|
|
193
|
-
else {
|
|
194
|
-
console.log("incorrect value passed", onwhat)
|
|
195
|
-
logger.error("wrong onwhat passed", { "onwhat": onwhat }, funcname)
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
catch (error) {
|
|
199
|
-
logger.error("unexpected error on 'on' function", error, funcname)
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* call to check if datasocket is connected
|
|
205
|
-
*/
|
|
206
|
-
isConnected() {
|
|
207
|
-
return this.ws && this.ws.readyState === WebSocket.OPEN;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* starts ping messages to ws
|
|
212
|
-
*/
|
|
213
|
-
startPing() {
|
|
214
|
-
this.pingInterval = setInterval(() => {
|
|
215
|
-
if (this.isConnected()) {
|
|
216
|
-
this.ws.send("ping");
|
|
217
|
-
}
|
|
218
|
-
}, 1000);
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* stops pinging mechanism
|
|
223
|
-
*/
|
|
224
|
-
stopPing() {
|
|
225
|
-
clearInterval(this.pingInterval);
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* Subscribe to socket.
|
|
230
|
-
* @param {Array|string} towhat - array or string of what you want to subscribe to.
|
|
231
|
-
*/
|
|
232
|
-
subscribe(towhat) {
|
|
233
|
-
const funcname = "subscribe"
|
|
234
|
-
const logger = this.Logger
|
|
235
|
-
try {
|
|
236
|
-
if (this.isConnected()) {
|
|
237
|
-
logger.debug("trying to subscribe to", { "towhat": towhat }, funcname)
|
|
238
|
-
if (towhat.constructor === Array) {
|
|
239
|
-
this.ws.send(JSON.stringify({ "T": "SUB_ORD", "SLIST": towhat, "SUB_T": 1 }));
|
|
240
|
-
}
|
|
241
|
-
else {
|
|
242
|
-
this.ws.send(JSON.stringify({ "T": "SUB_ORD", "SLIST": [towhat], "SUB_T": 1 }));
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
else {
|
|
246
|
-
logger.error("websocket is not connected", { "towhat": towhat }, funcname)
|
|
247
|
-
console.log('Cannot send message. WebSocket is not connected.');
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
catch (error) {
|
|
251
|
-
|
|
252
|
-
logger.error("unexpected error on subscribe function", error, funcname)
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
/**
|
|
257
|
-
* Unsubscribe to socket.
|
|
258
|
-
* @param {Array|string} towhat - array or string of what you want to unsubscribe to.
|
|
259
|
-
*/
|
|
260
|
-
unsubscribe(towhat) {
|
|
261
|
-
const funcname = "unsubscribe"
|
|
262
|
-
const logger = this.Logger
|
|
263
|
-
try {
|
|
264
|
-
if (this.isConnected()) {
|
|
265
|
-
if (towhat.constructor === Array) {
|
|
266
|
-
logger.debug("trying to unsubscribe to", { "towhat": towhat }, funcname)
|
|
267
|
-
this.ws.send(JSON.stringify({ "T": "SUB_ORD", "SLIST": towhat, "SUB_T": -1 }));
|
|
268
|
-
}
|
|
269
|
-
else {
|
|
270
|
-
this.ws.send(JSON.stringify({ "T": "SUB_ORD", "SLIST": [towhat], "SUB_T": -1 }));
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
else {
|
|
274
|
-
logger.error("websocket is not connected", { "towhat": towhat }, funcname)
|
|
275
|
-
console.log('Cannot send message. WebSocket is not connected.');
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
catch (error) {
|
|
279
|
-
logger.error("unexpected error on unsubscribe function", error, funcname)
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
* call to close the datasocket.
|
|
285
|
-
*/
|
|
286
|
-
close() {
|
|
287
|
-
if (this.ws && this.isConnected) {
|
|
288
|
-
clearInterval(this.autoreconnectinterval)
|
|
289
|
-
this.stopPing()
|
|
290
|
-
this.ws.close();
|
|
291
|
-
this.isUserClosed = true
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
/**
|
|
296
|
-
* call to enable autoreconnect functionality of websocket.
|
|
297
|
-
*/
|
|
298
|
-
_autoreconnect() {
|
|
299
|
-
if (this.autoreconnectflag){
|
|
300
|
-
var waitSeconds = Math.floor((reconnectiontries+5)/5);
|
|
301
|
-
waitSeconds *= 5;
|
|
302
|
-
|
|
303
|
-
this.autoreconnectinterval = setTimeout(() => {
|
|
304
|
-
if ((!this.isConnected()) && (reconnectiontries < this.maxreconnectiontries)) {
|
|
305
|
-
console.log("trying to reconnect ", reconnectiontries + 1)
|
|
306
|
-
reconnectiontries++
|
|
307
|
-
this.connect()
|
|
308
|
-
} else if (reconnectiontries >= this.maxreconnectiontries) {
|
|
309
|
-
console.log("max autoconnect tries exceeded")
|
|
310
|
-
clearInterval(this.autoreconnectinterval)
|
|
311
|
-
this.stopPing()
|
|
312
|
-
reconnectiontries = 0
|
|
313
|
-
}
|
|
314
|
-
}, (waitSeconds)*1000);
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
/**
|
|
318
|
-
* call to enable autoreconnect functionality of websocket.
|
|
319
|
-
*/
|
|
320
|
-
autoreconnect(reConnectTriesCount=5){
|
|
321
|
-
this.autoreconnectflag=true
|
|
322
|
-
this.maxreconnectiontries=(reConnectTriesCount>50) ? 50 : reConnectTriesCount
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
|
|
1
|
+
const WebSocket = require('ws');
|
|
2
|
+
let Logger = require("../logger/log.js")
|
|
3
|
+
let { Config } = require("../config/config");
|
|
4
|
+
let mapper =require("./mapper.js")
|
|
5
|
+
var reconnectiontries = 0
|
|
6
|
+
const ordstatobj={11: 4, 12: 4, 20: 4, 21: 4, 22: 6, 23: 6, 24: 6, 25: 6, 26: 6, 90: 2,
|
|
7
|
+
91: 1, 92: 5, 93: 5, 94: 5, 51: 6, 52: 6, 53: 6, 54: 6, 55: 6, 61: 6, 62: 6, 63: 6,
|
|
8
|
+
64: 6, 71: 6, 72: 6, 73: 1}
|
|
9
|
+
function datamapper(a,b){
|
|
10
|
+
const result = {};
|
|
11
|
+
|
|
12
|
+
for (const key of Object.keys(b)) {
|
|
13
|
+
if (a.hasOwnProperty(key)) {
|
|
14
|
+
result[b[key]] = a[key];
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return result;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Class to access order update websocket.
|
|
22
|
+
* @class
|
|
23
|
+
*/
|
|
24
|
+
class FyersOrderSocket {
|
|
25
|
+
|
|
26
|
+
constructor(authorizationKey, logpath = undefined ,loggingFlag=true) {
|
|
27
|
+
this.url = Config.Order_SOCKET;
|
|
28
|
+
this.authorizationKey = authorizationKey;
|
|
29
|
+
this.ws = null;
|
|
30
|
+
this.onErrorCallback = null;
|
|
31
|
+
this.onCloseCallback = null;
|
|
32
|
+
this.onMessageCallback = null;
|
|
33
|
+
this.onOpenCallback = null;
|
|
34
|
+
this.orderscallback = null;
|
|
35
|
+
this.LogPath = logpath;
|
|
36
|
+
this.Logger = new Logger(this.LogPath,loggingFlag);
|
|
37
|
+
this.positionscallback = null;
|
|
38
|
+
this.tradescallback = null;
|
|
39
|
+
this.isPingEnabled = true;
|
|
40
|
+
this.pingInterval = null;
|
|
41
|
+
this.autoreconnectinterval = null;
|
|
42
|
+
this.orderUpdates = "orders"
|
|
43
|
+
this.tradeUpdates = "trades"
|
|
44
|
+
this.positionUpdates = "positions"
|
|
45
|
+
this.edis="edis"
|
|
46
|
+
this.pricealerts="pricealerts"
|
|
47
|
+
this.isUserClosed=false
|
|
48
|
+
this.autoreconnectflag=false
|
|
49
|
+
this.maxreconnectiontries = 0
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* called to connect to the order socket
|
|
54
|
+
*/
|
|
55
|
+
connect() {
|
|
56
|
+
const funcname = "connect"
|
|
57
|
+
const logger = this.Logger
|
|
58
|
+
try {
|
|
59
|
+
logger.debug("initalizing connection to socket", { "accesstoken": this.authorizationKey }, funcname)
|
|
60
|
+
this.ws = new WebSocket(this.url, {
|
|
61
|
+
headers: {
|
|
62
|
+
Authorization: this.authorizationKey,
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
this.ws.binaryType = 'arraybuffer';
|
|
66
|
+
this.ws.on('error', (error) => {
|
|
67
|
+
if (this.onErrorCallback) {
|
|
68
|
+
this.onErrorCallback(error);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
console.log("error occoured", error)
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
this.ws.on('close', (event) => {
|
|
76
|
+
this.stopPing()
|
|
77
|
+
if (this.onCloseCallback) {
|
|
78
|
+
this.onCloseCallback(event);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
console.log("ws closed", event)
|
|
82
|
+
if(!this.isUserClosed&&(reconnectiontries<this.maxreconnectiontries)){
|
|
83
|
+
this._autoreconnect()
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
this.ws.on('message', (message) => {
|
|
89
|
+
const data = message.toString();
|
|
90
|
+
if (data === 'pong') {
|
|
91
|
+
return
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
var parseddata = JSON.parse(data)
|
|
95
|
+
if (parseddata.hasOwnProperty("orders")) {
|
|
96
|
+
var orderdata=datamapper(parseddata["orders"],mapper.orders)
|
|
97
|
+
orderdata.status=ordstatobj[orderdata.status]
|
|
98
|
+
orderdata['orderNumStatus']=orderdata.id+':'+orderdata.status
|
|
99
|
+
if (this.orderscallback) {
|
|
100
|
+
this.orderscallback({"s":"ok","orders":orderdata})
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
console.log("Orders_internal", {"s":"ok","orders":orderdata})
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
else if (parseddata.hasOwnProperty("positions")) {
|
|
107
|
+
var positiondata=datamapper(parseddata["positions"],mapper.position)
|
|
108
|
+
if (this.positionscallback) {
|
|
109
|
+
this.positionscallback({"s":"ok","positions":positiondata})
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
console.log("positions_internal", {"s":"ok","positions":positiondata})
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
else if (parseddata.hasOwnProperty("trades")) {
|
|
116
|
+
var tradebookdata=datamapper(parseddata["trades"],mapper.tradebook)
|
|
117
|
+
if (this.tradescallback) {
|
|
118
|
+
this.tradescallback({"s":"ok","trades":tradebookdata})
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
console.log("trades_internal", {"s":"ok","trades":tradebookdata})
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
if (this.onMessageCallback) {
|
|
126
|
+
this.onMessageCallback(parseddata);
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
console.log("message_internal", parseddata)
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
this.ws.on('open', () => {
|
|
137
|
+
reconnectiontries = 0
|
|
138
|
+
if (this.onOpenCallback) {
|
|
139
|
+
this.onOpenCallback();
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
console.log("connected")
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (this.isPingEnabled) {
|
|
146
|
+
this.startPing();
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
catch (error) {
|
|
151
|
+
console.log(`Error occured ${funcname}: ${error}`)
|
|
152
|
+
logger.error("unexpected error on connect function", error, funcname)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* used to define onmessage,onerror,onopen,onclose for websocket.
|
|
158
|
+
* @param {string} onwhat - defines for what the callback function is.
|
|
159
|
+
* @param {Function} callback - the callback function.
|
|
160
|
+
* @throws error message if onwhat is not valid.
|
|
161
|
+
*/
|
|
162
|
+
on(onwhat, callback) {
|
|
163
|
+
const funcname = "on"
|
|
164
|
+
const logger = this.Logger
|
|
165
|
+
try {
|
|
166
|
+
if (onwhat === 'error') {
|
|
167
|
+
this.onErrorCallback = callback;
|
|
168
|
+
}
|
|
169
|
+
else if (onwhat === 'general') {
|
|
170
|
+
this.onMessageCallback = callback;
|
|
171
|
+
}
|
|
172
|
+
else if (onwhat === 'connect') {
|
|
173
|
+
this.onOpenCallback = callback;
|
|
174
|
+
}
|
|
175
|
+
else if (onwhat === 'close') {
|
|
176
|
+
const socketonclose = function (event){
|
|
177
|
+
callback(event)
|
|
178
|
+
if(!this.isUserClosed&&(reconnectiontries<this.maxreconnectiontries)){
|
|
179
|
+
this._autoreconnect()
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
this.onCloseCallback = socketonclose;
|
|
183
|
+
}
|
|
184
|
+
else if (onwhat === 'orders') {
|
|
185
|
+
this.orderscallback = callback;
|
|
186
|
+
}
|
|
187
|
+
else if (onwhat === 'trades') {
|
|
188
|
+
this.tradescallback = callback;
|
|
189
|
+
}
|
|
190
|
+
else if (onwhat === 'positions') {
|
|
191
|
+
this.positionscallback = callback;
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
console.log("incorrect value passed", onwhat)
|
|
195
|
+
logger.error("wrong onwhat passed", { "onwhat": onwhat }, funcname)
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
catch (error) {
|
|
199
|
+
logger.error("unexpected error on 'on' function", error, funcname)
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* call to check if datasocket is connected
|
|
205
|
+
*/
|
|
206
|
+
isConnected() {
|
|
207
|
+
return this.ws && this.ws.readyState === WebSocket.OPEN;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* starts ping messages to ws
|
|
212
|
+
*/
|
|
213
|
+
startPing() {
|
|
214
|
+
this.pingInterval = setInterval(() => {
|
|
215
|
+
if (this.isConnected()) {
|
|
216
|
+
this.ws.send("ping");
|
|
217
|
+
}
|
|
218
|
+
}, 1000);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* stops pinging mechanism
|
|
223
|
+
*/
|
|
224
|
+
stopPing() {
|
|
225
|
+
clearInterval(this.pingInterval);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Subscribe to socket.
|
|
230
|
+
* @param {Array|string} towhat - array or string of what you want to subscribe to.
|
|
231
|
+
*/
|
|
232
|
+
subscribe(towhat) {
|
|
233
|
+
const funcname = "subscribe"
|
|
234
|
+
const logger = this.Logger
|
|
235
|
+
try {
|
|
236
|
+
if (this.isConnected()) {
|
|
237
|
+
logger.debug("trying to subscribe to", { "towhat": towhat }, funcname)
|
|
238
|
+
if (towhat.constructor === Array) {
|
|
239
|
+
this.ws.send(JSON.stringify({ "T": "SUB_ORD", "SLIST": towhat, "SUB_T": 1 }));
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
this.ws.send(JSON.stringify({ "T": "SUB_ORD", "SLIST": [towhat], "SUB_T": 1 }));
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
logger.error("websocket is not connected", { "towhat": towhat }, funcname)
|
|
247
|
+
console.log('Cannot send message. WebSocket is not connected.');
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
catch (error) {
|
|
251
|
+
|
|
252
|
+
logger.error("unexpected error on subscribe function", error, funcname)
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Unsubscribe to socket.
|
|
258
|
+
* @param {Array|string} towhat - array or string of what you want to unsubscribe to.
|
|
259
|
+
*/
|
|
260
|
+
unsubscribe(towhat) {
|
|
261
|
+
const funcname = "unsubscribe"
|
|
262
|
+
const logger = this.Logger
|
|
263
|
+
try {
|
|
264
|
+
if (this.isConnected()) {
|
|
265
|
+
if (towhat.constructor === Array) {
|
|
266
|
+
logger.debug("trying to unsubscribe to", { "towhat": towhat }, funcname)
|
|
267
|
+
this.ws.send(JSON.stringify({ "T": "SUB_ORD", "SLIST": towhat, "SUB_T": -1 }));
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
this.ws.send(JSON.stringify({ "T": "SUB_ORD", "SLIST": [towhat], "SUB_T": -1 }));
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
else {
|
|
274
|
+
logger.error("websocket is not connected", { "towhat": towhat }, funcname)
|
|
275
|
+
console.log('Cannot send message. WebSocket is not connected.');
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
catch (error) {
|
|
279
|
+
logger.error("unexpected error on unsubscribe function", error, funcname)
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* call to close the datasocket.
|
|
285
|
+
*/
|
|
286
|
+
close() {
|
|
287
|
+
if (this.ws && this.isConnected) {
|
|
288
|
+
clearInterval(this.autoreconnectinterval)
|
|
289
|
+
this.stopPing()
|
|
290
|
+
this.ws.close();
|
|
291
|
+
this.isUserClosed = true
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* call to enable autoreconnect functionality of websocket.
|
|
297
|
+
*/
|
|
298
|
+
_autoreconnect() {
|
|
299
|
+
if (this.autoreconnectflag){
|
|
300
|
+
var waitSeconds = Math.floor((reconnectiontries+5)/5);
|
|
301
|
+
waitSeconds *= 5;
|
|
302
|
+
|
|
303
|
+
this.autoreconnectinterval = setTimeout(() => {
|
|
304
|
+
if ((!this.isConnected()) && (reconnectiontries < this.maxreconnectiontries)) {
|
|
305
|
+
console.log("trying to reconnect ", reconnectiontries + 1)
|
|
306
|
+
reconnectiontries++
|
|
307
|
+
this.connect()
|
|
308
|
+
} else if (reconnectiontries >= this.maxreconnectiontries) {
|
|
309
|
+
console.log("max autoconnect tries exceeded")
|
|
310
|
+
clearInterval(this.autoreconnectinterval)
|
|
311
|
+
this.stopPing()
|
|
312
|
+
reconnectiontries = 0
|
|
313
|
+
}
|
|
314
|
+
}, (waitSeconds)*1000);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* call to enable autoreconnect functionality of websocket.
|
|
319
|
+
*/
|
|
320
|
+
autoreconnect(reConnectTriesCount=5){
|
|
321
|
+
this.autoreconnectflag=true
|
|
322
|
+
this.maxreconnectiontries=(reConnectTriesCount>50) ? 50 : reConnectTriesCount
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
326
|
module.exports = FyersOrderSocket
|