简体   繁体   English

为什么我们需要网络套接字?

[英]Why do we need web-sockets?

This is more of a n00b question, but I've never really known the answer. 这更像是一个n00b问题,但我从未真正知道答案。

so why do we need the websockets protocol? 那为什么我们需要websockets协议?

and, what are the advantages over comet-style/long poll/hanging GET-style use of HTTP? 并且,与彗星式/长轮询/悬挂式GET风格的HTTP使用有什么优势?

Comet and Ajax can both deliver end-user experiences that provide desktop-like functionality and low user-perceived latency, only Web Sockets lives up to the promise of providing a native means to accurately and efficiently stream events to and from the browser with negligible latency. Comet和Ajax都可以提供最终用户体验,提供类似桌面的功能和低用户感知的延迟,只有Web套接字能够提供本机方式,以准确有效地将事件传入和传出浏览器,延迟可以忽略不计。

With polling it makes unnecessary requests and, as a result, many connections are opened and closed needlessly in low-message-rate situations.(as with polling it sends HTTP requests at regular intervals and immediately receives a response.) 通过轮询,它会产生不必要的请求,因此,在低消息速率情况下会不必要地打开和关闭许多连接。(与轮询一样,它会定期发送HTTP请求并立即收到响应。)

Web Sockets remove the overhead and dramatically reduce complexity. Web套接字消除了开销,并大大降低了复杂性。

1-WebSocket is a naturally full-duplex, bidirectional, single-socket connection. 1-WebSocket是一种自然的全双工,双向,单插槽连接。 With WebSocket, your HTTP request becomes a single request to open a WebSocket connection and reuses the same connection from the client to the server, and the server to the client. 使用WebSocket,您的HTTP请求将成为打开WebSocket连接的单个请求,并重用从客户端到服务器以及服务器到客户端的相同连接。

2-WebSocket reduces latency. 2-WebSocket减少了延迟。 For example, unlike polling, WebSocket makes a single request. 例如,与轮询不同,WebSocket只发出一个请求。 The server does not need to wait for a request from the client. 服务器不需要等待来自客户端的请求。 Similarly, the client can send messages to the server at any time. 同样,客户端可以随时向服务器发送消息。 This single request greatly reduces latency over polling, which sends a request at intervals, regardless of whether messages are available. 此单个请求极大地减少了轮询的延迟,轮询以一定间隔发送请求,无论消息是否可用。

3-WebSocket makes real-time communication much more efficient. 3-WebSocket使实时通信更加高效。 You can always use polling (and sometimes even streaming) over HTTP to receive notifications over HTTP. 您始终可以通过HTTP使用轮询(有时甚至是流式传输)来通过HTTP接收通知。 However, WebSocket saves bandwidth, CPU power, and latency. 但是,WebSocket可以节省带宽,CPU功耗和延迟。 WebSocket is an innovation in performance. WebSocket是一项性能创新。

4-WebSocket is an underlying network protocol that enables you to build other standard protocols on top of it. 4-WebSocket是一种底层网络协议,使您可以在其上构建其他标准协议。

5-WebSocket is part of an effort to provide advanced capabilities to HTML5 applications in order to compete with other platforms. 5-WebSocket是为HTML5应用程序提供高级功能以与其他平台竞争的努力的一部分。

6-WebSocket is about Simplicity 6-WebSocket是关于Simplicity的

这是一篇关于websocket在websocket.org上的优势的文章

It isn't clear that we do need them. 目前尚不清楚我们确实需要它们。 In the scenario of pushing events to the client, a page can make ordinary AJAX GET requests in a loop, and the server can "hang" until events are available. 在将事件推送到客户端的场景中,页面可以在循环中生成普通的AJAX GET请求,并且服务器可以“挂起”直到事件可用。 After some timeout passes the server can return a "no events" response so the client will reconnect. 经过一些超时后,服务器可以返回“无事件”响应,以便客户端重新连接。 During the period where the connection is open and the client is waiting for a response, there is an effective push channel from the server back to the client. 在连接打开且客户端正在等待响应的期间,存在从服务器返回到客户端的有效推送通道。

The timeout period can be adjusted to reduce the amount of unnecessary reconnecting, though it cannot typically be infinite because most server frameworks will terminate the server-side process if it appears to have hung for too long. 可以调整超时时间以减少不必要的重新连接,尽管它通常不会是无限的,因为大多数服务器框架如果看起来已经挂起太长时间将终止服务器端进程。

Given this existing capability, the question is: does a new communication framework really add significant value over what can already be done? 鉴于现有的能力,问题是:新的通信框架是否确实增加了已经可以做的重要价值 It wouldn't really enable something that cannot be done. 它不会真正实现无法完成的事情。 It would only slightly improve it. 它只会略微改善它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM