javascript - 刷新客户端网页时如何重新连接到 Web Socket
问题描述
当我刷新我的客户端网页时,我停止从龙卷风套接字服务器接收数据。如何重新连接到流?
我尝试将连接对象附加到列表中,然后在关闭时从列表中删除连接,但事实证明,当您刷新页面时,连接永远不会关闭,它根据服务器保持活动状态,但它也没有不再在客户端接收数据:(
这是我的龙卷风服务器
# 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>
那么,当我刷新客户端网页时,如何继续从套接字服务器接收数据?
解决方案
循环阻塞了你的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)
...
作为附加提示,使用 aset()
而不是 alist()
来存储您的连接,这样您就不会意外添加重复的连接。
在这种情况下,您还需要稍微修改您的代码。set
没有append
方法,而是有add
方法。
connections = set()
...
self.connections.add(self) # to add connection
...
self.connections.remove(self) # to remove connetion
推荐阅读
- reactjs - React material-ui中Tab、TabContext、Tablist、TabPanel的区别
- node.js - 如何在不删除服务器生成的文件的情况下部署 dist 文件夹
- python - 逐列检查数组中的元素是否存在于另一个数组中
- python - Mac 上 CPLEX Python API 安装大问题 CpoException
- amazon-web-services - 为什么我不能通过 EC2 (AWS) 访问我托管的 Flask webApp
- swift - 试图将元素附加到矩阵中的向量时卡住了
- php - Wordpress 如何将自定义类添加到作者名称以进行样式设置
- react-router-dom - 白页 react-router-dom
- python - Scrapy不解析数据
- python - Django:如何应用 uuid django.contrib.auth 迁移