相关疑难解决方法(0)

WebSocket服务器在python中定期发送消息

我在python中有一个龙卷风web服务器:

import tornado.httpserver
import tornado.websocket
import tornado.ioloop
from tornado.ioloop import IOLoop
import tornado.web
import time
import   threading
import sys
from datetime import datetime
from datetime import timedelta

def sample():
    print 'hiiiii'
    threading.Timer(10, sample).start()

class WSHandler(tornado.websocket.WebSocketHandler):

    def open(self):
        print 'new connection'

    def on_message(self, message):
        self.write_message(message)  
        self.msg('hellooooooo')
        print message

    def msg(self,message):
        self.write_message(message)
        threading.Timer(10, self.msg('in timer')).start()
        print 'in msg'+message
    def on_close(self):
        print 'connection closed'

application = tornado.web.Application([
    (r'/', WSHandler),
])

if __name__ == "__main__":
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(8888)
    interval_ms=120
    main_loop=tornado.ioloop.IOLoop.instance()
main_loop.start()
Run Code Online (Sandbox Code Playgroud)

而客户是

<html> …
Run Code Online (Sandbox Code Playgroud)

python tornado websocket periodic-task

3
推荐指数
1
解决办法
7112
查看次数

标签 统计

periodic-task ×1

python ×1

tornado ×1

websocket ×1