javascript - Unable to get the socket data through binance Websockets
问题描述
This is my client-side code base. It is working with one of the exchange websockets but not working with this websocket. Any suggestions?
websocket reference: https://github.com/binance-exchange/binance-official-api-docs/blob/master/web-socket-streams.md
import React, { Component, createContext } from "react";
export const Contx = createContext();
export class ConProvider extends Component {
state = {
coins: [],
digCoin: [],
sou: [],
passSocket: undefined
};
componentDidMount() {
this.socketCall();
}
socketCall = () => {
var ws = new WebSocket("wss://stream.binance.com:9443");
var msg = {
"method": "SUBSCRIBE",
"params": "btcusdt@depth",
"id": 1
};
ws.onopen = () => {
ws.send(msg);
};
ws.onmessage = e => {
const value = e.data;
this.setState({
coins: value
});
};
this.setState({
passSocket: ws
});
};
socketClose = () => {
var wss = this.state.passSocket;
wss.close();
};
render() {
console.log(this.state.coins);
// console.log(this.state.coins)
return (
<Contx.Provider
value={{
...this.state,
socketCall: this.socketCall,
socketClose: this.socketClose
}}
>
{this.props.children}
</Contx.Provider>
);
}
}
解决方案
const ws = new WebSocket('wss://stream.binance.com:9443/ws');
const msg = {
method: 'SUBSCRIBE',
params: ['btcusdt@depth'],
id: 1,
};
ws.onopen = () => {
ws.send(JSON.stringify(msg));
};
Send accepts JSON format, I changed msg to object, passed the array to params and added /ws as mentioned above.
推荐阅读
- c++ - 如何链接到 CMake 中的目标但在本地部分修改某些属性?
- php - 无法从选项值传递值并使用 jQuery AJAX PHP 在表格上显示
- typescript - 如何使用 TypeScript Compiler API 来提取数组的类型
- javascript - 如何在每个单词后重置 javascript 语音识别
- r - 应用地图或修改后如何在小标题中保留变量类型
- python - 如何始终在交互式 matplotlib 窗口中包含完整的图例/额外的艺术家
- automated-tests - cypress 中的工具提示测试
- html - 使用大型自定义拇指图像扩展输入范围的移动
- php - 按性别和年龄对多维数组进行排序
- php - 如何检查多个日期范围之间的重叠日期?