我在javascript中使用websockets连接到API并读取一些数据。
以下是代码:
function processHUOBIAPI(client,exchange_name,exchange_wss,exchange_symbol)
{
// Define constants
const WebSocket = require('ws');
const pako = require('pako');
// Open connection once one is established
var wss = new WebSocket(exchange_wss);
wss.onopen = () =>
{
// Send request to subscribe
var symbol = exchange_symbol.toLowerCase()
wss.send(JSON.stringify(
{
"sub": "market." + symbol + ".kline.1min",
"id": symbol
}
));
};
// Parse channel information and send to Redis
wss.onmessage = (data) =>
{
//typedef data;
console.log("Receive message", data);
let text = pako.inflate(data, {
to: 'string'
});
mssg = JSON.parse(text);
if (mssg.ping) {
wss.send(JSON.stringify({
pong: mssg.ping
}));
} else if (mssg.tick) {
console.log(mssg);
//handle(msg);
} else {
console.log(text);
}
}
在这种情况下,我收到错误:
.... / node_modules / PAKO / LIB / inflate.js:384 if(inflator.err){throw inflator.msg || MSG [inflator.err]; } ^ 未知压缩方法
如果我将“wss.onmessage =(data)=>”更改为“wss.on('message',(data)=> {”并删除额外的')'代码完美的工作。
wss.on和wss.onmessage有什么区别?
这个新手如此欣赏任何指导。如果这是一个愚蠢的人,请提前道歉....