简体   繁体   中英

How to keep a WebSocket connection to Heroku alive?

I have an Python Tornado app hosted at Heroku. It serves WebSocket connections and I want them to be kept alive for a long time (two hours). However, due to Heroku's limitations, it terminates the connection after 55 seconds since the last byte received.
Possible solutions could be:

  • Send keep-alive packets every 50 seconds
    I'd like to stay away from this option and use it in case there's absolutely no other option
  • Include the Connection: Keep-Alive header in WebSocket requests
    Since the client is also written in Python, there's a way to include such a header in every request. And Heroku docs mentioned that such a header would stop the dyno from breaking the connection. But that doesn't seem to be the case - I've sent such a request and the connection was still shut down after a timeout.

Here's some code for reference.
This is the client part where the connection gets established:

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

And this is the Heroku log message:

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=

How can I keep the connections alive?

正如他们关于 Websockets 的文档所述,保持活动数据包实际上是 Heroku 上 Websockets 的唯一解决方案。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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