简体   繁体   English

node.js和heroku上的socket.io,空闲超时错误

[英]socket.io on node.js and heroku, idle timeout error

I'm using socket.io, which works great locally, but once in production with heroku it times out. 我使用的是socket.io,在本地运行良好,但是一旦在heroku中投入生产,它就会超时。 I know this is because heroku will kill/timout a web socket connection if nothing is heard inside the 55 second rolling window. 我知道这是因为,如果在55秒的滚动窗口内听不到任何声音,heroku将会终止/刷新Web套接字连接。

The problem I've got is that I'm setting the socket.io hearbeat interval to 60000 so nothing is heard for 60 seconds, ie outside the 55 second window: 我遇到的问题是,我将socket.io的听音间隔设置为60000,所以60秒内没有听到任何声音,即在55秒的窗口之外:

io.set('heartbeat interval', 60000); //heroku timeout with Idle connection error. 

I need some way of polling the server inside that 55 second window that keeps the connection alive. 我需要在55秒的窗口内轮询服务器以使连接保持活动状态的某种方式。 Changing the heartbeat interval isn't really an option, so I'm not sure how to go about this. 更改心跳间隔并不是真正的选择,因此我不确定如何执行此操作。

In general, you want your router to disconnect idle connections so they don't consume system resources on the backend. 通常,您希望路由器断开空闲连接,以便它们不消耗后端的系统资源。 The challenge is separating actually idle / lost connections and realtime-connected clients that just haven't said anything for a while. 面临的挑战是将实际上闲置/丢失的连接与实时连接的客户端(只是一段时间没有说话)分开。

One solution is just having the clients ping the server within the 55-second window. 一种解决方案是让客户端在55秒的窗口内ping服务器。 An example of that is here: 一个例子在这里:

https://github.com/hunterloftis/websocket-ping/blob/2dd45b100a754ee8b151ab3d16d2ee3787d2dc8e/index.html#L65 https://github.com/hunterloftis/websocket-ping/blob/2dd45b100a754ee8b151ab3d16d2ee3787d2dc8e/index.html#L65

https://github.com/hunterloftis/websocket-ping/blob/2dd45b100a754ee8b151ab3d16d2ee3787d2dc8e/index.html#L80-L83 https://github.com/hunterloftis/websocket-ping/blob/2dd45b100a754ee8b151ab3d16d2ee3787d2dc8e/index.html#L80-L83

Basically just io.emit('ping') on a setInterval . 基本上只是在setIntervalio.emit('ping') That way, as long as the client is alive, it will continue to maintain its connection with the server. 这样,只要客户端还活着,它将继续维持与服务器的连接。

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

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