【发布时间】:2015-11-22 01:14:15
【问题描述】:
我正在使用此代码从源获取连续的无限数据流,对其进行处理,然后将处理后的无限流发送到连接到我的服务器的客户端。问题是龙卷风只支持一个 websocket 连接,但我想将数据转发到连接到我的服务器的所有客户端。如何支持多个客户端?
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web
class WSHandler(tornado.websocket.WebSocketHandler):
def open(self):
print 'new connection'
self.write_message("Hello World")
def readData(self):
while True:
--continue generating data--
self.write_message(generated data)
def on_message(self, message):
print 'message received %s' % message
def on_close(self):
print 'connection closed'
application = tornado.web.Application([
(r'/ws', WSHandler),
])
if __name__ == "__main__":
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8888)
tornado.ioloop.IOLoop.instance().start()
【问题讨论】:
-
您在“生成数据”部分到底在做什么以及如何做?它可能正在那里进行非 asyc 调用。
-
我正在连接到以 xml 格式输出无限数据流的 TLS 服务器。我正在将我需要的数据处理成 JSON。那是生成数据的部分。我想要做的是将该数据发送到连接到我的 websocket 的所有客户端,但我不想为连接到我的 websocket 的每个客户端单独连接到我的 TLS 服务器。一个到 TLS 服务器的连接和生成的数据被发送到连接到 websocket 的所有客户端。让我知道是否需要任何其他细节。 :)