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

好久不见. 提交于 2019-12-08 11:46:26

问题


I have a Tornado Websocket server which has a dictionary of open connections:

class WebSocketHandler(tornado.websocket.WebSocketHandler):
    def open(self, *args):
        self.id = self.generate_id()
        self.stream.set_nodelay(True)

        # ... DO STUFF ...

        clients[self.id] = {"id": self.id, "time":datetime.now(), "object": self}
        self.write_message("Connection SUCCESSFUL! Thanks for connecting! Your 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 would like to do automatically close all connections which are more than one hour old. So something like this:

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

But I don't know where I should put this check and closing old connections.


回答1:


Please refer to this answer for 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)

        # ... DO STUFF ...

        clients[self.id] = {"id": self.id, "time":datetime.now(), "object": self}
        self.write_message("Connection SUCCESSFUL! Thanks for connecting! Your 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() # you wont even have to iterate over the clients.

Follow the IOLoop of tornado and reference question it will be easy to understand.



来源:https://stackoverflow.com/questions/31289829/how-to-automatically-close-connection-serverside-after-a-certain-time-in-tornado

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!