为什么 Tornado 无法检测到 IE 中的连接关闭
我尝试通过Tornado+Jquery实现“长轮询”。存在问题:建立轮询后,我关闭 IE(而不是其他浏览器),连接仍然存在,程序继续运行,直到达到退出条件。
服务器
class Poll(BaseHandler):
@tornado.web.asynchronous
def post(self):
self.get_data(callback=self.async_callback(self.on_finish))
print("Exiting from async.")
def get_data(self, callback,t=30):
data={}
data["t"]=datetime.datetime.now()
if self.request.connection.stream.closed():
print "Connection closed"
return
if t:
tornado.ioloop.IOLoop.instance().add_timeout(time.time() + 2, lambda: self.get_data(callback, t-1))
else:
callback(data)
def on_finish(self,data):
self.write(json.dumps(data))
self.finish()
客户端:
$.ajax({url: "/poll",
type: "POST",
dataType: "json",
async:true,
success: function(data){
alert(data)
interval = window.setTimeout(ldy.poll, 0);
}
});
如果我使用 FF 或 Chrome。当我关闭浏览器时,self.request.connection.stream.close() 将变为 TRUE,返回问题,但对于 IE,它始终为 False。这样,如果用户在两个页面之间切换(使用长轮询),服务器的资源很快就会耗尽,每次点击都会挂起,直到一个轮询退出。 为什么?以及如何解决。帮助!帮助! T_T
I try to implement "Long polling" by Tornado+Jquery. There's problem that: after a polling established, I close the IE(not other browser) the connection still alive and the program keep going until reach my exit condition.
server
class Poll(BaseHandler):
@tornado.web.asynchronous
def post(self):
self.get_data(callback=self.async_callback(self.on_finish))
print("Exiting from async.")
def get_data(self, callback,t=30):
data={}
data["t"]=datetime.datetime.now()
if self.request.connection.stream.closed():
print "Connection closed"
return
if t:
tornado.ioloop.IOLoop.instance().add_timeout(time.time() + 2, lambda: self.get_data(callback, t-1))
else:
callback(data)
def on_finish(self,data):
self.write(json.dumps(data))
self.finish()
Client:
$.ajax({url: "/poll",
type: "POST",
dataType: "json",
async:true,
success: function(data){
alert(data)
interval = window.setTimeout(ldy.poll, 0);
}
});
If I use FF or Chrome. When I close the browser, self.request.connection.stream.closed() will turn to TRUE the problem returned but for IE it always False. So that if user switch between two pages(using long polling) the resources of the server quickly exhausted and every click will be hang until one polling exit.
Why? and how to sovle it. Help! Help! T_T
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论