本文介绍了多个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?
推荐答案
可能想要这样做的几个原因,但它们可能不太常见(至少现在还没有):
There are several reasons why you might want to do that but they probably aren't too common (at least not yet):
- 您有发送/接收的加密和未加密数据(例如,某些数据体积庞大但不敏感)。
- 您同时拥有流数据和延迟敏感数据:想象一下偶尔会在游戏中播放视频的互动游戏。您不希望大型媒体流延迟接收对延迟敏感的正常游戏消息。
- 您同时拥有文本(例如JSON控制消息)和二进制数据(类型化数组或blob)并且不要我想打扰添加你自己的协议层来区分,因为WebSockets已经为你做了这个。
- 你支持多个WebSocket子协议(URI之后的可选设置)并且页面想要访问多个(每个WebSocket连接仅限于一个子协议)。
- 您有几个不同的WebSocket服务位于同一个Web服务器和端口之后。客户端为每个连接选择的方式可能取决于URI路径,URI方案(ws或wss),子协议,甚至可能是从客户端到服务器的第一条消息。
- You have both encrypted and unencrypted data that you are sending/receiving (e.g. some of the data is bulky but not sensitive).
- You have both streaming data and latency sensitive data: imagine an interactive game that occasionally has streamed video inside the game. You don't want large media streams to delay receipt of latency sensitive normal game messages.
- You have both textual (e.g. JSON control messages) and binary data (typed arrays or blobs) and don't want to bother with adding your own protocol layer to distinguish since WebSockets already does this for you.
- You have multiple WebSocket sub-protocols (the optional setting after the URI) that you support and the page wants to access more than one (each WebSocket connection is limited to a single sub-protocol).
- You have several different WebSocket services sitting behind the same web server and port. The way the client chooses per connection might depend on URI path, URI scheme (ws or wss), sub-protocol, or perhaps even the first message from client to server.
我确信还有其他原因,但这就是我能想到的全部。
I'm sure there are other reasons but that's all I can think of off the top of my head.
这篇关于多个websocket连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!