首页 > 解决方案 > SocketIO App 在 Heroku 上表现异常但在本地工作

问题描述

我登录了服务器和客户端应用程序。

组件在单击时会发出 SocketIO 事件。现在第一个客户端(打开到页面的第一个窗口)单击该组件,我在服务器上看到收到事件的日志,在客户端上看到发送事件的日志。

现在有趣的是,当我复制选项卡时,服务器注册了一个新用户(套接字连接成功)但是当我单击组件时,没有消息说事件已发送但组件确实改变了颜色。

我在 setState 的回调中发送 SocketIO 事件和 console.logging 消息。

有问题的组件是一个单元格。

这是我的代码:

handleOnCellClick() {
    if (!this.state.alive) {
      // if dead then proceed add some color to the Cell's life :) and notify server of event
      console.log("cell wasn't alive");
      this.setState(
        {
          r: this.props.defaultRandomColorProps[0],
          g: this.props.defaultRandomColorProps[1],
          b: this.props.defaultRandomColorProps[2],
          alive: true
        },
        () => {
          this.props.socketProps.emit("cellChanged", {
            key: [this.props.cell_row_index, this.props.cell_col_index],
            color: { r: this.state.r, g: this.state.g, b: this.state.b }
          });
          console.log("emitted");
        }
      );
    }
  }

事实上,请在secret-forest-30430.herokuapp.com/上亲自查看。导航到游戏选项卡。它包含一个单元组件的“板”。

我必须再次重申,这整件事在当地有效。这是 github 上项目的链接:https ://github.com/Dobermensch/ExpressTest.git

知道发生了什么吗?

TL;DR:setState 回调不起作用?

编辑:为了退后一步,我正在尝试创建多人康威的生命游戏

标签: javascriptnode.jsreactjsexpresssocket.io

解决方案


推荐阅读