<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>获取okcoin中国日交易数据</title> <style> table,td,th{ border:1px solid #000; border-collapse:collapse; } </style> </head> <body> <table id="ok_data"></table> <script type="text/javascript"> var wsUri="wss://real.okcoin.cn:10440/websocket/okcoinapi"; //国际站 real.okcoin.cn 替换成 real.okcoin.com var ok_data=document.getElementById(‘ok_data‘); var websocket=new WebSocket(wsUri); var flag=false; var obj={}; var arr=[]; websocket.onopen=function(evt){ /* websocket.send("{‘event‘:‘addChannel‘,‘channel‘:‘ok_btccny_kline_X‘}"); ①x值为:1min,3min,5min,15min,30min,1hour,2hour,4hour,6hour,12hour,day,3day,week ②返回格式:[时间 ,开盘价,最高价,最低价,收盘价,成交量 ] # Request {‘event‘:‘addChannel‘,‘channel‘:‘ok_btccny_kline_1min‘} # Response [{ "channel":"ok_btccny_kline_1min", "data":{ [[1411720800000,2466.98,2467.98,2466.21,2467.97,101.3], [1411720860000,2467.96,2467.98,2467.94,2467.98,5.89]] } }] */ websocket.send("{‘event‘:‘addChannel‘,‘channel‘:‘ok_btccny_kline_day‘}"); } websocket.onmessage=function(evt){ var re_data=evt.data; if(re_data.indexOf(‘[‘)==0){ re_data=re_data.substring(1,re_data.length-1); } var obj=JSON.parse(re_data); if(flag==false)//false为第一次加载 { arr=eval(obj.data); displayData(arr); flag=true; } } function displayData(arr){ var str=""; var times,years,months,date; str+="<tr><th>日期</th><th>开盘</th><th>最高</th><th>最低</th><th>收盘</th><th>成交量</th></tr>"; for(var i=0;i<arr.length-1;i++)//最后一项数据为当日数据,不完整,略去 { str+="<tr>"; for(var j=0;j<arr[i].length;j++) { if(j==0){ times=new Date(arr[i][j]); years=times.getFullYear(); months=times.getMonth()+1; date=times.getDate(); str+="<td>"+years+"."+months+"."+date+"</td>" } else{ str+="<td>"+arr[i][j]+"</td>" } } str+="</tr>"; } ok_data.innerHTML=str; } </script> </body> </html>
时间: 2024-11-10 13:43:08