facebook / react-native

A framework for building native applications using React
https://reactnative.dev
MIT License
118.74k stars 24.29k forks source link

Expected HTTP 101 response but was '403 Forbidden' in Android WebSocket - React Native #17692

Closed mr-siadati closed 6 years ago

mr-siadati commented 6 years ago

Hi I Making a chess app with react native, i sending & receiving my request with websocket, when i run my app in ios every thing is ok,but when i run my app in android the web socket not open and return " Expected HTTP 101 response but was '403 Forbidden' ".

my create game code :

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,
      );
    };
  };

any one has idea?

react-native-bot commented 6 years ago

@facebook-github-bot no-template

facebook-github-bot commented 6 years ago

Hey @mr-siadati, thanks for posting this! It looks like your issue is missing some required information. Can you please add all the details specified in the Issue Template? This is necessary for people to be able to understand and reproduce your issue. I am going to close this, but please feel free to open a new issue with the additional information provided. Thanks!

How to ContributeWhat to Expect from Maintainers