首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Comet连接超时

Comet连接超时
EN

Stack Overflow用户
提问于 2011-12-15 05:08:43
回答 1查看 223关注 0票数 1

我有一个用Twisted编写的简单的comet服务器。它打印出时间戳。我还有一个运行在同一端口上的小型静态the服务器。所服务的页面上的javascript试图通过附加来自comet服务器的时间戳来更新页面。

但是没有显示来自comet服务器的任何内容。使用Chromium中的开发人员工具,我可以看到打开了一个长连接。但是连接超时,并且不会向页面追加任何内容。为什么不动呢?

下面是一个自包含的示例:

代码语言:javascript
复制
from twisted.internet import reactor
from twisted.internet import task
from twisted.web import server
from twisted.web.server import Site
from twisted.web.resource import Resource
import time

class ClockPage(Resource):
    isLeaf = True
    def __init__(self):
        self.presence=[]
        loopingCall = task.LoopingCall(self._print_time)
        loopingCall.start(1, False)
        Resource.__init__(self)

    def render_GET(self, request):
    # The browser won't display any output until it's gotten a minimum
    # number of bytes from the server or something. Hence, junk divs.
        request.write('''<div class="11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"></div><div class="111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111187348937284789374872387847847811111111111111723872187383738271893789217387389737389711111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"></div>''')
        request.write('<b>%s</b><br>' % (time.ctime(),))
        self.presence.append(request)
        return server.NOT_DONE_YET

    def _print_time(self):
        for p in self.presence:
            p.write('<b>%s</b><br>' % (time.ctime(),))

class UpdatePage(Resource):
    def render_GET(self, request):
        return """
        <!doctype html>
<html>
    <head>
        <title>Metastatus</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js";></script>

        <script type="text/javascript">
function addentry(type, msg) {
    "use strict";
    if (type === "new" && msg !== "") {
        $("#entries").prepend(
            "<li>" + msg + "</li>"
        );
    }
}

function waitForMsg() {
    "use strict";
    $.ajax({
        type: "GET",
        url: "http://localhost:8080/clock",

        async: true, /* If set to non-async, browser shows page as "Loading.."*/
        cache: false,
        timeout: 50000, /* Timeout in ms */

        success: function (data) { /* called when request to barge.php completes */
            addentry("new", data); /* Add response to a .msg div (with the "new" class)*/
            setTimeout(
                waitForMsg(), /* Request next message */
                1000 /* ..after 1 seconds */
            );
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            addentry("error", textStatus + " (" + errorThrown + ")");
            setTimeout(
                waitForMsg(), /* Try again after.. */
                "15000"
            ); /* milliseconds (15seconds) */
        }
    });
}

$(document).ready(function () {
    "use strict";
    waitForMsg(); /* Start the inital request */
});
  </script>
   </head>
    <body>
      <h1>Example</h1>
      <ul id="entries"></ul>
    </body>
</html>"""

if __name__ == '__main__':
    root = Resource()
    root.putChild('', UpdatePage())
    root.putChild('clock', ClockPage())
    factory = Site(root)
    reactor.listenTCP(8080, factory)
    reactor.run()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-15 07:34:14

在响应完全交付给客户端之前,您不能依赖于在JavaScript中获取响应数据。此行为因浏览器而异,因此您有时会看到它按您想要的方式工作,而在其他时间则不工作。

您也不能依赖于无限期地保持打开的连接,这主要是由于浏览器和服务器之间的HTTP代理。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8511678

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档