How to automatically close connection serverside a

2019-06-12 16:05发布

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条回答
▲ chillily
2楼-- · 2019-06-12 16:28

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.

查看更多
登录 后发表回答