def quit(): # Only send a message if we were already online. if g.user_id is None or "chat_id" not in request.form: abort(400) try: g.chat_id = int(request.form["chat_id"]) except ValueError: abort(400) if disconnect(g.redis, g.chat_id, g.session_id): db_connect() get_chat_user() send_quit_message(g.db, g.redis, g.chat_user, g.user, g.chat) return "", 204
def on_close(self): # Unsubscribe here and let the exit callback handle disconnecting. if hasattr(self, "redis_client"): self.redis_client.unsubscribe(self.redis_client.subscribed) if self.joined and disconnect(redis, self.chat_id, self.id): try: send_quit_message(self.db, redis, *self.get_chat_user()) except NoResultFound: send_userlist(self.db, redis, self.chat) self.db.commit() self.set_typing(False) print "socket closed:", self.id redis.srem("chat:%s:sockets:%s" % (self.chat_id, self.session_id), self.id) sockets.remove(self)
# Make sure a message is sent every 25 seconds so the long poll requests # don't time out. # XXX INCREASE THIS TO SEVERAL MINUTES for chat_id in redis.zrangebyscore("longpoll_timeout", 0, current_time): redis.publish("channel:%s" % chat_id, '{"messages":[]}') if redis.hlen("chat:%s:online" % chat_id) != 0: redis.zadd("longpoll_timeout", time.time() + 25, chat_id) else: redis.zrem("longpoll_timeout", chat_id) # And do the reaping. for dead in redis.zrangebyscore("chats_alive", 0, current_time): print current_time, "Reaping ", dead chat_id, session_id = dead.split("/") user_id = redis.hget("chat:%s:online" % chat_id, session_id) disconnected = disconnect(redis, chat_id, session_id) # Only send a timeout message if they were already online. if not disconnected: print "Not sending timeout message." continue try: dead_chat_user = ( db.query(ChatUser) .filter(and_(ChatUser.user_id == user_id, ChatUser.chat_id == chat_id)) .options(joinedload(ChatUser.chat), joinedload(ChatUser.user)) .one() ) except NoResultFound: print "Unable to find ChatUser (chat %s, user %s)." % (chat_id, user_id) continue if dead_chat_user.computed_group == "silent" or dead_chat_user.chat.type in ("pm", "roulette"):