bitmex-simple-ws
Advanced tools
Comparing version 1.0.2 to 1.0.3
107
index.js
@@ -5,2 +5,4 @@ const WebSocket = require('ws'); | ||
const wait = n => new Promise(r => setTimeout(r, n)); | ||
class Connection extends EventEmitter { | ||
@@ -13,2 +15,3 @@ | ||
this.connected = false; | ||
this.reopen = true; | ||
@@ -21,10 +24,3 @@ if(auth && auth.key && auth.secret) { | ||
this.subscription = {}; | ||
this.book = {}; | ||
this.bookTopics = [ | ||
'orderBookL2_25', | ||
'orderBookL2' | ||
]; | ||
this.subscriptions = []; | ||
} | ||
@@ -47,3 +43,22 @@ | ||
reconnect = async () => { | ||
if(!this.reopen) { | ||
return; | ||
} | ||
this.connect(); | ||
await this.afterOpen; | ||
await wait(200); | ||
this.subscriptions.forEach(sub => { | ||
sub.active = false; | ||
this.subscribe(sub.name); | ||
}); | ||
} | ||
disconnect() { | ||
this.reopen = false; | ||
this.ws.disconnect(); | ||
@@ -70,29 +85,34 @@ } | ||
readyHook(); | ||
console.log('opened!'); | ||
console.log('[BITMEX] opened!'); | ||
} | ||
this.ws.onerror = e => console.error('error', e); | ||
this.ws.onclose = e => console.log('onclose', e); | ||
const onError = e => { | ||
if( | ||
e.message === 'Unexpected server response: 403' || | ||
e.message === 'Unexpected server response: 429' | ||
) { | ||
throw new Error(`[BITMEX] received "${.message}" need to back off reconnecting`) | ||
} | ||
this.ws.onmessage = this.handleMessage.bind(this); | ||
console.log(new Date, '[BITMEX] error', e.message); | ||
} | ||
this.ws.on('error', onError) | ||
this.ws.onerror = onError; | ||
this.ws.onclose = async e => { | ||
console.log(new Date, '[BITMEX] close'); | ||
this.emit('close'); | ||
this.connected = false; | ||
await wait(1000); | ||
this.reconnect(); | ||
} | ||
this.ws.onmessage = this.handleMessage; | ||
return this.afterOpen; | ||
} | ||
handleMessage(e) { | ||
handleMessage = e => { | ||
const payload = JSON.parse(e.data); | ||
if(this.bookTopics.includes(payload.table)) { | ||
const { symbol } = payload.data[0]; | ||
const id = `${payload.table}:${symbol}`; | ||
if(this.book[id]) { | ||
this.book[id].handle(payload); | ||
this.emit(id, this.book[id]); | ||
return; | ||
} | ||
} | ||
this.emit('message', payload); | ||
@@ -103,28 +123,25 @@ } | ||
if(this.subscription[topic]) { | ||
let registration; | ||
this.subscriptions.forEach(sub => { | ||
if(sub.name === topic) { | ||
registration = sub; | ||
} | ||
}); | ||
if(registration && registration.active) { | ||
console.log(new Date, '[BITMEX] refusing to subscribe to same topic twice', topic); | ||
return; | ||
} | ||
if(!registration) { | ||
this.subscriptions.push({name: topic, active: true}); | ||
} else if(!registration.active) { | ||
registration.active = true; | ||
} | ||
this.ws.send(`{"op": "subscribe", "args": ["${topic}"]}`); | ||
this.subscription[topic] = true; | ||
} | ||
// watchBook(symbol, topic = 'orderBookL2') { | ||
// if(!this.bookTopics.includes(topic)) { | ||
// throw new Error('This book topic is not supported'); | ||
// } | ||
// | ||
// const id = `${topic}:${symbol}`; | ||
// | ||
// if(this.book[id]) { | ||
// return; | ||
// } | ||
// | ||
// this.book[id] = new Book(id); | ||
// this.rawSubscribe(id); | ||
// } | ||
} | ||
module.exports = Connection; |
{ | ||
"name": "bitmex-simple-ws", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "", | ||
@@ -25,5 +25,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"lodash": "^4.17.11", | ||
"ws": "^7.0.0" | ||
} | ||
} |
@@ -7,3 +7,2 @@ # bitmex-simple-ws | ||
- Handle reconnects | ||
- Add heartbeat | ||
@@ -10,0 +9,0 @@ |
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
5288
1
115
11
- Removedlodash@^4.17.11
- Removedlodash@4.17.21(transitive)