<script src="https://cdn.bootcss.com/pako/1.0.10/pako.js">
<script type="text/javascript">
var sock = null;
var wsuri = "wss://api.huobi.vn/ws";
window.onload = function() {
console.log("onload");
sock = new WebSocket(wsuri);
sock.onopen = function() {
console.log("connected to " + wsuri);
sock.send(JSON.stringify({"req":"market.btcusdt.kline.1min","symbol":"btcusdt","period":"15min"}));
//sock.send(JSON.stringify({"sub":"market.btcusdt.kline.1min","symbol":"btcusdt","period":"1min"}));
//sock.send(JSON.stringify({"sub":"market.btcusdt.trade.detail","id":"BTC"}));
}
sock.onclose = function(e) {
console.log("connection closed (" + e.code + ")");
}
sock.onmessage = function(evt) {
if(evt.data instanceof Blob){
var result = '';
var reader = new FileReader();
reader.onload = function() {
result = JSON.parse(pako.inflate(reader.result,{to:'string'}));
if(result.ping){
sock.send(JSON.stringify({pong:result.ping}));
}
console.log(result)
}
reader.readAsBinaryString(evt.data);
}
}
};
</script>
网友评论