程序偶尔会出错提示:SyntaxError: invalid json (at offset 1) at main (__FILE__:27) 并中断
根据错误提示应该是 client.read() 获取的data数据出错了 JSON无法处理 我这样写容错可以吗?
var data = JSON.parse(msg)
if (!data) {
data = JSON.parse(msg)
}
function main() {
var client = Dial("wss://api.aex.zone/ws/v1|reconnect=true");
client.write('{"cmd": {"type": 2},"type": 1,"pairs": [{ "market": "cnc", "coin": "eth" }]}')
while (true) {
var msg = client.read();
var data = JSON.parse(msg)
var type = data.cmd.type
if (type === 1) {
var CNCBuy = data.bids[0].price
var CNCBuyamount = data.bids[0].amount
var CNCSell = data.asks[0].price
var CNCSellamount = data.asks[0].amount
}
Log(CNCBuy)
/*无法跨域调用变量 而且如果关注多个交易对 还需要判断其它条件才能筛选数据 涉及到后续的交易逻辑判断 需要跨域的变量就更多了 更复杂了 搞了一天了 好累啊*/
}
}