This repository has been archived by the owner on Sep 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 186
/
BasicClient.ts
367 lines (315 loc) · 11.4 KB
/
BasicClient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* eslint-disable @typescript-eslint/no-unused-vars */
import { EventEmitter } from "events";
import { IClient } from "./IClient";
import { SmartWss } from "./SmartWss";
import { Watcher } from "./Watcher";
import { Market } from "./Market";
export type MarketMap = Map<string, Market>;
export type WssFactoryFn = (path: string) => SmartWss;
export type SendFn = (remoteId: string, market: Market) => void;
/**
* Single websocket connection client with
* subscribe and unsubscribe methods. It is also an EventEmitter
* and broadcasts 'trade' events.
*
* Anytime the WSS client connects (such as a reconnect)
* it run the _onConnected method and will resubscribe.
*/
export abstract class BasicClient extends EventEmitter implements IClient {
public hasTickers: boolean;
public hasTrades: boolean;
public hasCandles: boolean;
public hasLevel2Snapshots: boolean;
public hasLevel2Updates: boolean;
public hasLevel3Snapshots: boolean;
public hasLevel3Updates: boolean;
protected _wssFactory: WssFactoryFn;
protected _tickerSubs: MarketMap;
protected _tradeSubs: MarketMap;
protected _candleSubs: MarketMap;
protected _level2SnapshotSubs: MarketMap;
protected _level2UpdateSubs: MarketMap;
protected _level3SnapshotSubs: MarketMap;
protected _level3UpdateSubs: MarketMap;
protected _wss: SmartWss;
protected _watcher: Watcher;
constructor(
readonly wssPath: string,
readonly name: string,
wssFactory?: WssFactoryFn,
watcherMs?: number,
) {
super();
this._tickerSubs = new Map();
this._tradeSubs = new Map();
this._candleSubs = new Map();
this._level2SnapshotSubs = new Map();
this._level2UpdateSubs = new Map();
this._level3SnapshotSubs = new Map();
this._level3UpdateSubs = new Map();
this._wss = undefined;
this._watcher = new Watcher(this, watcherMs);
this.hasTickers = false;
this.hasTrades = true;
this.hasCandles = false;
this.hasLevel2Snapshots = false;
this.hasLevel2Updates = false;
this.hasLevel3Snapshots = false;
this.hasLevel3Updates = false;
this._wssFactory = wssFactory || (path => new SmartWss(path));
}
//////////////////////////////////////////////
public close() {
if (this._beforeClose) {
this._beforeClose();
}
this._watcher.stop();
if (this._wss) {
this._wss.close();
this._wss = undefined;
}
}
public reconnect() {
this.emit("reconnecting");
if (this._wss) {
this._wss.once("closed", () => this._connect());
this.close();
} else {
this._connect();
}
}
public subscribeTicker(market: Market) {
if (!this.hasTickers) return;
return this._subscribe(market, this._tickerSubs, this._sendSubTicker.bind(this));
}
public unsubscribeTicker(market: Market): Promise<void> {
if (!this.hasTickers) return;
this._unsubscribe(market, this._tickerSubs, this._sendUnsubTicker.bind(this));
}
public subscribeCandles(market: Market) {
if (!this.hasCandles) return;
return this._subscribe(market, this._candleSubs, this._sendSubCandles.bind(this));
}
public unsubscribeCandles(market: Market): Promise<void> {
if (!this.hasCandles) return;
this._unsubscribe(market, this._candleSubs, this._sendUnsubCandles.bind(this));
}
public subscribeTrades(market: Market) {
if (!this.hasTrades) return;
return this._subscribe(market, this._tradeSubs, this._sendSubTrades.bind(this));
}
public unsubscribeTrades(market: Market): Promise<void> {
if (!this.hasTrades) return;
this._unsubscribe(market, this._tradeSubs, this._sendUnsubTrades.bind(this));
}
public subscribeLevel2Snapshots(market: Market) {
if (!this.hasLevel2Snapshots) return;
return this._subscribe(
market,
this._level2SnapshotSubs,
this._sendSubLevel2Snapshots.bind(this),
);
}
public unsubscribeLevel2Snapshots(market: Market): Promise<void> {
if (!this.hasLevel2Snapshots) return;
this._unsubscribe(
market,
this._level2SnapshotSubs,
this._sendUnsubLevel2Snapshots.bind(this),
);
}
public subscribeLevel2Updates(market: Market) {
if (!this.hasLevel2Updates) return;
return this._subscribe(
market,
this._level2UpdateSubs,
this._sendSubLevel2Updates.bind(this),
);
}
public unsubscribeLevel2Updates(market: Market): Promise<void> {
if (!this.hasLevel2Updates) return;
this._unsubscribe(market, this._level2UpdateSubs, this._sendUnsubLevel2Updates.bind(this));
}
public subscribeLevel3Snapshots(market: Market) {
if (!this.hasLevel3Snapshots) return;
return this._subscribe(
market,
this._level3SnapshotSubs,
this._sendSubLevel3Snapshots.bind(this),
);
}
public unsubscribeLevel3Snapshots(market: Market): Promise<void> {
throw new Error("Method not implemented.");
}
public subscribeLevel3Updates(market: Market) {
if (!this.hasLevel3Updates) return;
return this._subscribe(
market,
this._level3UpdateSubs,
this._sendSubLevel3Updates.bind(this),
);
}
public unsubscribeLevel3Updates(market: Market): Promise<void> {
if (!this.hasLevel3Updates) return;
this._unsubscribe(market, this._level3UpdateSubs, this._sendUnsubLevel3Updates.bind(this));
}
////////////////////////////////////////////
// PROTECTED
/**
* Helper function for performing a subscription operation
* where a subscription map is maintained and the message
* send operation is performed
* @param {Market} market
* @param {Map}} map
* @param {String} msg
* @param {Function} sendFn
* @returns {Boolean} returns true when a new subscription event occurs
*/
protected _subscribe(market: Market, map: MarketMap, sendFn: SendFn) {
this._connect();
const remote_id = market.id;
if (!map.has(remote_id)) {
map.set(remote_id, market);
// perform the subscription if we're connected
// and if not, then we'll reply on the _onConnected event
// to send the signal to our server!
if (this._wss && this._wss.isConnected) {
sendFn(remote_id, market);
}
return true;
}
return false;
}
/**
* Helper function for performing an unsubscription operation
* where a subscription map is maintained and the message
* send operation is performed
*/
protected _unsubscribe(market: Market, map: MarketMap, sendFn: SendFn) {
const remote_id = market.id;
if (map.has(remote_id)) {
map.delete(remote_id);
if (this._wss.isConnected) {
sendFn(remote_id, market);
}
}
}
/**
* Idempotent method for creating and initializing
* a long standing web socket client. This method
* is only called in the subscribe method. Multiple calls
* have no effect.
*/
protected _connect() {
if (!this._wss) {
this._wss = this._wssFactory(this.wssPath);
this._wss.on("error", this._onError.bind(this));
this._wss.on("connecting", this._onConnecting.bind(this));
this._wss.on("connected", this._onConnected.bind(this));
this._wss.on("disconnected", this._onDisconnected.bind(this));
this._wss.on("closing", this._onClosing.bind(this));
this._wss.on("closed", this._onClosed.bind(this));
this._wss.on("message", (msg: string) => {
try {
this._onMessage(msg);
} catch (ex) {
this._onError(ex);
}
});
this._beforeConnect();
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this._wss.connect();
}
}
/**
* Handles the error event
* @param {Error} err
*/
protected _onError(err) {
this.emit("error", err);
}
/**
* Handles the connecting event. This is fired any time the
* underlying websocket begins a connection.
*/
protected _onConnecting() {
this.emit("connecting");
}
/**
* This method is fired anytime the socket is opened, whether
* the first time, or any subsequent reconnects. This allows
* the socket to immediate trigger resubscription to relevent
* feeds
*/
protected _onConnected() {
this.emit("connected");
for (const [marketSymbol, market] of this._tickerSubs) {
this._sendSubTicker(marketSymbol, market);
}
for (const [marketSymbol, market] of this._candleSubs) {
this._sendSubCandles(marketSymbol, market);
}
for (const [marketSymbol, market] of this._tradeSubs) {
this._sendSubTrades(marketSymbol, market);
}
for (const [marketSymbol, market] of this._level2SnapshotSubs) {
this._sendSubLevel2Snapshots(marketSymbol, market);
}
for (const [marketSymbol, market] of this._level2UpdateSubs) {
this._sendSubLevel2Updates(marketSymbol, market);
}
for (const [marketSymbol, market] of this._level3UpdateSubs) {
this._sendSubLevel3Updates(marketSymbol, market);
}
this._watcher.start();
}
/**
* Handles a disconnection event
*/
protected _onDisconnected() {
this._watcher.stop();
this.emit("disconnected");
}
/**
* Handles the closing event
*/
protected _onClosing() {
this._watcher.stop();
this.emit("closing");
}
/**
* Fires before connect
*/
protected _beforeConnect() {
//
}
/**
* Fires before close
*/
protected _beforeClose() {
//
}
/**
* Handles the closed event
*/
protected _onClosed() {
this.emit("closed");
}
////////////////////////////////////////////
// ABSTRACT
protected abstract _onMessage(msg: any);
protected abstract _sendSubTicker(remoteId: string, market: Market);
protected abstract _sendSubCandles(remoteId: string, market: Market);
protected abstract _sendUnsubCandles(remoteId: string, market: Market);
protected abstract _sendUnsubTicker(remoteId: string, market: Market);
protected abstract _sendSubTrades(remoteId: string, market: Market);
protected abstract _sendUnsubTrades(remoteId: string, market: Market);
protected abstract _sendSubLevel2Snapshots(remoteId: string, market: Market);
protected abstract _sendUnsubLevel2Snapshots(remoteId: string, market: Market);
protected abstract _sendSubLevel2Updates(remoteId: string, market: Market);
protected abstract _sendUnsubLevel2Updates(remoteId: string, market: Market);
protected abstract _sendSubLevel3Snapshots(remoteId: string, market: Market);
protected abstract _sendUnsubLevel3Snapshots(remoteId: string, market: Market);
protected abstract _sendSubLevel3Updates(remoteId: string, market: Market);
protected abstract _sendUnsubLevel3Updates(remoteId: string, market: Market);
}