【问题标题】:Socket.io is working on Local Host but not HerokuSocket.io 正在本地主机上工作,但不是 Heroku
【发布时间】:2020-06-10 18:10:45
【问题描述】:

我设法让套接字在我的 LocalHost 上工作,但是在上传到 Heroku 时出现以下错误。

Heroku 日志:

2020-02-26T19:18:12.223451+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=polling&t=N22xsN8" host=evening-beach-84352.herokuapp.com request_id=9e085385-d06d-42f8-a02b-741ebdb50139 fwd="99.239.140.92" dyno=web.1 connect=1ms service=7ms status=200 bytes=1156 protocol=https
2020-02-26T19:18:16.268249+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=polling&t=N22xtMN" host=evening-beach-84352.herokuapp.com request_id=560a1283-e9ad-4f98-b65d-c98e3af0d989 fwd="99.239.140.92" dyno=web.1 connect=1ms service=7ms status=200 bytes=1156 protocol=https
2020-02-26T19:18:16.340518+00:00 heroku[router]: at=info method=POST path="/socket.io/?EIO=3&transport=polling&t=N22xtNI" host=evening-beach-84352.herokuapp.com request_id=0747c99f-bfa7-4a48-9dc9-396237ea4afd fwd="99.239.140.92" dyno=web.1 connect=1ms service=17ms status=404 bytes=417 protocol=https
2020-02-26T19:18:16.345722+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=polling&t=N22xtNd" host=evening-beach-84352.herokuapp.com request_id=c28aa30e-c33f-4d2b-809d-7f723c9ad2f8 fwd="99.239.140.92" dyno=web.1 connect=1ms service=11ms status=200 bytes=1156 protocol=https
2020-02-26T19:18:16.394512+00:00 heroku[router]: at=info method=POST path="/socket.io/?EIO=3&transport=polling&t=N22xtOS" host=evening-beach-84352.herokuapp.com request_id=93635a58-6c21-4d1a-81b4-ba67c712bc4a fwd="99.239.140.92" dyno=web.1 connect=1ms service=3ms status=404 bytes=417 protocol=https
2020-02-26T19:18:18.146871+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=polling&t=N22xtpq" host=evening-beach-84352.herokuapp.com request_id=8dc8077a-5719-4f97-bd60-dc2f9585efea fwd="99.239.140.92" dyno=web.1 connect=1ms service=5ms status=200 bytes=1156 protocol=https
2020-02-26T19:18:18.104971+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=polling&t=N22xtp6" host=evening-beach-84352.herokuapp.com request_id=1a135498-5911-4757-90a6-52f22188b3ae fwd="99.239.140.92" dyno=web.1 connect=1ms service=8ms status=200 bytes=1156 protocol=https
2020-02-26T19:18:18.174618+00:00 heroku[router]: at=info method=POST path="/socket.io/?EIO=3&transport=polling&t=N22xtqB" host=evening-beach-84352.herokuapp.com request_id=dce824bb-5243-45be-bd9b-5be3026412ac fwd="99.239.140.92" dyno=web.1 connect=1ms service=3ms status=404 bytes=417 protocol=https
2020-02-26T19:18:18.235090+00:00 heroku[router]: at=info method=POST path="/socket.io/?EIO=3&transport=polling&t=N22xtr2" host=evening-beach-84352.herokuapp.com request_id=44dbc61d-5e40-4283-b602-77107c20bdcd fwd="99.239.140.92" dyno=web.1 connect=1ms service=7ms status=404 bytes=417 protocol=https

浏览器控制台日志:

polling-xhr.js:271 POST https://evening-beach-84352.herokuapp.com/socket.io/?EIO=3&transport=polling&t=N22yWHd 404 (Not Found)

我在我的客户端代码上设置了这个:

import socketIOClient from "socket.io-client";

const socket = socketIOClient("https://evening-beach-84352.herokuapp.com/")

这是我的服务器端代码:

const server = require('http').Server(app);
const socketIO = require('socket.io')
const io = socketIO(server, { origin: "*:*" });

const PORT = process.env.PORT || 8080;

【问题讨论】:

  • 您好,请问您解决了这个问题吗?我也启用了会话亲和性。我收到类似的错误。 heroku[路由器]: at=info method=GET path="/socket.io/?transport=polling host=myawesomeapp.herokuapp.com request_id=5f15281e-14ff-461e-aadc-db3a8243e7ae fwd="73.202.145.46" dyno= web.1 连接=0ms 服务=2ms 状态=400 字节=195 协议=https
  • 如果您看到上述错误...您会看到“轮询”,但我根本没有在 Socket.io 选项中添加轮询。

标签: node.js sockets heroku socket.io


【解决方案1】:

您是否在应用中启用了会话亲和性?来自support article

使用 Socket.io 的应用应该启用会话关联。如果你打算 使用 Node 的集群模块或将您的应用程序扩展到多个测功机,您 还应该遵循 Socket.io 的多节点说明。

heroku features:enable http-session-affinity

【讨论】:

  • 我刚刚添加了,但是没有运气。我仍然得到相同的 404。在我的 Heroku 日志中,它显示 GET 请求的状态为 200,但 POST 请求的状态为 404。所以似乎我的 GET 请求到达了服务器,但没有到达我的 POST?
【解决方案2】:

我最近才开始使用 Heroku,刚刚将 socket.io 添加到我的应用程序中,并且看到了类似的错误。昨天他们走了,今天他们回来了。我的浏览器中有两个标签页 1 个有效,另一个无效...

区别:http 与 https

当您单击 Heroku 中的链接将您带到您的应用程序时,它会导航到 https://myapp.herokuapp.com,但如果我手动导航到 http://myapp.herokuapp.com,所有错误都会突然消失。

现在我想知道一个有效而另一个无效的原因......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-05
    • 2013-07-15
    • 2021-12-01
    • 2019-10-12
    • 2016-11-06
    • 2018-03-11
    • 1970-01-01
    • 2019-12-27
    相关资源
    最近更新 更多