Đẩy giao dịch Binance lên WeChat theo thời gian thực (thực hành giao thức wss)

Binance Study Webscoket PushMessage
Ngày tạo: 2018-10-20 14:42:39 sửa đổi lần cuối: 2019-07-03 16:27:05
sao chép: 105 Số nhấp chuột: 6234
3
tập trung vào
1444
Người theo dõi

Thông tin giao dịch Bitcoin được chuyển đến WeChat thông qua giao thức websocket, có thể được sử dụng như một bài tập cho giao thức wss. Nguyên tắc chính là cập nhật listenKey mỗi 30 phút, và sau đó đăng ký vào dòng dữ liệu đăng ký tài khoản.

Mã nguồn chiến lược
function main() {
    var listenKey = JSON.parse(HttpQuery('https://api.binance.com/api/v1/userDataStream','',null,'X-MBX-APIKEY:'+APIKEY)).listenKey;
    HttpQuery('https://api.binance.com/api/v1/userDataStream', {method:'DELETE',data:'listenKey='+listenKey}, null,'X-MBX-APIKEY:'+ APIKEY);
    listenKey = JSON.parse(HttpQuery('https://api.binance.com/api/v1/userDataStream','',null,'X-MBX-APIKEY:'+ APIKEY)).listenKey;
    var datastream = Dial("wss://stream.binance.com:9443/ws/"+listenKey, 100);
    var update_listenKey_time =  Date.now()/1000;
    while (true){
        if (Date.now()/1000 - update_listenKey_time > 1800){
            update_listenKey_time = Date.now()/1000;
            HttpQuery('https://api.binance.com/api/v1/userDataStream', {method:'PUT',data:'listenKey='+listenKey}, null,'X-MBX-APIKEY:'+ APIKEY);
            Log('keep listenKey alive');
        }
        var data = datastream.read();
        if(data){
            data = JSON.parse(data);
            if(data.e == 'executionReport' && data.x == 'TRADE'){
                Log(data.S, data.s,  'amount is ', data.l, 'at price:', data.p, '@');
            }
        }
    }
}