如何知道客户端在Flask app中断开连接的时间

时间:2017-11-22 18:48:39

标签: python flask connection long-polling

我尝试使用HTTP长轮询,大概是这样的:

        //Rich text box
        richTextBox1.Text = "test test1 test test                              ";

        //Get the first index in the line from where you want to select.
        int firstCharIndex = richTextBox1.Text.IndexOf("test1");

        //Get the line based on character index.
        int line = richTextBox1.GetLineFromCharIndex(firstCharIndex);

        //Get the line it self.
        string headerLineText = richTextBox1.Lines[line];

        //Use second ovrload of select method and pass length of the line as second parameter
        //with the below approach, you can start highlighting from any where in the line.
        richTextBox1.Select(firstCharIndex, headerLineText.Length- firstCharIndex);

这很好用但是如果客户端切断连接,服务器仍然会运行循环。我想用这样的东西替换循环:

@app.route('/get_stuff', methods=["GET"])
def get_stuff():
    while True:
        stuff = database.lookupSomething()
        if stuff:
            return json.dumps(stuff)
        else:
            time.sleep(1)

但是,我找不到任何知道连接是否仍然有效的方法。其他人提出了同样的问题,答案就是使用SocketIO。我不能这样做,宁可使用像上面这样简单的GET请求。有没有办法知道客户是否关闭了他的连接?

0 个答案:

没有答案