我正在使用ws包连接到poloniex web套接字,使用以下代码:
const WS = require('ws');
const ws = new WS('wss://api2.poloniex.com');
ws.on('open', () => {
ws.send(JSON.stringify({
command: 'subscribe',
channel: 'USDT_BTC'
}));
});
ws.on('message', (msg) => {
console.log(JSON.parse(msg));
ws.close(1000);
});
ws.on('close', (code) => {
console.log(`closing with code ${code}`);
});
ws.on('error', (error) => {
console.error(error);
});
然而,我得到以下输出:
RangeError: Invalid WebSocket frame: MASK must be clear
at Receiver.getInfo (/home/clecio/Programming/test-poloniex/node_modules/ws/lib/receiver.js:299:14)
at Receiver.startLoop (/home/clecio/Programming/test-poloniex/node_modules/ws/lib/receiver.js:136:22)
at Receiver._write (/home/clecio/Programming/test-poloniex/node_modules/ws/lib/receiver.js:83:10)
at writeOrBuffer (internal/streams/writable.js:358:12)
at Receiver.Writable.write (internal/streams/writable.js:303:10)
at TLSSocket.socketOnData (/home/clecio/Programming/test-poloniex/node_modules/ws/lib/websocket.js:1116:35)
at TLSSocket.emit (events.js:400:28)
at addChunk (internal/streams/readable.js:293:12)
at readableAddChunk (internal/streams/readable.js:267:9)
at TLSSocket.Readable.push (internal/streams/readable.js:206:10) {
code: 'WS_ERR_UNEXPECTED_MASK',
[Symbol(status-code)]: 1002
}
closing with code 1006
有人知道这个错误是什么意思吗?我尝试了类似的代码与类似的服务器,我没有得到任何错误。同样,如果我不发送任何代码到函数ws.close()
,我不会得到任何错误。有人知道为什么会这样吗?
我是在尝试用c#实现我自己的WebSocket时学到的。"掩码"必须是明确的。数据不会被掩盖;意思是省略XOR键,不编码有效负载。
发送"hi"编码:
Mask bit set and 7-bit payload length
vv
{81, 82, 97, 34, 89, 6A, FF, 5D}
^^^^^^^^^^^^^^ ^^^^^^
Mask Key Encoded Payload
发送"hi"unencoded:
注意没有掩码键
Mask bit not set and 7-bit payload length
v
{81, 2, 68, 69}
^^^^^^
Raw Payload
既然你似乎在JS中使用WebSocket库,我想看看是否有一个参数你必须设置,编码你的ws.send()
正在做什么。