Py学习  »  tornado

带有WebSockets的Tornado Coroutine不适用于python3

user3732793 • 5 年前 • 1063 次点击  

这个 HandlerWebsockets 工作正常,只是用目前发送的信息来回复。 messageToSockets(msg) . 但是,这两种方法都试图从Web应用程序的协同程序向WebSocket发送消息,但都不起作用。似乎所有的东西都被这些尝试所阻挡…

class webApplication(tornado.web.Application):
    def __init__(self):
        handlers = [
            (r'/', HandlerIndexPage),
            (r'/websocket', HandlerWebSocket, dict(msg='start')),
        ]

        settings = {
            'template_path': 'templates'
        }
        tornado.web.Application.__init__(self, handlers, **settings)

    @gen.coroutine
    def generateMessageToSockets(self):
        while True:
            msg = str(randint(0, 100))
            print ('new messageToCon: ', msg)
            yield [con.write_message(msg) for con in HandlerWebSocket.connections]
            yield gen.sleep(1.0)

if __name__ == '__main__':

    ws_app = webApplication()
    server = tornado.httpserver.HTTPServer(ws_app)
    port = 9090
    print('Listening on port:' + str(port))
    server.listen(port)
    IOLoop.current().spawn_callback(webApplication.generateMessageToSockets)
    IOLoop.current().set_blocking_log_threshold(0.5)
    IOLoop.instance().start()

这里是WebSockets处理程序

class HandlerWebSocket(tornado.websocket.WebSocketHandler):
    connections = set()

    def initialize(self, msg):
        print('HWS:' + msg)

    def messageToSockets(self, msg):
        print ('return message: ', msg)
        [con.write_message(msg) for con in self.connections]

    def open(self):
            self.connections.add(self)
            print ('new connection was opened')
            pass

    def on_message(self, message):
            print ('from WebSocket: ', message)
            self.messageToSockets(message)

    def on_close(self):
            self.connections.remove(self)
            print ('connection closed')
            pass

在示例、这里的问题、文档等方面,我有点迷茫。因此,对于如何正确启动连续调用WebSocket例程的任何提示,我们都非常感谢。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/33992
 
1063 次点击  
文章 [ 2 ]  |  最新文章 5 年前
user3732793
Reply   •   1 楼
user3732793    5 年前

def generateMessageToSockets():
    while True:
        msg = str(randint(0, 100))
        print ('new messageToCon: ', msg)
        [con.write_message(msg) for con in HandlerWebSocket.connections]
        sleep(1.0)


class WebApplication(tornado.web.Application):
    def __init__(self):
        handlers = [
            (r'/', HandlerIndexPage),
            (r'/websocket', HandlerWebSocket, dict(msg='start')),
        ]

        settings = {
            'template_path': 'templates'
        }
        tornado.web.Application.__init__(self, handlers, **settings)

if __name__ == '__main__':
    tGenarate =  threading.Thread(target=generateMessageToSockets)
    tGenarate.start()
    ws_app = WebApplication()
    server = tornado.httpserver.HTTPServer(ws_app)
    port = 9090
    print('Listening on port:' + str(port))
    server.listen(port)
    ioloop.IOLoop.instance().start()

Ben Darnell
Reply   •   2 楼
Ben Darnell    5 年前

generateMessageToSockets

yield [con.write_message(msg) for con in HandlerWebSocket.connections]
yield gen.moment

gen.sleep