node.js - Socket.io 可以在本地主机上运行,​​但不能在 Heroku 上运行

标签 node.js sockets heroku socket.io

我设法让套接字在我的 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;

最佳答案

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

Apps using Socket.io should enable session affinity. If you plan to use Node’s Cluster module or to scale your app to multiple dynos, you should also follow Socket.io’s multiple-nodes instructions.

heroku features:enable http-session-affinity

关于node.js - Socket.io 可以在本地主机上运行,​​但不能在 Heroku 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60421223/

相关文章:

node.js - 有时 GET 返回 304 而不是 200

c++ - 通过 Berkeley 套接字使用线程通知

mysql - 将 MySQL "get top y for each n"GROUP BY 查询转换为 PostgreSQL

node.js - Heroku 上的 Node 应用程序段错误:std::bad_alloc 和退出代码 134

javascript - 查找文本中的日期

angularjs - 使用 AngularJS 和 NodeJS、MongoDB 进行动态 URL 查询

java - 如何修复 Windows 7 UDP 回复 Linux UDP 连接创建者? Linux 到 Linux 工作但 Linux 到 Windows 7 不工作的地方

node.js - Heroku dynos 上的内存如何工作?

javascript - 我的数据库文件有什么问题?

java - 如何在java中反射(reflect)端口53上的UDP数据包?