【发布时间】:2019-12-18 21:51:34
【问题描述】:
每次 Socket.IO 连接到我的 Heroku 应用程序时都会返回 400 Bad Request。我该如何解决这个问题?
我有一个使用 Socket.IO 的 Node.js Web 应用程序。在本地开发中没有问题,但是当我将应用程序部署到 Heroku 时,每个 socket.io 连接都会返回 400 Bad Request。我已经尝试手动将传输方法设置为 websocket、轮询和两者,但它仍然不起作用。我还为我的测功机打开了会话亲和性。
在我拥有的服务器中
const app = express();
const server = http.createServer(app);
const io = socketIO(server);
const namespace = io.of('/client-web-app');
客户端通过连接
const socket = io('/client-web-app')
像往常一样。
浏览器控制台记录以下内容:
polling-xhr.js:269 POST https://<app-name>.herokuapp.com/socket.io/?EIO=3&transport=polling&t=Mo5GHwF&sid=7Pn_tN47tE2NqFKMAAAc 400 (Bad Request)
还有一个 GET 和 websocket 版本的请求
与此同时,Heroku 记录以下内容:
at=info method=POST path="/socket.io/?EIO=3&transport=polling&t=Mo5GjRq&sid=wSTQhI8NQRpLbb3xAAAm" host=<app-name>.herokuapp.com request_id=da83df98-0522-4e0a-b0cc-fdbc3304b7f8 fwd="110.54.138.89" dyno=web.1 connect=0ms service=2ms status=400 bytes=296 protocol=https
2019-08-12T09:57:28.339289+00:00 app[web.1]: device disconnected
2019-08-12T09:57:28.628468+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=polling&t=Mo5GjRs&sid=wSTQhI8NQRpLbb3xAAAm" host=<app-name>.herokuapp.com request_id=99ba8299-6f8a-448f-a8ce-d0b34b4a062f fwd="110.54.138.89" dyno=web.1 connect=0ms service=2ms status=400 bytes=228 protocol=https
出于某种原因,将我的实际应用程序名称替换为 <app-name>。
【问题讨论】:
标签: javascript node.js heroku socket.io