WebSockets能耗 [英] WebSockets Energy Consumption

查看:114
本文介绍了WebSockets能耗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在移动设备(iPhone / Android)上,通过实施WebSockets与HTTP对电池消耗的影响是什么?

On a mobile device (iPhone/Android), what is the impact on battery consumption by implementing WebSockets vs HTTP?

假设我正在构建即时消息应用并有两个选项:

Suppose I'm building an instant messaging app and have two options:


  1. 依靠推送通知来通知设备新消息,然后通过REST API获取该消息。

  2. 当用户启动应用程序时建立WebSocket连接,并在应用程序处于活动状态时维护该连接。服务器直接将所有消息转发到设备,而不是使用推送通知。

有人告诉我保持持久的WebSocket连接将是一个庞大的电池供电,因为它需要天线持续工作,而不是在每次请求后断电 - 但这是真的吗?

Someone told me that maintaining a persistent WebSocket connection would be a huge battery hog because it requires the antenna to be constantly active, rather than powering down after each request - but is that really true?

这个答案表明,在iOS上,每个设备始终与推送通知服务保持持久连接(类似于WebSockets,我想),所以不会这表明设备的天线无论如何都是全天候运行的?

This answer suggests that on iOS, each device maintains a persistent connection with the Push Notification service at all times (similar to WebSockets, I suppose), so doesn't that suggest that the device's antenna is running 24/7 anyway?

对于即时通讯应用程序来说,HTTP请求的额外开销是微不足道的,但在应用程序中需要不断下载大量项目,持久的WebSocket连接将非常有用。有关在移动设备上维护长期WebSocket连接的任何见解(特别是有关电池消耗)都非常有用!

The extra overhead of HTTP requests would be pretty insignificant for an instant messaging app, but in an application where a large number of items need to be downloaded constantly, a persistent WebSocket connection would be very useful. Any insight about maintaining long-term WebSocket connections on mobile devices (especially concerning battery consumption) would be extremely helpful!

推荐答案

Pure WebSockets处于空闲状态的/ TCP套接字解决方案(没有乒乓包)甚至不检测连接丢失,因为它不发送任何数据。一旦发生握手,它会假定连接已建立,直到某些东西关闭套接字或发送方将尝试发送一些数据并且检测到不再有连接(在这种情况下,套接字有时会自动关闭,但我不会计算在它上面。

Pure WebSockets/TCP sockets solution (without ping-pong packets) in idle state doesn't even detect a connection loss because it doesn't send any data. Once handshake has occured, it assumes that connection is established until something closes the socket or sender will attempt to send some data and it is detected that there is no connection anymore (in this case socket is sometimes closed automatically, but I wouldn't count on it).

因此,当连接处于空闲状态时,WiFi可以毫无顾虑地进入睡眠状态,从而最大限度地降低功耗。但是有一些缺点:它没有检测到连接丢失,因此如果ping失败,你必须实现ping-pong解决方案并尝试重新连接。 ping循环将影响CPU使用率和功耗,但它将远低于始终处于On状态的WiFi。当然,您将无法在睡眠模式下接收数据。

So, when connection is in idle state, the WiFi could go to sleep state without a worry and therefore minimize power consumption. But there is some drawback: it does not detect connection loss, so you have to implement ping-pong solution with attempt to reconnect if ping failed. The ping loop will have impact on CPU usage and power consumption, but it will be much lower than WiFi which is constantly in "On" state. Of course, you will lose the ability to receive data when in sleep mode.

来源:拥有自己的Android和WebSockets体验。

Source: own Android and WebSockets experience.

这篇关于WebSockets能耗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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