Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bitgetlimited
GitHub Repository: bitgetlimited/v3-bitget-api-sdk
Path: blob/master/bitget-node-sdk-api/src/lib/ws/BitgetWsClient.ts
518 views
1
import {EventEmitter} from 'events';
2
import {encrypt, encryptRSA, toJsonString} from '../util';
3
import {API_CONFIG} from '../config';
4
import WebSocket from 'ws';
5
import * as Console from 'console';
6
import {WsLoginReq} from '../model/ws/WsLoginReq';
7
import {WsBaseReq} from '../model/ws/WsBaseReq';
8
import {SubscribeReq} from '../model/ws/SubscribeReq';
9
import {BIZ_CONSTANT} from '../contant';
10
11
export abstract class Listenner{
12
abstract reveice(message:string):void;
13
}
14
15
export class BitgetWsClient extends EventEmitter {
16
private websocketUri: string;
17
private socket?: WebSocket;
18
private interval?: NodeJS.Timeout | null;
19
private isOpen?: boolean;
20
private callBack?: Listenner;
21
private apiKey!:string;
22
private apiSecret!:string;
23
private passphrase!:string;
24
25
constructor(callBack:Listenner,apiKey: string, apiSecret: string, passphrase: string) {
26
super();
27
this.websocketUri = API_CONFIG.WS_URL;
28
this.callBack = callBack;
29
this.socket = new WebSocket(API_CONFIG.WS_URL, {});
30
this.apiKey = apiKey;
31
this.apiSecret = apiSecret;
32
this.passphrase = passphrase;
33
34
this.socket.on('open', () => this.onOpen());
35
this.socket.on('close', (code, reason) => this.onClose(code, reason));
36
this.socket.on('message', data => this.onMessage(data));
37
}
38
39
login() {
40
const timestamp = Math.floor(Date.now() / 1000);
41
let sign = encrypt('GET','/user/verify',null,timestamp,this.apiSecret);
42
if (API_CONFIG.SIGN_TYPE === BIZ_CONSTANT.RSA) {
43
sign = encryptRSA('GET','/user/verify',null,timestamp,this.apiSecret);
44
}
45
46
const wsLoginReq = new WsLoginReq(this.apiKey,this.passphrase,timestamp.toString(),sign);
47
const args = new Array<WsLoginReq>();
48
args.push(wsLoginReq);
49
const request = new WsBaseReq('login',args);
50
this.send(request);
51
}
52
53
subscribe(args: SubscribeReq[]) {
54
const request = new WsBaseReq('subscribe',args);
55
this.send(request);
56
}
57
58
unsubscribe(args: SubscribeReq[]) {
59
const request = new WsBaseReq('unsubscribe',args);
60
this.send(request);
61
}
62
63
private send(messageObject: any) {
64
const that = this;
65
if (!this.socket) throw Error('socket is not open');
66
const jsonStr = toJsonString(messageObject);
67
Console.info('sendInfo:'+jsonStr)
68
69
setInterval(() => {
70
if (that.isOpen) {
71
this.socket?.send(jsonStr);
72
}
73
}, 1000);
74
}
75
76
private onOpen() {
77
this.isOpen = true;
78
Console.info(`on open Connected to ${this.websocketUri}`);
79
this.initTimer();
80
this.emit('open');
81
}
82
83
private initTimer() {
84
this.interval = setInterval(() => {
85
if (this.socket) {
86
this.socket.send('ping');
87
}
88
}, 5000);
89
}
90
91
private onMessage(data: WebSocket.Data) {
92
if (typeof data === 'string') {
93
this.callBack?.reveice(data);
94
}
95
}
96
97
private onClose(code: number, reason: string) {
98
Console.log(`Websocket connection is closed.code=${code},reason=${reason}`);
99
this.socket = undefined;
100
if (this.interval) {
101
clearInterval(this.interval);
102
this.interval = null;
103
}
104
this.emit('close');
105
}
106
107
close() {
108
if (this.socket) {
109
Console.log(`Closing websocket connection...`);
110
this.socket.close();
111
if (this.interval) {
112
clearInterval(this.interval);
113
this.interval = null;
114
}
115
this.socket = undefined;
116
}
117
}
118
}
119