我有一个用于 WebSocket 服务器的简单代码:
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web
import time
class WSHandler(tornado.websocket.WebSocketHandler):
def open(self):
print 'New connection was opened'
self.write_message("Conn!")
def on_message(self, message):
print 'Got :', message
self.write_message("Received: " + message)
def on_close(self):
print 'Conn closed...'
application = tornado.web.Application([
(r'/ws', WSHandler),
])
if __name__ == "__main__":
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(65)
tornado.ioloop.IOLoop.instance().start()
我希望能够向所有已连接的客户端发送消息,但我不知道如何做到,也似乎找不到相关信息。请提供一点帮助好吗?谢谢。
[con.write_message('你好!') for con in self.connections]
- James