繁体   English   中英

刷新客户端网页时如何重新连接到Web套接字

[英]How to reconnect to a Web Socket when I refresh the client web page

当刷新客户端网页时,我停止从龙卷风套接字服务器接收数据。 如何重新连接到流?

我尝试将连接对象添加到列表中,然后在关闭时将其从列表中删除,但事实证明,刷新页面时,连接永不关闭,根据服务器保持活动状态,但也不会在客户端再接收数据:(

这是我的龙卷风服务器

# python 3
from tornado import web, httpserver, ioloop, websocket, options
from time import time, sleep


class ChannelHandler(websocket.WebSocketHandler):
    """Handler that handles a websocket channel"""
    connections = list()

    @classmethod
    def urls(cls):
        return [(r'/websocket', cls, {})]

    def initialize(self):
        self.channel = None

    def open(self):
        # When Client opens a websocket
        # add the new connnection to connections
        self.connections.append(self)

    def on_message(self, message):
        # Message received on channel
        # keep sending all connected clients the time info
        while True:
            [client.write_message({'time()': str(time())}) for client in self.connections]
            sleep(1)
            print('still sending')

    def on_close(self):
        # Channel is closed
        # delete client from active connections if they close connection
        self.connections.remove(self)
        print('CLOSED connection?')

    def check_origin(self, origin):
        # Override the origin check if needed
        return True


def main():
    # Create tornado application and supply URL routes
    app = web.Application(ChannelHandler.urls())
    # Setup HTTP Server
    http_server = httpserver.HTTPServer(app)
    http_server.listen(8000, 'localhost')
    # Start IO/Event loop
    ioloop.IOLoop.instance().start()


if __name__ == '__main__':
    main()

套接字客户端是

<script type="text/javascript">
    var ws = new WebSocket("ws://localhost:8000/websocket");
    ws.onopen = function () {
        ws.send("Hello, world");
    };
    ws.onmessage = function (evt) {
        console.log(evt.data);
    };
</script>

因此,当刷新cleint网页时,如何继续从套接字服务器接收数据?

while循环阻塞了整个服务器。 您已经在循环中添加了sleep(1)调用来暂停循环一秒钟,但是time.sleep是一个阻塞函数,因此也没有任何帮助。

您需要使用异步替代time.sleep -Tornado的gen.sleep

您还需要将on_message函数转换为协程。

例:

from tornado import gen


async def on_message(...):
    ...
    while True:
        ...
        await gen.sleep(1)
        ...

另外 ,请使用set()而不是list()来存储连接,以免意外添加重复的连接。

在这种情况下,您还需要稍微修改一下代码。 set没有append方法,而是具有add方法。

connections = set()
...
self.connections.add(self) # to add connection
...
self.connections.remove(self) # to remove connetion

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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