从 J2ME 客户端轮询 HTTP 服务器

发布于 2024-07-30 07:39:21 字数 288 浏览 8 评论 0原文

我的手机(客户端)上运行着一个 J2ME 应用程序,

我想打开与服务器的 HTTP 连接并不断轮询服务器上的更新信息。

执行的每次轮询都会耗尽 GPRS 字节,并且从长远来看会变得昂贵,因为 GPRS 计费是基于发送和接收的数据包。 是否有使用 HTTP 协议进行轮询的字节有效方式?

我也听说过长轮询,但我不确定它是如何工作的以及它的效率如何。

实际上,首选的方法是服务器告诉手机应用程序新数据已准备好使用,这样就不需要进行轮询,但是我不知道这些技术,尤其是在 J2ME 中。

I have a J2ME app running on my mobile phone(client),

I would like to open an HTTP connection with the server and keep polling for updated information on the server.

Every poll performed will use up GPRS bytes and would turn out expensive in the long run, as GPRS billing is based on packets sent and received.
Is there a byte efficient way of polling using the HTTP protocol?.

I have also heard of long polling, But I am not sure how it works and how efficient it would be.

Actually the preffered way would be for the Server to tell the phone app that new data is ready to be used that way polling won't be needed to be done, however I don't know of these techniques especially in J2ME.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

难忘№最初的完美 2024-08-06 07:39:21

如果您想仅使用 HTTP 来解决此问题,长轮询将是最好的方法。 这相当容易。 首先,您需要在服务器端设置一个用于通知的 URL(例如 http://example.com/notify),并定义通知协议。 该协议可以像一些文本行一样简单,每一行都是一个事件。 例如,

  MSG user1
  PHOTO user2 album1
  EMAIL user1
  HEARTBEAT 300

手机上的轮询线程的工作方式如下:

  1. 与通知 URL 建立 HTTP 连接。 在 J2ME 中,您可以使用 GCF HttpConnection。
  2. 如果没有事件要推送,服务器将阻塞。
  3. 如果服务器响应,则获取每一行并生成一个新线程来通知应用程序并环回#1。
  4. 如果连接因任何原因关闭,请休眠一段时间并返回步骤 1。

您必须注意以下实现细节,

  1. 调整客户端和服务器上的 HTTP 超时。 超时时间越长,效率越高。 连接超时将导致重新连接。
  2. 在电话和服务器上启用 HTTP keepalive。 TCP 的 3 次握手在 GPRS 术语中是昂贵的,所以尽量避免它。
  3. 检测失效连接。 在移动环境中,很容易获得过时的 HTTP 连接(连接已消失,但轮询线程仍在等待)。 您可以使用心跳来恢复。 假设心率为 5 分钟。 服务器应每 5 分钟发送一次通知。 如果没有数据要推送,则只发送 HEARTBEAT。 在电话上,如果 5 分钟内没有收到任何消息,轮询线程应尝试关闭并重新打开轮询连接。
  4. 仔细处理连接错误。 当存在连接问题时,长轮询效果不佳。 如果处理不当,可能会破坏交易。 例如,如果睡眠时间不够长,您可能会在步骤 4 上浪费大量数据包。 如果可能,请检查手机上的 GPRS 可用性,并在 GPRS 不可用时暂停轮询线程以节省电池。
  5. 如果实施不当,服务器成本可能会非常高。 例如,如果您使用 Java servlet,则每个正在运行的应用程序将至少有一个相应的轮询连接及其线程。 根据用户数量,这可能会很快杀死 Tomcat :) 您需要使用资源高效的技术,例如 Apache Mina。

有人告诉我还有其他更有效的方法可以将通知推送到手机,例如使用短信和一些 IP 级别的技巧。 但您要么必须进行一些低级的不可移植编程,要么面临专利侵权的风险。 长轮询可能是使用纯 HTTP 解决方案可以获得的最佳方案。

If you want solve this problem using HTTP only, long polling would be the best way. It's fairly easy. First you need to setup an URL on server side for notification (e.g. http://example.com/notify), and define a notification protocol. The protocol can be as simply as some text lines and each line is an event. For example,

  MSG user1
  PHOTO user2 album1
  EMAIL user1
  HEARTBEAT 300

The polling thread on the phone works like this,

  1. Make a HTTP connection to notification URL. In J2ME, you can use GCF HttpConnection.
  2. The server will block if no events to push.
  3. If the server responds, get each line and spawn a new thread to notify the application and loopback to #1.
  4. If the connection closes for any reason, sleep for a while and go back to step 1.

You have to pay attention to following implementation details,

  1. Tune HTTP timeouts on both client and server. The longer the timeout, the more efficient. Timed out connection will cause a reconnect.
  2. Enable HTTP keepalive on both the phone and the server. TCP's 3-way handshake is expensive in GPRS term so try to avoid it.
  3. Detect stale connections. In mobile environments, it's very easy to get stale HTTP connections (connection is gone but polling thread is still waiting). You can use heartbeats to recover. Say heartbeat rate is 5 minutes. Server should send a notification in every 5 minutes. If no data to push, just send HEARTBEAT. On the phone, the polling thread should try to close and reopen the polling connection if nothing received for 5 minutes.
  4. Handling connectivity errors carefully. Long polling doesn't work well when there are connectivity issues. If not handled properly, it can be the deal-breaker. For example, you can waste lots of packets on Step 4 if the sleep is not long enough. If possible, check GPRS availability on the phone and put the polling thread on hold when GPRS is not available to save battery.
  5. Server cost can be very high if not implemented properly. For example, if you use Java servlet, every running application will have at least one corresponding polling connection and its thread. Depending on the number of users, this can kill a Tomcat quickly :) You need to use resource efficient technologies, like Apache Mina.

I was told there are other more efficient ways to push notifications to the phone, like using SMS and some IP-level tricks. But you either have to do some low level non-portable programming or run into risks of patent violations. Long polling is probably the best you can get with a HTTP only solution.

栀子花开つ 2024-08-06 07:39:21

我不知道你所说的“轮询”是什么意思,你的意思是像 IMAP IDLE?
连接保持打开状态,并且一次又一次地建立连接本身没有开销。 如前所述,另一种可能的解决方案是 HTTP 请求的 HEAD 标头(忘记了,谢谢!)。

查看此教程,了解 J2ME 中 HTTP 连接的基础知识。

无法将数据推送到没有推送支持的应用程序/设备(例如黑莓)。

I don't know exactly what you mean by "polling", do you mean something like IMAP IDLE?
A connection stays open and there is no overhead for building up the connection itself again and again. As stated, another possible solution is the HEAD Header of a HTTP Request (forgot it, thanks!).

Look into this tutorial for the basic of HTTP Connections in J2ME.

Pushing data to an application/device without Push Support (like a Blackberry) is not possible.

固执像三岁 2024-08-06 07:39:21

最好的方法是使用套接字连接。 许多应用程序(例如 GMail)都使用它们。

The best way to do this is to use socket connection. Many application like GMail use them.

无法言说的痛 2024-08-06 07:39:21

HEAD HTTP 请求 是 HTTP 提供的方法,如果您想检查页面是否无论是否更改,浏览器和代理服务器都使用它来检查页面是否已更新,而不消耗太多带宽。

在 HTTP 术语中,HEAD 请求与没有正文的 GET 相同,我认为最多只有几百个字节,如果您的轮询不是很频繁,那么这看起来是可以接受的。

The HEAD HTTP request is the method that HTTP provides if you want to check if a page has changed or not, it is used by browsers and proxy servers to check whether a page has been updated or not without consuming much bandwidth.

In HTTP terms, the HEAD request is the same as GET without the body, I assume this would be only a couple hundred bytes at most which looks acceptable if your polls are not very frequent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文