【发布时间】:2019-05-24 08:21:51
【问题描述】:
当我刷新客户端网页时,我停止从龙卷风套接字服务器接收数据。如何重新连接到流?
我尝试将连接对象附加到列表中,然后在关闭时从列表中删除连接,但事实证明,当您刷新页面时,连接永远不会关闭,它根据服务器保持活动状态,但它客户端也不再接收数据:(
这是我的龙卷风服务器
# 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()
而socket客户端是
<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>
那么,当我刷新客户端网页时,如何继续从套接字服务器接收数据?
【问题讨论】:
-
on_message内部有一个无限的while循环。由于 Tornado 使用单个线程,因此您基本上完全阻塞了整个服务器。什么都不会发生。永远不要运行无限的while循环。也不要在on_message处理程序中运行定期任务。您需要一个后台异步作业。 -
@freakish Inifinite
while如果循环内的代码使用yield或await语法以使循环暂停并且IOLoop 可以运行其他任务,则循环不是问题。然而,在这种情况下,缺少yield/await会导致问题。
标签: javascript python websocket socket.io tornado