简体   繁体   English

无法通过binance Websockets获取socket数据

[英]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.它正在与交换 websocket 之一一起工作,但不与此 websocket 一起工作。 Any suggestions?有什么建议?

websocket reference: https://github.com/binance-exchange/binance-official-api-docs/blob/master/web-socket-streams.md websocket 参考: 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. Send 接受 JSON 格式,我将 msg 更改为 object,将数组传递给 params 并添加 /ws 如上所述。

试试

var ws = new WebSocket("wss://stream.binance.com:9443/ws");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM