调用 Tornado 服务器时保持 ajax 调用处于活动状态
我正在尝试编写我的第一个龙卷风应用程序。
龙卷风的示例示例显示了以下代码,但是似乎一旦“MainHandler”函数返回,客户端与客户端之间的连接就会建立起来。该服务器将丢失。
我希望服务器在数据可用时持续推送数据。
如何保持这一管道畅通?
在客户端,我正在考虑进行 ajax 调用。这行得通吗?
我以为一旦收到数据,ajax调用就结束了。
Facebook确实说客户端使用龙卷风与服务器保持一个线程打开,因此我知道这是可以做到的,我想我在这里正在努力理解一些简单的概念..
import tornado.ioloop
import tornado.web
import time
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("What happens after this call ?")
# while(True): time.sleep(5) <push more data># This would be ugly.. plus blocking(correct ?)
application = tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
我的理解中缺少什么?
I'm trying to write my first tornado application.
The sample example for tornado, shows the below code, however it seems that once the "MainHandler" function returns, the connection between the client & this server will be lost.
I want the server to continuously push data, as and when it becomes available.
How does one keep this pipe open ?
On the client side I'm thinking of doing an ajax call. Will that work ?
I thought once the data is received, the ajax call ends.
Facebook does say that the client keeps a thread open with the server using tornado, hence I know it can be done, I think I'm struggling at some simple concept here..
import tornado.ioloop
import tornado.web
import time
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("What happens after this call ?")
# while(True): time.sleep(5) <push more data># This would be ugly.. plus blocking(correct ?)
application = tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
What is missing in my understanding ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想保持连接打开,您可能需要使用 websocket。类似于:
You might want to use a websocket if you want to keep the connection open. Something like: