简体   繁体   中英

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

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.

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.

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