我有一个在Heroku上托管的Python Tornado应用程序 . 它提供WebSocket连接,我希望它们能够长时间保持活动状态(两小时) . 但是,由于Heroku的限制,它自收到的最后一个字节以来就是terminates the connection after 55 seconds .
可能的解决办法是:

  • 每50秒发送一次keep-alive数据包
    我'd like to stay away from this option and use it in case there' s aboslutely no 其他选项

  • 在WebSocket请求中包含 Connection: Keep-Alive 标头
    由于客户端也是用Python编写的,所以似乎就是这种情况 - 我发送了这样的请求,并且在超时后连接仍然关闭 .

这里有一些代码供参考 .
这是 Build 连接的客户端部分:

req = HTTPRequest('server_url',
                  headers={'Connection': 'Keep-Alive'})
self.conn = await websocket_connect(req, io_loop = self.ioloop)

这是Heroku日志消息:

at=error code=H15 desc="Idle connection" method=GET path="/" 
host='app_url' request_id=some_id fwd="ip_address" 
dyno=web.1 connect=0ms service=55003ms status=503 bytes=

如何保持连接存活?