WebSockets 能耗 [英] WebSockets Energy Consumption

查看:25
本文介绍了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,我想),所以这不是表明设备的天线无论如何都在 24/7 运行吗?

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!

推荐答案

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

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 循环会对 CPU 使用率和功耗产生影响,但会远低于 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天全站免登陆