Javascript Heroku返回400个错误的请求响应(Socket.IO Node.js)

Javascript Heroku返回400个错误的请求响应(Socket.IO Node.js),javascript,node.js,heroku,socket.io,Javascript,Node.js,Heroku,Socket.io,每次Socket.IO连接到我的Heroku应用程序时,都会返回400个错误请求。我该如何解决这个问题 我有一个Node.js web应用程序,它使用Socket.IO。在本地开发时没有问题,但当我将应用程序部署到Heroku时,每个socket.io连接都会返回400个错误请求。我尝试过手动将传输方法设置为websocket、polling以及两者,但仍然不起作用。我还为我的dyno打开了会话关联 在我的服务器上 const app = express(); const server = ht

每次Socket.IO连接到我的Heroku应用程序时,都会返回400个错误请求。我该如何解决这个问题

我有一个Node.js web应用程序,它使用Socket.IO。在本地开发时没有问题,但当我将应用程序部署到Heroku时,每个socket.io连接都会返回400个错误请求。我尝试过手动将传输方法设置为websocket、polling以及两者,但仍然不起作用。我还为我的dyno打开了会话关联

在我的服务器上

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)
 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
还有GET和websocket版本的请求

同时,Heroku记录了以下内容:

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

由于某些原因,将我的实际应用程序名称替换为。

找到了解决我自己问题的方法。在这里张贴,以防其他人有与我相同的问题。只需明确声明您将使用websocket作为传输

因此,在socket.io服务器中:

const app = express();
const server = http.createServer(app);
const io = socketIO(server, {transports: ['websocket']});
在socket.io客户端中:

constsocket=io'/clientwebapp',{transports:['websocket']}


Heroku似乎只支持websocket作为socket.io传输。

找到了解决我自己问题的方法。在这里张贴,以防其他人有与我相同的问题。只需明确声明您将使用websocket作为传输

因此,在socket.io服务器中:

const app = express();
const server = http.createServer(app);
const io = socketIO(server, {transports: ['websocket']});
在socket.io客户端中:

constsocket=io'/clientwebapp',{transports:['websocket']}


Heroku似乎只支持websocket作为socket.io传输。

谢谢,它工作得很好!我将向socketIO docs打开一个关于此的PR。仍然不起作用,我不得不将该站点从heroku移出以添加nginx标题:嗨,我已经为服务器和客户端添加了“websocket”选项。仍然看到类似的错误。你有什么想法吗?谢谢,它工作得很好!我将向socketIO docs打开一个关于此的PR。仍然不起作用,我不得不将该站点从heroku移出以添加nginx标题:嗨,我已经为服务器和客户端添加了“websocket”选项。仍然看到类似的错误。你知道吗?