多个 websocket 连接
从同一个客户端到同一服务器有两个不同的 websocket 连接有什么优点吗?对我来说,这似乎是一个糟糕的设计选择,但是有什么理由/地方它应该表现得更好吗?
Is there any advantages of having two distinct websocket connections to the same server from the same client? To me this seems a bad design choice, but is there any reason why/where it should work out better?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能想要这样做的原因有多种,但它们可能不太常见(至少目前还不是):
我确信还有其他原因,但这就是我能想到的全部原因。
There are several reasons why you might want to do that but they probably aren't too common (at least not yet):
I'm sure there are other reasons but that's all I can think of off the top of my head.
我发现当您仅订阅服务器管理的某些对象的更新时,它可以使客户端逻辑变得更加简单。您无需为单个通道设计自定义订阅协议,只需为每个元素打开一个套接字即可。
假设您通过 REST API 获得了元素集合,
您可以使用如下所示的套接字 url 来订阅单个元素的更新:
当然,有人可能会认为这不适用于复杂页面。然而,对于小型和简单的应用程序来说,它可能会让您的生活变得更加轻松。
I found that it can make client logic much simpler when you are only subscribing to updates of certain objects being managed by the server. Instead of devising a custom subscription protocol for a single channel, you can just open a socket for each element.
Let's say you obtained a collection of elements via a REST API at
You could subscribe to updates of a single element using a socket url like this:
Of course one can argue that this doesn't scale for complex pages. However, for small and simple appications it might make your life a lot easier.
除了kanaka说的之外,可能还有另一个问题。比如你的APP是这样配置的:每30秒WebSocket网络进行一次乒乓,服务器根据乒乓来判断是否有客户端的响应。您开始通过 WebSocket 上传大量数据,例如 1Gb,这可能需要几分钟,具体取决于互联网连接速度。服务器发送 ping,客户端接收并发送 pong,但 WebSocket 通道已经忙于传输数据。服务器未收到 pong 并重置连接。数据传输中断。
因此,根据功能分离 WebSocket 通道是有意义的。一种应该用于客户端信令和系统数据,例如 - 已经上传了多少数据到服务器,另一种专门用于传输大数据。
In addition to what kanaka said, there may be another problem. For example, your APP configured this way: every 30 seconds there is a ping-pong of the WebSocket network, based on which the server determines if there is a response from the client. You start uploading a large amount of data over WebSocket, e.g 1Gb, which can take several minutes depending on the internet connection speed. The server sends a ping, the client receives it and sends a pong, but the WebSocket channel is already busy transmitting data. The server does not receive a pong and resets the connection. Data transmission is interrupted.
Therefore, it makes sense to separate WebSocket channels based on their functionality. One should be used for client signaling and system data, for example - how much has already been uploaded to the server, and another one specifically for transmitting large data.
您可以将 Web 套接字连接与某种上下文捆绑在一起。想象一下有两个同时聊天的聊天客户端。上下文是接收者。如果您只有一个 Web 套接字,则每条消息都需要包含收件人。如果您有两个 Web 套接字,则可以将每个 Web 套接字与一个接收者捆绑在一起。
You can bundle the web socket connection with some kind of context. Imagine a chat client with two simultaneous chats. The context is the recipient. If you have only one web socket, every message needs to include the recipient. If you have two web sockets you can bundle each web socket with a recipient.