python - How to automatically close connection serverside after a certain time in Tornado Websocket -


i have tornado websocket server has dictionary of open connections:

class websockethandler(tornado.websocket.websockethandler):     def open(self, *args):         self.id = self.generate_id()         self.stream.set_nodelay(true)          # ... stuff ...          clients[self.id] = {"id": self.id, "time":datetime.now(), "object": self}         self.write_message("connection successful! connecting! connection id is: %d :)" % self.id)         print datetime.now()         print "new connection. id: %d" % self.id         print "total number of open connections: %d" % len(clients)      def on_close(self):         print datetime.now()         print "closing connection %d." % self.id          if self.id in clients:             del clients[self.id]         print "number of open connections: %d" % len(clients) 

i automatically close connections more 1 hour old. this:

for client in clients.values():     if (datetime.now() - client["time"])>timedelta(minutes=60):         client["object"].close() 

but don't know should put check , closing old connections.

please refer this answer further clarification

import tornado import datetime class websockethandler(tornado.websocket.websockethandler):     def open(self, *args):         self.id = self.generate_id()         self.stream.set_nodelay(true)         self.timeout = tornado.ioloop.ioloop.current().add_timeout(             datetime.timedelta(minutes=60), self.explicit_close)          # ... stuff ...          clients[self.id] = {"id": self.id, "time":datetime.now(), "object": self}         self.write_message("connection successful! connecting! connection id is: %d :)" % self.id)         print datetime.now()         print "new connection. id: %d" % self.id         print "total number of open connections: %d" % len(clients)      def on_close(self):         print datetime.now()         print "closing connection %d." % self.id          if self.id in clients:             del clients[self.id]         print "number of open connections: %d" % len(clients)      def explicit_close(self):         self.close() # wont have iterate on clients. 

follow ioloop of tornado , reference question easy understand.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -