Disable an event in a long tornado chat poll

I am working on a chat application that uses Tornado on the backend. I used a long poll to get the number of online users and any new chat message available on the server. I opened two long contacts with the server.

It is very easy to detect when the user is connected to the network. I encoded this class, which maintains a list of (user) users who are logged on to the network. This list is easily sent to any client requesting online users (as you can see in the code below). But when the user goes offline, I do not know that, and therefore, the windowsill (user) remains the same. Therefore, even if someone goes offline, I cannot update the list of users. Please help me figure out a way by which I can delete users who are not connected (using a lengthy survey) and update the list of users.
I heard that I have to apply a certain type of polling mechanism to the list user, since the disconnect event is disabled in the tornado, unlike node.js. Please help me implement this polling mechanism. The response to the code would be much appreciated.

class OnlineHandler(BaseHandler): online = [] identifier = [] user = [] time = [] @tornado.web.asynchronous def post(self): random = self.get_argument("random", None) self.online_user(self.get_current_user(), self.retuser,random) def online_user(self, u, callback, random): cls = OnlineHandler if u not in cls.user: cls.time.append(time.time()) cls.user.append(u) else: index = cls.user.index(u) cls.time[index] = time.time() cls.online.append(callback) if random not in cls.identifier: # new user connected cls.identifier.append(random) logging.info("Sending online list to %r users",len(cls.online)) for callback in cls.online: try: callback(cls.user) except: logging.error("Error in online user callback", exc_info=True) cls.online = [] 
+4
source share
1 answer

I could not see where you delete your cls.user. The following is a long survey code:

class LongPollingHandler (tornado.web.RequestHandler):
@ tornado.web.asynchronous def get (self):
global online, account, last self.connection_closed = False self.user = self.get_argument ("name", no)
if self.user is offline: logging.info ("user:" + self.user) online.append (self.user) http = tornado.httpclient.AsyncHTTPClient ()
appURL = self.request.protocol + ": //" + self.request.host
http.fetch (appURL + "/ internal-poll", self._on_finish)

  '''push to the client''' def _on_finish(self, response): global online, count ,ipollcount gone = self.request.connection.stream.closed() print (self.user +" => " + str(gone)) if gone: online.remove(self.user) return self.write("welcome %s, current online number(s) %s" % (self.user, response.body)) 

self.finish ()

But I just had a problem opening the hosting (this is normal on my local host when the client left), I don’t know if there is a tornado behind the proxy server, whether there will be a proxy server connection after the client. I am also looking for an answer.

+1
source

All Articles