我的 Tornado 聊天正在丢失消息

my Tornado chat is losing messages

我在龙卷风聊天中丢失了消息,我不知道如何检测消息何时未发送并再次发送消息

有什么方法可以检测连接何时丢失?当 conexión 重新启动时发送消息

这是我的代码

def get(self):
    try:
        json.dumps(MessageMixin.cache)
    except KeyError:
        raise tornado.web.HTTPError(404)




class MessageMixin(object):
    waiters = {}
    cache = {}
    cache_size = 200

    def wait_for_messages(self,cursor=None):
        t = self.section_slug           
        waiters = self.waiters.setdefault(t, [])
        result_future = Future()

        waiters.append(result_future)
        return result_future

    def cancel_wait(self, future):
        t = self.section_slug
        waiters = self.waiters.setdefault(t, [])
        waiters.remove(future)
        # Set an empty result to unblock any coroutines waiting.
        future.set_result([])

    def new_messages(self, message):
        t = self.section_slug
        #cache = self.cache.setdefault(t, [])
        #print t
        #print self.waiters.setdefault(t, [])
        waiters = self.waiters.setdefault(t, [])        
        for future in waiters:
            try:
                if message is not None:
                    future.set_result(message)
            except Exception:

                logging.error("Error in waiter callback", exc_info=True)
        waiters = []
        #self.cache.extend(message)
        #if len(self.cache) > self.cache_size:
            #self.cache = self.cache[-self.cache_size:]


class MessageNewHandler(MainHandler, MessageMixin):
    def post(self, section_slug):
        self.section_slug = section_slug
        post = self.get_argument("html")
        idThread = self.get_argument("idThread")
        isOpPost = self.get_argument("isOpPost")
        arg_not = self.get_argument("arg")
        type_not = self.get_argument("type")
        redirect_to = self.get_argument("next", None)
        message= {"posts": [post],"idThread": idThread,"isOpPost": isOpPost, 
                    "type": type_not,"arg_not": arg_not}
        if redirect_to:
            self.redirect(redirect_to)
        else:
            self.write(post)
        self.new_messages(message)


class MessageUpdatesHandler(MainHandler, MessageMixin):
    @gen.coroutine
    def post(self, section_slug):
        self.section_slug = section_slug
        try:
            self.future = self.wait_for_messages(cursor=self.get_argument("cursor", None))
            data = yield self.future
            if self.request.connection.stream.closed():             
                return          
            self.write(data)
        except Exception:

            raise tornado.web.HTTPError(404)

    def on_connection_close(self):

        self.cancel_wait(self.future)   


class Application(tornado.web.Application):
    def __init__(self):
        handlers = [
            (r"/api/1\.0/stream/(\w+)", MessageUpdatesHandler),
            (r"/api/1\.0/streamp/(\w+)", MessageNewHandler)

        ]
        tornado.web.Application.__init__(self, handlers)


def main():
    tornado.options.parse_command_line()
    app = Application()
    port = int(os.environ.get("PORT", 5000))
    app.listen(port)
    tornado.ioloop.IOLoop.instance().start()


if __name__ == "__main__":
    main()

在最初的聊天演示中,这就是 wait_for_messagescursor 参数的用途:浏览器会告诉您它收到的最后一条消息,因此您可以向它发送此后的每条消息。您需要缓冲消息并可能在 wait_for_messages 中重新发送它们。您在此处引用的代码只会将消息发送到消息传入时连接的那些客户端(请记住,在长轮询中,发送消息会使客户端脱离 "waiting" 状态网络往返的持续时间,所以即使一切正常,客户端也会不断进入和离开等待状态)