Hello Serge,
after some digging around tornadio2 and sockjs-tornado's source code, I wonder if the following is correct for adding heartbeat support in sockjs-tornado at application level (before sockjs 0.4.0 come out with heartbeat and before you implement it in sockjs-tornado), you comments would be very helpful.
2. to add support for heartbeats coming from sockjs-client (for now, this is implemented at the application level in my app), i just need to add a periodic callback in each connected SockJSConnection instance, for example:
from sockjs.tornado import SockJSConnection, SockJSRouter, periodic
class MyConnection (SockJSConnection):
def on_open(this, info):
...
this._heartbeat_timer = periodic.Callback(self._heartbeat, self._heartbeat_interval, tornado.io_loop)
this._heartbeat_timer.start()
def on_message(this, msg):
if (msg == MyConnection.HEARTBEAT):
this._missed_heartbeats = 0
// other app logic
...
def on_close(this):
...
this._heartbeat_timer.stop()
this._heartbeat_timer = None
def _heartbeat(self):
self._missed_heartbeats += 1
if self._missed_heartbeats > 2:
self.close()
In the code above, I am copying from the existing heartbeat code in sockjs-tornado to use periodic.Callback from sockjs-tornado instead of the usual tornado.ioloop.PeriodicCallback, is this OK? Also, in the callback, i directly reference tornado.io_loop, is this correct? Lastly, I notice that there is an existing method called delay_heartbeat in sockjs-tornado, just wonder what it is used for?
thanks,
Chris