Created
January 22, 2017 19:08
-
-
Save ignition42/9c541f84c0cab76a2b6c93bdae1598b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let autobahn = require('autobahn'); | |
const URL = 'wss://api.poloniex.com'; | |
const REALM = 'realm1'; | |
let PAIR = 'BTC_ETH'; | |
let MAX_FOLLOWING_SEQ = 150; | |
let main = () => { | |
let connection = new autobahn.Connection({ | |
url: URL, | |
realm: REALM | |
}); | |
connection.onopen = (session, details) => { | |
console.log('Connected to poloniex socket at ' + URL); | |
let seqs = {}; // Updates that will be processed | |
let current = 0; // Current update to be processed | |
// For debugging | |
let lostseq = {}; // Lost sequence numbers | |
let successful = 0; // Total number of updates successfully processed | |
let t0 = null; | |
session.subscribe(PAIR, (args, kwargs, details) => { | |
let seq = kwargs.seq; | |
seqs[seq] = args; | |
if (current == 0) { | |
current = seq; | |
// For debugging | |
t0 = process.hrtime(); | |
} | |
if (seq in lostseq) { | |
// Never happened in a huge amount of tests | |
console.log(seq + ' => {"found": true}'); | |
} | |
let next = current + 1; | |
let length = Object.keys(seqs).length; | |
// Iterate and process all remaining updates | |
while (length != 0 && Object.keys(seqs)[0] <= next) { | |
let key = Object.keys(seqs)[0]; | |
if (key == next) { | |
// Apply the update | |
//applyUpdate(seqs[next]); | |
// Prepare for the next update | |
current = next; | |
next++; | |
// For debugging | |
t0 = process.hrtime(); | |
successful++; | |
} | |
delete seqs[key]; | |
} | |
if (length > MAX_FOLLOWING_SEQ) { | |
let lost = current + 1; | |
lostseq[lost] = true; | |
// NOTE: hrtime is not always accurate since the next 150 updates will be processed so fast because were already received | |
let t1 = process.hrtime(t0); | |
console.log(lost + ' => ' + JSON.stringify({"lost": true, "successful" : successful, "hrtime" : t1})); | |
// Skip the lost update and try the next | |
current++; | |
} | |
}); | |
}; | |
connection.onclose = (reason, details) => { | |
console.log('Poloniex socket connection lost'); | |
}; | |
connection.open(); | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment