我想在react中运行此代码
const WebSocketClient = require('websocket').client;
const client = new WebSocketClient();
client.on('connect', function(connection) {
console.log('Connected to Server...');
connection.on('message', function(message) {
if (message.type === 'utf8') {
console.log(message.utf8Data);
}
});
function send(message) {
if (connection.connected) {
connection.sendUTF(message);
}
}
// subscribe with snapshot
send(`{
"type": "subscribe",
"channel": "orderbook",
"requestId": 1,
"payload": {
"baseTokenAddress": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"quoteTokenAddress": "0xe41d2489571d322189246dafa5ebde1f4699f498",
"snapshot": true,
"limit": 100
}
}`);
});
client.connect('wss://ws.radarrelay.com/0x/v0/ws');
2。使用SockJS时 https://www.npmjs.com/package/sockjs 遇到错误 sockjs语法错误:URL的方案必须为'http:'或'https:'。不允许使用“ wss:”。 3.最后使用socket.io-client const message = { “ type”:“订阅”, “ channel”:“订单簿”, “ requestId”:1 “有效载荷”:{ “ baseTokenAddress”:“ 0xe41d2489571d322189246dafa5ebde1f4699f498”, “ quoteTokenAddress”:“ 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2”, “快照”:是的, “限制”:100 } } io.set('origins','*'); var socket = io('wss://ws.radarrelay.com/0x/v0/ws'); socket.on('connect',()=> { console.log('socket open'); socket.send(消息); });
socket.on('event', (data) => {
console.log(data);
});
socket.on('disconnect', () => {
console.log('socket closed');
});
出错 未捕获的TypeError:__WEBPACK_IMPORTED_MODULE_1_socket_io_client ___ default.a.set不是函数 如果我们删除io.set('origins','*'); 我们会收到CORB错误