@binquant/exchange
Advanced tools
Comparing version 0.0.12 to 0.0.13
@@ -457,16 +457,26 @@ "use strict"; | ||
this.isReady = false; | ||
this.isReconnecting = false; | ||
this.connect(); | ||
} | ||
async connect() { | ||
var _a, _b; | ||
this.logger.info('ws connecting'); | ||
// ensure last ws clear | ||
if ((_a = this.ws) === null || _a === void 0 ? void 0 : _a.onClear) { | ||
(_b = this.ws) === null || _b === void 0 ? void 0 : _b.onClear(); | ||
// ensure ws closed | ||
this.ws.terminate(); | ||
} | ||
this.listenKey = await this.options.createListenKey(); | ||
const ws = (this.ws = new WebSocket(this.url)); | ||
ws.on('error', (error) => { | ||
const onError = (error) => { | ||
this.logger.info('ws error:', error); | ||
}); | ||
ws.on('close', (...args) => { | ||
}; | ||
const onClose = (...args) => { | ||
this.logger.info('ws close:', JSON.stringify(args)); | ||
this.reconnect(); | ||
}); | ||
ws.on('open', () => { | ||
if (!this.isReconnecting) { | ||
this.reconnect(); | ||
} | ||
}; | ||
const onOpen = () => { | ||
this.isReady = true; | ||
@@ -480,12 +490,25 @@ if (this.subscribeQueue.length !== null) { | ||
this.logger.info('ws connected(open)'); | ||
}); | ||
ws.on('ping', () => { | ||
}; | ||
const onPing = () => { | ||
this.logger.info('ws ping'); | ||
ws.emit('pong'); | ||
}); | ||
ws.on('message', (message) => { | ||
}; | ||
const onMessage = (message) => { | ||
this.emit('message', JSON.parse(message)); | ||
}); | ||
}; | ||
ws.on('error', onError); | ||
ws.on('close', onClose); | ||
ws.on('open', onOpen); | ||
ws.on('ping', onPing); | ||
ws.on('message', onMessage); | ||
ws.onClear = () => { | ||
ws.off('error', onError); | ||
ws.off('close', onClose); | ||
ws.off('open', onOpen); | ||
ws.off('ping', onPing); | ||
ws.off('message', onMessage); | ||
}; | ||
} | ||
reconnect() { | ||
this.isReconnecting = true; | ||
// reconnect | ||
@@ -497,2 +520,3 @@ // @TODO resubscribes | ||
this.logger.info('ws reconnect all subscribes:', JSON.stringify(this.subscribeQueue)); | ||
this.isReconnecting = false; | ||
// connect | ||
@@ -499,0 +523,0 @@ this.connect(); |
{ | ||
"name": "@binquant/exchange", | ||
"version": "0.0.12", | ||
"version": "0.0.13", | ||
"description": "Exchanges Client", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
93215
2443