社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  tornado

带有WebSockets的Tornado Coroutine不适用于python3

user3732793 • 6 年前 • 1825 次点击  

这个 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
 
1825 次点击  
文章 [ 2 ]  |  最新文章 6 年前
user3732793
Reply   •   1 楼
user3732793    6 年前

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    6 年前

generateMessageToSockets

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

gen.sleep