本文共 1696 字,大约阅读时间需要 5 分钟。
难的是还是就地用JS显示出来相关的发布进度。
还好,花了一下午实现了。
可以移植到项目中去罗。。。
websocket.py:
import tornado.ioloopimport tornado.webimport tornado.websocketfrom tornado.ioloop import IOLoopfrom datetime import timedeltaimport timeimport osimport sysimport tornado.httpserverclass WebSocketHandler(tornado.websocket.WebSocketHandler): file_content = "" filename = "test.log" def open(self): pass def update_client(self): self.write_message(self._read_file(self.filename)) def on_message(self, message): print message self.filename = message.split('-')[1] self.update_client() def on_close(self): pass def _read_file(self, filename): print filename, '=================' with open(filename) as f: content = f.read() content_diff = content.replace(self.file_content, '') self.file_content = content return content_diffclass IndexPageHandler(tornado.web.RequestHandler): def get(self): self.render("websockets.html")class Application(tornado.web.Application): def __init__(self): handlers = [ (r'/', IndexPageHandler), (r'/ws', WebSocketHandler) ] settings = dict( template_path=os.path.join(os.path.dirname(__file__), "templates"), static_path=os.path.join(os.path.dirname(__file__), "static"), debug=True ) tornado.web.Application.__init__(self, handlers, **settings)if __name__ == '__main__': ws_app = Application() server = tornado.httpserver.HTTPServer(ws_app) server.listen(8888) tornado.ioloop.IOLoop.instance().start()
websocket.html:
Tornado WebSockets 停止刷新
样子:
转载地址:http://xjpxx.baihongyu.com/