node.js - 基于 heroku socket.io 服务器的 heroku 空闲连接超时

标签 node.js heroku socket.io

有时我的套接字服务器会出现以下问题:

这里是 heroku 日志:

heroku[router]: at=error code=H15 desc="Idle connection" method=GET path=/socket.io/1/websocket/RcjG0Zjot1U3uwLWX9SI 
host=www.*********.** request_id=69f5aa39-255b-4871-a928-1548ef2fdd4f 
fwd="217.200.201.210" dyno=web.1 connect=0 service=306473 status=503 bytes=870

我正在使用 socket.io 库版本 0.9.x 和 Node 0.8.x。 它托管在 heroku 上,并激活了 websocket (heroku labs:enable websockets -a myapp)。 我没有使用 xhr-polling

这是我实现套接字服务器的方式:

var httpServer = http.createServer(function(request, response) {
  var pathname = url.parse(request.url).pathname;
  if(pathname == "/") pathname = "index.html";
  if(pathname.indexOf("/chat/") != -1  ) pathname = "index.html";
  var filename = path.join(process.cwd(), 'public', pathname);
  path.exists(filename, function(exists) {
    if(!exists) {
      response.writeHead(404, { "Content-Type": "text/plain" });
      response.write("404 Not Found");
      response.end();
      return;
    }
    response.writeHead(200, { 'Content-Type': mime.lookup(filename) });
    fs.createReadStream(filename, {
      'flags': 'r',
      'encoding': 'binary',
      'mode': 0666,
      'bufferSize': 4 * 1024
    }).addListener("data", function(chunk) {
      response.write(chunk, 'binary');
    }).addListener("close", function() {
      response.end();
    });
  });
});


socket_calls = new socket_calls(webSocket, io, connectedUsers,clients );
webSocket.sockets.on('connection',socket_calls.socket_callback_func );
httpServer.listen(process.env.PORT || 8080, "0.0.0.0");

这个问题似乎只出现在 3g 连接上,一些系统上无法连接到服务器的客户端报告了这个问题。

我尝试使用“网络链接调节器”模拟不良连接,在多个具有 3g 连接和 Wi-Fi 连接的设备上重现该问题,但我无法重现该错误。

最佳答案

我已经解决了。 该问题仅出现在与 TIM 业务的 3g 连接上。

我已经从 heroku 命令行工具中禁用了 websocket 支持,并且我已经在 socket.io 上启用了 xhr-polling。

关于node.js - 基于 heroku socket.io 服务器的 heroku 空闲连接超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23713894/

相关文章:

javascript - 如何使 socket.io 异步方法同步?

python - Socket.IO 适用于 Firefox 和 Edge,但不适用于 Chrome?

node.js - 在nodeJs中执行乘法时返回Nan

javascript - Sequelize - 插入新行时避免删除多对多关联的行

node.js - 如何在 header 中传递参数

ruby-on-rails - 在重定向到 ssl 页面之前出现 ERR_INSECURE_RESPONSE 错误

http - 如何使用 http 而不是 https 在 Heroku 上提供 Golang 应用程序?

node.js - 如何安装 npm 以在 github 上工作

node.js - 如何优化我的 heroku web 应用程序

node.js - 将 socket.io 与集群一起使用?