URL: https://api.bqi.com/api/public/ticker
参数: group, 必填, 值 0, 20, 100, 500
说明:
group: 0=all, 20=bqi20
curl "https://api.bqi.com/api/public/ticker?group=0"
response:
{
"Group": 0, //指数类型
"Data": {
"Last": 101.61, //最新指数
"High24H": 102.87, //24小时最高指数,滚动粒度5分钟
"Low24H": 100.68, //24小时最低指数,滚动粒度5分钟
"Change24H": -0.49, //24小时涨跌值,滚动粒度5分钟
"ChangeRate24H": -0.48, //24小时涨跌幅%,滚动粒度5分钟
"Time": 1577322393 //时间戳,秒
},
"Code": 200, //成功则返回200
"Message": "" //错误原因
}
URL: https://api.bqi.com/api/public/tickers
curl "https://api.bqi.com/api/public/tickers"
response:
{
"Data": [{
"Group": 0, //指数类型
"Last": 101.57, //最新指数
"High24H": 102.87, //24小时最高指数,滚动粒度5分钟
"Low24H": 100.68, //24小时最低指数,滚动粒度5分钟
"Change24H": -0.57, //24小时涨跌值,滚动粒度5分钟
"ChangeRate24H": -0.55, //24小时涨跌幅%,滚动粒度5分钟
"Time": 1577322994 //时间戳,秒
}, {
"Group": 20,
"Last": 101.21,
"High24H": 102.4,
"Low24H": 100.36,
"Change24H": -0.47,
"ChangeRate24H": -0.46,
"Time": 1577322994
}, {
"Group": 100,
"Last": 78.46,
"High24H": 126.04,
"Low24H": 77.54,
"Change24H": -23.68,
"ChangeRate24H": -23.18,
"Time": 1577322994
}, {
"Group": 500,
"Last": 104.1,
"High24H": 104.82,
"Low24H": 101.45,
"Change24H": 2.3,
"ChangeRate24H": 2.26,
"Time": 1577322994
}],
"Code": 200, //成功则返回200
"Message": "" //错误原因
}
URL: https://api.bqi.com/api/public/kline
参数: group, 必填, 值 0, 20, 100, 500
参数: granularity, 必填, 值 60, 180, 300, 900, 1800, 3600, 7200, 14400, 21600, 43200, 86400, 604800
参数: begin, 可选, 开始时间(时间戳秒)
参数: end, 可选, 结束时间(时间戳秒)
参数: size, 可选, 返回条数(最大2000条)
说明:
group: 0=all, 20=bqi20
granularity: 1分钟, 3分钟, 5分钟, 15分钟, 30分钟, 1小时, 2小时, 4小时, 6小时, 12小时, 1天, 1周
curl "https://api.bqi.com/api/public/kline?group=0&granularity=60"
response:
{
"Group": 0, //指数类型
"Granularity": 60, //k线粒度
"Data": [
// 格式 [ 时间戳(秒), 开盘, 最高, 最低, 收盘 ]
[1577202900, 102.78, 102.79, 102.78, 102.79],
[1577202960, 102.77, 102.77, 102.7, 102.7],
[1577323680, 101.6, 101.6, 101.6, 101.6]
],
"Code": 200, //成功则返回200
"Message": "" //错误原因
}
URL: https://api.bqi.com/api/public/members
参数: group, 必填, 值 0, 20, 100, 500
参数: lang, 可选, 值 en-us, zh-cn
说明:
group: 0=all, 20=bqi20
结果按照市值排序
curl "https://api.bqi.com/api/public/members?group=0"
response:
{
"Group": 0,
"Data": [{
"CoinCode": "bitcoin", //币代码
"CoinName": "Bitcoin", //币名称
"CoinSymbol": "BTC", //币符号
"NativeName": "Bitcoin", //本土名
"MaxSupply": 21000000, //货币总量
"TotalSupply": 17901187, //现有流通量
"CirculatingSupply": 17901187, //循环供应量
"MarketCap": 128903549134, //市值
"Volume": 6449145269.4336, //成交量
"LastPrice": 7200.838086, //当前价格
"HighPrice24H": 7269.954023, //24小时最高
"LowPrice24H": 7142.958062, //24小时最低
"ChangePrice24H": -36.675691, //24小时涨跌额
"ChangePriceRate24H": -0.51, //24小时涨跌幅
"Time": 1577324733 //时间戳,秒
}, {
"CoinCode": "ethereum",
"CoinName": "Ethereum",
"CoinSymbol": "ETH",
"NativeName": "Ethereum",
"MaxSupply": 107100935,
"TotalSupply": 107100935,
"CirculatingSupply": 107100935,
"MarketCap": 13385208818,
"Volume": 2313965685.4661,
"LastPrice": 124.977516,
"HighPrice24H": 126.500768,
"LowPrice24H": 123.988757,
"ChangePrice24H": -1.283036,
"ChangePriceRate24H": -1.02,
"Time": 1577324733
}],
"Code": 200,
"Message": ""
}
服务器地址:
wss://api.bqi.com/ws/public
基本要求:
收发数据使用 gzip压缩
gzip库 https://github.com/imaya/zlib.js.git
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>WebSocket Demo</title>
</head>
<body>
<script src="gzip.min.js"></script>
<script src="gunzip.min.js"></script>
<script>
var WebSocketAPI = function (onopen) {
var obj = {};
var sock = null;
var dispose_func = null;
var timer = null;
var kline_queue = new Array();
var ticker_queue = new Array();
var cointicker_queue = new Array();
obj.onkline = function (data) { };
obj.onticker = function (data) { };
obj.oncointicker = function (data) { };
var gzip_compress = function (data) {
var plain = new TextEncoder().encode(data);
var gzip = new Zlib.Gzip(plain);
var compressed = gzip.compress();
return compressed;
};
var gzip_decompress = function (data) {
var gunzip = new Zlib.Gunzip(data);
var plain = gunzip.decompress();
return new TextDecoder().decode(plain);
};
var init = function () {
var isclose = false;
sock = new WebSocket('wss://api.bqi.com/ws/public');
sock.onopen = function () {
sock.onmessage = function (ev) {
ev.data.arrayBuffer().then(function (buffer) {
if (null === sock) {
return;
}
try {
var data = gzip_decompress(new Uint8Array(buffer));
data = JSON.parse(data);
if (data.Type === "response") {
if (data.Action === 'ticker') {
var item = ticker_queue.shift();
item.Callback(null, data);
}
else if (data.Action === 'kline') {
var item = kline_queue.shift();
item.Callback(null, data);
}
else if (data.Action === 'coin-ticker') {
var item = cointicker_queue.shift();
item.Callback(null, data);
}
}
else if (data.Type === "event") {
if (data.Action === 'ticker') {
obj.onticker(data);
}
else if (data.Action === 'kline') {
obj.onkline(data);
}
else if (data.Action === 'coin-ticker') {
obj.oncointicker(data);
}
}
}
catch (e) {
sock.close();
}
});
};
var clear = function (ev) {
while (kline_queue.length > 0) {
var item = kline_queue.shift();
item.Callback(new Error("code: " + ev.code + ", reason: " + ev.reason), null);
}
while (ticker_queue.length > 0) {
var item = ticker_queue.shift();
item.Callback(new Error("code: " + ev.code + ", reason: " + ev.reason), null);
}
while (cointicker_queue.length > 0) {
var item = cointicker_queue.shift();
item.Callback(new Error("code: " + ev.code + ", reason: " + ev.reason), null);
}
sock = null;
};
sock.onclose = function (ev) {
if (false == isclose) {
isclose = true;
clear(ev);
if (null === dispose_func) {
if (null !== timer) clearTimeout(timer);
timer = setTimeout(init, 1000);
}
else {
dispose_func();
}
}
};
sock.onerror = function (ev) {
if (false == isclose) {
isclose = true;
clear(ev);
if (null === dispose_func) {
if (null !== timer) clearTimeout(timer);
timer = setTimeout(init, 1000);
}
else {
dispose_func();
}
}
};
onopen();
};
};
// 指数行情
// group = 0, 20, 100, 500
// enable = true | false
// func = (err,data) =>{}
obj.ticker = function (group, enable, func) {
ticker_queue.push({
Action: 'ticker', Group: group, Enable: enable,
Callback: func
});
sock.send(gzip_compress(JSON.stringify({
Action: 'ticker', Group: group, Enable: enable
})));
};
// K线行情
// group = 0, 20, 100, 500
// granularity = 60, 180, 300, 900, 1800, 3600, 7200, 14400, 21600, 43200, 86400, 604800
// enable = true | false
// func = (err,data) =>{}
obj.kline = function (group, granularity, enable, func) {
kline_queue.push({
Action: 'kline', Group: group, Granularity: granularity, Enable: enable,
Callback: func
});
sock.send(gzip_compress(JSON.stringify({
Action: 'kline', Group: group, Granularity: granularity, Enable: enable
})));
};
// 币种行情
// coincode = bitcoin,....
// enable = true | false
// func = (err,data) =>{}
obj.cointicker = function (coincode, enable, func) {
cointicker_queue.push({
Action: 'coin-ticker', CoinCode: coincode, Enable: enable,
Callback: func
});
sock.send(gzip_compress(JSON.stringify({
Action: 'coin-ticker', CoinCode: coincode, Enable: enable
})));
};
// 释放
// func = () =>{}
obj.dispose = function (func) {
if (null !== sock) {
if (WebSocket.CONNECTING === sock.readyState) {
sock.close();
sock = null;
func();
}
else {
dispose_func = func;
sock.close();
}
}
else if (null !== timer) {
clearTimeout(timer);
timer = null;
func();
}
else {
func();
}
};
init();
return obj;
};
</script>
<script>
// 例子
// 掉线会自动重连
var api = new WebSocketAPI(function () {
// 事件
api.onticker = function (data) {
console.log(data);
};
api.onkline = function (data) {
console.log(data);
};
api.oncointicker = function (data) {
console.log(data);
};
// 调用接口
api.ticker(20, true, function (err, data) {
console.log(data);
});
api.ticker(100, true, function (err, data) {
console.log(data);
});
api.kline(20, 300, true, function (err, data) {
console.log(data);
});
api.kline(20, 900, true, function (err, data) {
console.log(data);
});
api.cointicker('bitcoin', true, function (err, data) {
console.log(data);
});
api.cointicker('ethereum', true, function (err, data) {
console.log(data);
});
api.cointicker('eos', true, function (err, data) {
console.log(data);
});
api.cointicker('eos', false, function (err, data) {
console.log(data);
});
//关闭
/*api.dispose(function () {
console.log("close");
});*/
});
</script>
</body>
</html>