通过websocket协议,将币安成交信息推送到微信,可以作为wss协议的练习。 具体原理是30分钟更新一次listenKey,然后订阅账户订阅的datastream。
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, '@'); } } } }
Eternal Ranger 不能支持其他交易所吗?
小草 从中学习HttpQuery函数的用法,以及币安user data stream使用技巧
小草 思路类似