lichess-org / lila

♞ lichess.org: the forever free, adless and open source chess server ♞
https://lichess.org
GNU Affero General Public License v3.0
15.62k stars 2.28k forks source link

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

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?

niklasf commented 6 years ago

I imagine you figured it out by now or moved on. Otherwise more debug info, like the headers sent would be required.