预期的HTTP 101响应但是被禁止了#39; 403在Android WebSocket中 - React Native

时间:2018-01-22 13:31:46

标签: android react-native websocket

我做了一个国际象棋应用程序,我发送&用websocket接收我的请求, 当我在ios中运行我的应用程序时,每件事情都可以,但是当我在Android中运行我的应用程序时,Web套接字无法打开并返回"预期的HTTP 101响应但是被禁止了#39; 403 "

我的创建游戏代码:

createGame() {
    const { playConfig } = this.props;

    fetch('https://en.lichess.org/setup/ai', {
      method: 'POST',
      headers: {
        Accept: 'application/vnd.lichess.v2+json',
        'Content-Type': 'application/json',
      },
      body: playConfig,
    })
      .then(res => res.json())
      .then(this.onGameCreated);
  }

  onGameCreated = res => {
    const { game } = this.state;
    const socketUrl = res.url.socket;
    const clientId = Math.random().toString(36).substring(2);
    clearInterval(this.interval);
    this.wsReady = false;
    let url = `wss://socket.lichess.org${socketUrl}?sri=${clientId}&mobile=1`;
    this.ws = new WebSocket(
      url,
    );

    this.ws.onmessage = e => {
      // a message was received
      console.log(`received: ${e.data}`);
      const data = JSON.parse(e.data);

      let moveData;
      let victor;
      if (data.t === 'move' && data.v > game.history().length) {
        moveData = data.d;
      } else if (data.t === 'end') {
        victor = data.d;
      } else if (data.t === 'b') {
        // b for batch
        const first = data.d[0];
        if (first) {
          if (first.d.status && first.d.status.name === 'mate') {
            moveData = first.d;
          }
          if (first.t === 'end') {
            victor = first.d;
          }
          if (first.d.winner) {
            victor = first.d.winner;
          }
        }
      }

      if (victor) {
        dongSound.play();
        this.setState({
          victor,
        });
        this.ws = null;
      } else if (moveData) {
        const { uci, clock } = moveData;
        const castle = moveData.castle;
        let from = uci.substring(0, 2);
        let to = uci.substring(2, 4);

        if (castle && castle.king) {
          from = castle.king[0];
          to = castle.king[1];
        }

        this.board.movePiece(to, from);
        if (clock) {
          this.latestClock = clock;
        }
      }
    };

    this.ws.onerror = e => {
      // an error occurred
      console.log(e.message);
    };

    this.ws.onopen = () => {
      this.wsReady = true;
      dongSound.play();
      this.setState({
        initialized: true,
        userColor: res.player.color === 'white' ? 'w' : 'b',
      });
      console.log('ws open');
      // ping every second
      this.interval = setInterval(
        () => {
          this.sendMessage({
            t: 'p',
            v: game.history().length,
          });
        },
        1000,
      );
    };
  };

任何人都有想法? 提前谢谢你

1 个答案:

答案 0 :(得分:0)

您似乎无权在此网络服务器上打开套接字。 我不认为问题出在您的Java代码中,而是Web服务器配置。