Path: blob/master/bitget-node-sdk-api/src/lib/ws/BitgetWsClient.ts
518 views
import {EventEmitter} from 'events';1import {encrypt, encryptRSA, toJsonString} from '../util';2import {API_CONFIG} from '../config';3import WebSocket from 'ws';4import * as Console from 'console';5import {WsLoginReq} from '../model/ws/WsLoginReq';6import {WsBaseReq} from '../model/ws/WsBaseReq';7import {SubscribeReq} from '../model/ws/SubscribeReq';8import {BIZ_CONSTANT} from '../contant';910export abstract class Listenner{11abstract reveice(message:string):void;12}1314export class BitgetWsClient extends EventEmitter {15private websocketUri: string;16private socket?: WebSocket;17private interval?: NodeJS.Timeout | null;18private isOpen?: boolean;19private callBack?: Listenner;20private apiKey!:string;21private apiSecret!:string;22private passphrase!:string;2324constructor(callBack:Listenner,apiKey: string, apiSecret: string, passphrase: string) {25super();26this.websocketUri = API_CONFIG.WS_URL;27this.callBack = callBack;28this.socket = new WebSocket(API_CONFIG.WS_URL, {});29this.apiKey = apiKey;30this.apiSecret = apiSecret;31this.passphrase = passphrase;3233this.socket.on('open', () => this.onOpen());34this.socket.on('close', (code, reason) => this.onClose(code, reason));35this.socket.on('message', data => this.onMessage(data));36}3738login() {39const timestamp = Math.floor(Date.now() / 1000);40let sign = encrypt('GET','/user/verify',null,timestamp,this.apiSecret);41if (API_CONFIG.SIGN_TYPE === BIZ_CONSTANT.RSA) {42sign = encryptRSA('GET','/user/verify',null,timestamp,this.apiSecret);43}4445const wsLoginReq = new WsLoginReq(this.apiKey,this.passphrase,timestamp.toString(),sign);46const args = new Array<WsLoginReq>();47args.push(wsLoginReq);48const request = new WsBaseReq('login',args);49this.send(request);50}5152subscribe(args: SubscribeReq[]) {53const request = new WsBaseReq('subscribe',args);54this.send(request);55}5657unsubscribe(args: SubscribeReq[]) {58const request = new WsBaseReq('unsubscribe',args);59this.send(request);60}6162private send(messageObject: any) {63const that = this;64if (!this.socket) throw Error('socket is not open');65const jsonStr = toJsonString(messageObject);66Console.info('sendInfo:'+jsonStr)6768setInterval(() => {69if (that.isOpen) {70this.socket?.send(jsonStr);71}72}, 1000);73}7475private onOpen() {76this.isOpen = true;77Console.info(`on open Connected to ${this.websocketUri}`);78this.initTimer();79this.emit('open');80}8182private initTimer() {83this.interval = setInterval(() => {84if (this.socket) {85this.socket.send('ping');86}87}, 5000);88}8990private onMessage(data: WebSocket.Data) {91if (typeof data === 'string') {92this.callBack?.reveice(data);93}94}9596private onClose(code: number, reason: string) {97Console.log(`Websocket connection is closed.code=${code},reason=${reason}`);98this.socket = undefined;99if (this.interval) {100clearInterval(this.interval);101this.interval = null;102}103this.emit('close');104}105106close() {107if (this.socket) {108Console.log(`Closing websocket connection...`);109this.socket.close();110if (this.interval) {111clearInterval(this.interval);112this.interval = null;113}114this.socket = undefined;115}116}117}118119