Created
August 28, 2020 09:02
-
-
Save ib0b/ae17557618d6eba5e970c85ee67ff2de to your computer and use it in GitHub Desktop.
Websockets Depth Cache for Binance
This file contains hidden or 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
var WebSocket = require('ws'); | |
var ws; | |
var depthCache = {} | |
function connect() { | |
ws = new WebSocket("wss://fstream.binance.com/ws/bnbusdt@depth@0ms"); | |
ws.on('open', function open() { | |
ws.send('something'); | |
}); | |
ws.on('connection', function incoming(data) { | |
console.log("connected") | |
console.log(data); | |
}); | |
ws.on('ping', function incoming(data) { | |
console.log("ping") | |
console.log(data); | |
}); | |
ws.on('pong', function incoming(data) { | |
console.log("pong") | |
console.log(data); | |
}); | |
ws.on('close', function incoming(data) { | |
console.log("closed") | |
setTimeout(connect, 1000) | |
console.log(data); | |
}); | |
ws.on('message', function incoming(data) { | |
console.log("message", data) | |
try { | |
data = JSON.parse(data) | |
data.a.forEach(ask => { | |
var price = ask[0] | |
var volume = Number(ask[1]) | |
if (volume == 0) { | |
delete cache[price] | |
} else { | |
cache[price] = volume | |
} | |
}); | |
data.b.forEach(ask => { | |
var price = ask[0] | |
var volume = Number(ask[1]) | |
if (volume == 0) { | |
delete cache[price] | |
} else { | |
cache[price] = volume | |
} | |
}); | |
} catch (ex) { | |
console.error("error", ex, data) | |
} | |
}); | |
} | |
connect() | |
var cache = {} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment