简体   繁体   中英

Multiple clients connected to Tornado Server

I am using this code to get a continuous infinite stream of data from a source, process it and then send the processed infinite stream to the clients connected to my server. The problem is that tornado is only supporting one websocket connection but I want to forward the data to all the clients connected to my server. How do I support multiple clients?

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()

是一个我认为与你想要做的事情相匹配的例子。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM