[英]Comet connection timing out
我有一個用Twisted編寫的簡單彗星服務器。 它打印出時間戳。 我也有一個在同一端口上運行的小型靜態Web服務器。 所提供頁面上的javascript嘗試通過附加來自彗星服務器的時間戳來更新頁面。
但是彗星服務器沒有顯示任何內容。 使用Chromium中的開發人員工具,我可以看到一個長連接已打開。 但是連接超時,並且頁面上沒有任何內容。 為什么不起作用?
這是一個獨立的示例:
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()
在響應完全交付給客戶端之前,您不能依靠在JavaScript中獲取響應數據。 這種行為因瀏覽器而異,因此有時您會看到它可以正常運行,而在其他所有時間都無法運行。
您還不能依賴於無限期保持打開狀態的連接,這主要是由於瀏覽器和服務器之間的HTTP代理所致。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.