Heroku 和 Socket.io、400 个错误请求和许多断开连接

Heroku and Socket.io, 400 Bad requests and many disconnects

所以我有一个可以在 socket.io 上运行的应用程序,我试图让它在 Heroku 上运行,但是在 运行.

上遇到了严重的问题

在控制台日志中,在 chrome 控制台中,大约每秒都会出现大量断开连接。在 chrome 控制台中,我不断收到 400 个错误请求。

https://appname.herokuapp.com/socket.io/?api_key=xxx&EIO=3&transport=polling&t=1424782938420-1&sid=hcTvjdxYePd4kNS5AAAe Failed to load resource: the server responded with a status of 400 (Bad Request)
disconnect
WebSocket connection to 'wss://appname.herokuapp.com/socket.io/?api_key=xxx=3&transport=websocket&sid=hcTvjdxYePd4kNS5AAAe' failed: WebSocket is closed before the connection is established.

所以在我的server.js

var cluster = require('cluster');
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var redis = require('redis');
var redisAdapter = require('socket.io-redis');

var port = process.env.PORT || 3800;

var workers = process.env.WORKERS || require('os').cpus().length;

var redisUrl = process.env.REDISTOGO_URL || 'redis://rediscloud:pw@url:14599';

var redisOptions = require('parse-redis-url')(redis).parse(redisUrl);
var pub = redis.createClient(redisOptions.port, redisOptions.host, {
  detect_buffers: true,
  auth_pass: redisOptions.password
});
var sub = redis.createClient(redisOptions.port, redisOptions.host, {
  detect_buffers: true,
  auth_pass: redisOptions.password
});

io.set('origins', '*:*');

io.adapter(redisAdapter({
  pubClient: pub,
  subClient: sub
}));

console.log('Redis adapter started with url: ' + redisUrl);

/* -------------------------------- */

//APP LOGIC HERE

/* -------------------------------- */

if (cluster.isMaster) {
  console.log('start cluster with %s workers', workers - 1);
  workers--;
  for (var i = 0; i < workers; ++i) {
    var worker = cluster.fork();
    console.log('worker %s started.', worker.process.pid);
  }

  cluster.on('death', function(worker) {
    console.log('worker %s died. restart...', worker.process.pid);
  });
  cluster.on('exit', function(worker) {
    console.log('worker %s exited. restart...', worker.process.pid);
    cluster.fork();
  });
} else {
  start();
}

function start() {
  http.listen(port, function() {
    console.log('listening on *:' + port);
  });
}

在我的client.html

<script src="https://appname.herokuapp.com/socket.io/socket.io.js"></script>
<script>
var socket = io.connect("https://appname.herokuapp.com", { query: "api_key=xxx" });
</script>

我读过粘性会话会导致问题,但大部分信息似乎已经过时,显然 Heroku 现在很好地支持 websockets。

我不是在多个实例上尝试 运行,这只是 运行 heroku ps:scale web=1

甚至不确定 websockets 是否正常工作,它似乎在使用轮询然后也失败了。

所以我的问题是: 为什么我在本地正常工作时会收到所有这些断开连接和 400 个错误请求?我该如何解决?

我遇到了同样的问题。解决方案是更改托管。我不想离开 heroku,因为它真的很方便。但在我浪费大量时间搜索修复后,我尝试将应用程序部署到 OpenShift。 我还添加了修改后的服务器启动,提供显式端口和主机(没有它就无法工作!)

var port = process.env.OPENSHIFT_NODEJS_PORT || 8080,
    ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
...
sockets.setupListeners(server);
server.listen(port, ip_address);

您可以在此处 enter link description here 第 4 条下找到更多详细信息。

我相信你会解决你的头痛。