gpt4 book ai didi

javascript - 高效电池定期数据提交

转载 作者:搜寻专家 更新时间:2023-11-01 04:33:00 25 4
gpt4 key购买 nike

我有一个要在手机上运行的 Web 应用程序,当它打开时会不断生成数据(每隔几秒或每隔几分钟生成几千字节,具体取决于设置)并需要将其实时推送到服务器。没有数据从服务器发送到浏览器。

我主要关心的是让这个提交电池高效,几秒钟的延迟完全没问题。

我设想了两种解决方案:

  1. 定期使用数据向服务器执行POST(以避免维持永久连接)
  2. 有一个开放的 websocket 并定期发送消息(以避免 HTTP 请求的重量)

哪种电池效率最高?我还缺少其他策略吗?


实际上我的应用程序将托管在 heroku 上,does not supports websockets然而,这会导致长时间轮询,因此目前我假设按需发布更好,但我想知道将来是否可以选择它(或者这个假设可能是错误的)。

最佳答案

安卓

在Android设备上,存在三种不同的网络 radio 状态as the documentation says :

The state machine for a typical 3G network radio consists of threeenergy states:

  1. Full power: Used when a connection is active, allowing the device to transfer data at its highest possible rate.

  2. Low power: An intermediate state that uses around 50% of the battery power at the full state.

  3. Standby: The minimal energy state during which no network connection is active or required.

While the low and idle states drain significantly less battery, theyalso introduce significant latency to network requests. Returning tofull power from the low state takes around 1.5 seconds, while movingfrom idle to full can take over 2 seconds.

设备在闲置 5 秒后从 Full 变为 Low,然后再在 12 秒后从 Low 变为 Standby。

上面的链接还涵盖了电池友好型连接的一些最佳实践,尽管它没有具体说明 Websockets。

iOS

我在 iOS 设备上找不到此类特定文档,但型号 seems to apply in a similar way :

Cellular and Wi-Fi radios are designed to power down when there is noactivity. Depending on the radio, though, doing so can take severalseconds. If your app transmits small bursts of data every few seconds,the radios may stay powered up and continue to consume power, evenwhen they are not actually doing anything. Rather than transmit smallamounts of data more often, it is better to transmit a larger amountof data once or at relatively large intervals.

那又怎么样?

一般来说,您应该使用简短的 POST 请求并尽可能少地发送数据,这样 radio 就可以在两者之间断电。

关于javascript - 高效电池定期数据提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13158018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com