我尝试使用tornado运行一个服务器websocket,我想在循环中发送消息给客户机,而不是在我想要的时候,也不是onmessage。在
这是我现在的代码:
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web
class WSHandler(tornado.websocket.WebSocketHandler):
def open(self):
print 'new connection'
def on_message(self, message):
print 'message received: %s' % message
if message == "data":
self.write_message("message")
# here i want when i receive data from the client, to continue sending data for it until the connection is closed, and in the some time keep accepting other connections
def on_close(self):
print 'connection closed'
def check_origin(self, origin):
return True
application = tornado.web.Application([
(r'/ws', WSHandler),
])
if __name__ == "__main__":
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8888)
myIP = socket.gethostbyname(socket.gethostname())
print '*** Websocket Server Started at %s***' % myIP
tornado.ioloop.IOLoop.instance().start()
这里有一种方法,使用一个循环,在关闭前每秒发送一条消息。最棘手的部分是在连接关闭时取消循环。此版本使用了}。在
Event.wait
的超时参数;其他可选参数包括gen.with_timeout
和{相关问题 更多 >
编程相关推荐