Javascript websockets的缺点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6224771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:50:14  来源:igfitidea点击:

disadvantages of websockets

javascriptweb-serviceswebsocket

提问by Pacerier

I would like to know what kind of limitations there are in using websockets.

我想知道使用 websockets 有哪些限制。

Websockets is just so.. powerful. I can't imagine that it is without disadvantages.

Websockets 是如此......强大。我无法想象它没有缺点。

Say, what is the number of users that can simultaneously connect to a server (if I'm creating a game and users will connect to the game through WebSockets, what will limit the number of users able to connect at any one time?)

比如说,可以同时连接到服务器的用户数量是多少(如果我正在创建一个游戏并且用户将通过 WebSockets 连接到游戏,那么任何时候能够连接的用户数量会受到什么限制?)

Also is it true that with each additional connection, the quality of the connections (speed and stuff like that) will decrease?

此外,每增加一个连接,连接的质量(速度和类似的东西)是否会降低?

采纳答案by suriv

The advantages and disadvantages will of course depend on the specific use case, but I'll try to point out some differences between WebSocket and HTTP.

优缺点当然取决于具体的用例,但我会尝试指出 WebSocket 和 HTTP 之间的一些差异。

WebSocket is more complex than HTTP. You can establish an HTTP connection with a telnet client, but you probably cannot do the same with WS. Even if you ignored the handshake requirements (which include the use of the SHA1 hash function), you would then be unable to properly mask and frame the data to be sent, and the server would close the connection.

WebSocket 比 HTTP 更复杂。您可以与 telnet 客户端建立 HTTP 连接,但您可能无法与 WS 建立相同的连接。即使您忽略了握手要求(包括使用 SHA1 哈希函数),您也将无法正确屏蔽和构建要发送的数据,并且服务器将关闭连接。

As Uwe said, WebSocket connections are intended to be more persistent than HTTP connections. If you only want to receive an update every 30 minutes, you will want to go with HTTP. If you want to receive updates every second, a WebSocket might be a better option, because establishing an HTTP connection takes a lot of time.

正如 Uwe 所说,WebSocket 连接旨在比 HTTP 连接更持久。如果您只想每 30 分钟接收一次更新,您将需要使用 HTTP。如果您想每秒接收更新,WebSocket 可能是更好的选择,因为建立 HTTP 连接需要很多时间。

To establish an HTTP connection, you first have to establish a TCP connection (SYN, SYN/ACK, ACK), then send a GET request with a pretty big header, then finally receive the server's response (along with another big header).

要建立 HTTP 连接,首先必须建立 TCP 连接(SYN、SYN/ACK、ACK),然后发送一个带有相当大标头的 GET 请求,最后接收服务器的响应(以及另一个大标头)。

With an open WebSocket you simply receive the response (no request needed), and it comes with a much smaller header: from two bytes for small frames, up to 10 bytes for ridiculously large frames (in the gigabyte range).

使用开放式 WebSocket,您只需接收响应(不需要请求),并且它带有一个更小的标头:从小帧的两个字节到可笑的大帧(在千兆字节范围内)最多 10 个字节。

You need to weigh the two costs (keeping a connection open vs establishing a new connection) to decide between the two protocols.

您需要权衡两种成本(保持连接打开与建立新连接)以在两种协议之间做出决定。

Note: this answer is based on the current draft of the protocol (draft-ietf-hybi-thewebsocketprotocol-09). WebSocket is evolving rapidly, many implementations are still based on older drafts, and some details may change before it is finalized.

注意:此答案基于协议的当前草案(draft-ietf-hybi-thewebsocketprotocol-09)。WebSocket 发展迅速,许多实现仍然基于较旧的草案,在最终确定之前,某些细节可能会发生变化。

回答by Uwe Keim

From what I read, this seems to be related to HTTP Server Pushwhich I read that it is usually not recommended to use since it creates lots of connections on the server.

从我读到的内容来看,这似乎与HTTP 服务器推送有关,我读到它通常不推荐使用,因为它在服务器上创建了大量连接。

If I have to choose, I probably would always develop a client polling mechanism.

如果非要我选,我可能会一直开发客户端轮询机制。