WebSockets在什么时候比轮询效率低? [英] At what point are WebSockets less efficient than Polling?

查看:281
本文介绍了WebSockets在什么时候比轮询效率低?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我知道上述问题的答案在一定程度上取决于您的应用程序的体系结构,但我对非常简单的场景非常感兴趣.

While I understand that the answer to the above question is somewhat determined by your application's architecture, I'm interested mostly in very simple scenarios.

从本质上讲,如果我的应用每隔5秒或每分钟ping检查一次更改,或者每分钟一次,发送以维持打开的Web套接字连接的数据最终将超过您通过简单轮询所浪费的数据?

Essentially, if my app is pinging every 5 seconds for changes, or every minute, around when will the data being sent to maintain the open Web Sockets connection end up being more than the amount you would waste by simple polling?

基本上,我感兴趣的是,如果应用程序不一定需要实时更新,而仅需要定期检查,是否有一种方法可以通过使用Meteor之类的框架来量化您造成的效率低下?

Basically, I'm interested in if there's a way of quantifying how much inefficiency you incur by using frameworks like Meteor if an application doesn't necessarily need real-time updates, but only periodic checks.

请注意,由于Meteor之类的框架具有高度优化的方法,仅请求对数据库的更新,因此我的重点是带宽利用率,而不是数据库访问时间.

Note that my focus here is on bandwidth utilization, not necessarily database access times, since frameworks like Meteor have highly optimized methods of requesting only updates to the database.

推荐答案

websocket连接的全部目的是,您不必对应用程序进行ping操作.相反,客户端只连接一次,然后服务器只要有可用更改就可以直接发送客户端更改.客户永远不必问.服务器只是在可用时发送数据.

The whole point of a websocket connection is that you don't ever have to ping the app for changes. Instead, the client just connects once and then the server can just directly send the client changes whenever they are available. The client never has to ask. The server just sends data when it's available.

对于任何类型的服务器启动的数据,这在带宽方面比http轮询更为有效.除了为您提供更及时的结果之外(结果将立即交付,而不是仅在下一个轮询间隔内由客户端发现).

For any type of server-initiated-data, this is way more efficient with bandwidth than http polling. Besides giving you much more timely results (the results are delivered immediately rather than discovered by the client only on the next polling interval).

对于纯带宽使用,详细信息取决于实际情况. http轮询请求必须建立一个TCP连接并确认该连接(如果是SSL连接,则还要发送更多数据),然后它必须发送http请求,包括属于该主机的任何相关cookie,包括相关标头和获取URL.然后,服务器必须发送响应.而且,大多数情况下,轮询的所有开销都将完全浪费带宽,因为没有什么要报告的.

For pure bandwidth usage, the details would depend upon the exact circumstances. An http polling request has to set up a TCP connection and confirm that connection (even more data if its an SSL connection), then it has to send the http request, including any relevant cookies that belong to that host and including relevant headers and the GET URL. Then, the server has to send a response. And, most of the time all of this overhead of polling will be completely wasted bandwidth because there's nothing new to report.

webSocket以一个简单的http请求开头,然后将该协议升级为webSocket协议. webSocket连接本身根本不需要发送任何数据,直到服务器将某些内容发送到客户端为止,在这种情况下,服务器仅发送数据包.发送数据本身的开销也要少得多.没有cookie,没有标题等,只是数据.即使您在webSocket上使用了一些保持活动状态,与HTTP请求的开销相比,该数据量也非常少.

A webSocket starts with a simple http request, then upgrades the protocol to the webSocket protocol. The webSocket connection itself need not send any data at all until the server has something to send to the client in which case the server just sends the packet. Sending the data itself has far less overhead too. There are no cookies, no headers, etc... just the data. Even if you use some keep-alives on the webSocket, that amount of data is incredibly tiny compared to the overhead of an HTTP request.

因此,您将节省多少带宽取决于具体情况.如果在找到任何有用数据之前需要50个轮询请求,则与webSocket方案相比,这些http请求中的每个请求都被完全浪费了.带宽上的差异可能很大.

So, how exactly much you would save in bandwidth depends upon the details of the circumstances. If it takes 50 polling requests before it finds any useful data, then every one of those http requests is entirely wasted compared to the webSocket scenario. The difference in bandwidth could be enormous.

您询问了仅需要定期检查的应用程序.一旦进行了定期检查,导致没有数据被检索,那将浪费带宽.这就是webSocket的全部思想.没有数据要发送时,您不会占用带宽(或几乎没有带宽).

You asked about an application that only needs periodic checks. As soon as you have a periodic check that results in no data being retrieved, that's wasted bandwidth. That's the whole idea of a webSocket. You consume no bandwidth (or close to no bandwidth) when there's no data to send.

这篇关于WebSockets在什么时候比轮询效率低?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆