简体   繁体   English

Heroku 返回 400 Bad Request Response (Socket.IO Node.js)

[英]Heroku returns 400 Bad Request Response (Socket.IO Node.js)

A 400 Bad Request is returned every time Socket.IO connects to my Heroku app.每次 Socket.IO 连接到我的 Heroku 应用程序时,都会返回 400 Bad Request。 How do I fix this?我该如何解决?

I have a Node.js web app that uses Socket.IO.我有一个使用 Socket.IO 的 Node.js Web 应用程序。 There are no problems while in local development but when I deployed the app to Heroku, every socket.io connection returns a 400 Bad Request.在本地开发时没有问题,但是当我将应用程序部署到 Heroku 时,每个 socket.io 连接都会返回 400 Bad Request。 I've tried manually setting the transport method to websocket, polling, and both but it still doesn't work.我已经尝试将传输方法手动设置为 websocket、轮询和两者,但它仍然不起作用。 I've also turned on session affinity for my dyno.我还为我的 dyno 开启了会话关联。

In the server I have在服务器中我有

const app = express();
const server = http.createServer(app);
const io = socketIO(server);

const namespace = io.of('/client-web-app');

the client connects through客户端通过

const socket = io('/client-web-app')

as you ordinarily do.就像你通常做的那样。

Browser console logs the following:浏览器控制台记录以下内容:

polling-xhr.js:269 POST https://<app-name>.herokuapp.com/socket.io/?EIO=3&transport=polling&t=Mo5GHwF&sid=7Pn_tN47tE2NqFKMAAAc 400 (Bad Request)

there is also a GET and websocket versions of the request还有一个 GET 和 websocket 版本的请求

Meanwhile, Heroku logs the following:同时,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

replaced my actual application name with <app-name> for reasons.出于某些原因,用<app-name>替换了我的实际应用程序名称。

Found a solution to my own problem.找到了解决我自己问题的方法。 Posting here in case anyone else is having the same problem as mine.在这里发布以防其他人遇到与我相同的问题。 Just explicitly state that you going to use websocket as your transport.只需明确说明您将使用websocket作为传输。

So in your socket.io server:所以在你的 socket.io 服务器中:

const app = express();
const server = http.createServer(app);
const io = socketIO(server, {transports: ['websocket']});

and in your socket.io client:并在您的 socket.io 客户端中:

const socket = io('/client-web-app', {transports: ['websocket']});

It seems Heroku only supports websocket as a socket.io transport. Heroku 似乎只支持websocket作为 socket.io 传输。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM