美文网首页
火币ws数据获取

火币ws数据获取

作者: AlastairYuan | 来源:发表于2019-11-14 17:42 被阅读0次
    
    <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>
    
    
    
    

    相关文章

      网友评论

          本文标题:火币ws数据获取

          本文链接:https://www.haomeiwen.com/subject/sktdictx.html