从J2ME客户端轮询HTTP服务器 [英] polling a HTTP server from J2ME client

查看:103
本文介绍了从J2ME客户端轮询HTTP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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.

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

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.

推荐答案

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

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. 建立与通知URL的HTTP连接。在J2ME中,您可以使用GCF HttpConnection。

  2. 如果没有要推送的事件,服务器将阻止。

  3. 如果服务器响应,请获取每一行并生成一个新线程以通知应用程序并回送到#1。

  4. 如果连接关闭任何原因,睡了一会儿,然后回到第1步。

  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. 调整客户端和服务器上的HTTP超时。超时越长,效率越高。超时连接将导致重新连接。

  2. 在电话和服务器上启用HTTP keepalive。 TCP的三次握手在GPRS术语中很昂贵,所以尽量避免使用它。

  3. 检测过时的连接。在移动环境中,很容易获得陈旧的HTTP连接(连接已经消失,但轮询线程仍在等待)。您可以使用心跳恢复。说心跳率是5分钟。服务器应每5分钟发送一次通知。如果没有要推送的数据,只需发送HEARTBEAT即可。在电话上,如果5分钟内没有收到任何内容,则轮询线程应该尝试关闭并重新打开轮询连接。

  4. 小心处理连接错误。当存在连接问题时,长轮询不能很好地工作。如果处理不当,它可能是交易破坏者。例如,如果睡眠时间不够,您可以在步骤4中浪费大量数据包。如果可能,请检查手机上的GPRS可用性,并在无法使用GPRS时将轮询线程置于保持状态。

  5. 如果未正确实施,服务器成本可能会非常高。例如,如果使用Java servlet,则每个正在运行的应用程序将至少具有一个相应的轮询连接及其线程。根据用户数量,这可能会迅速杀死Tomcat :)您需要使用资源有效的技术,例如Apache Mina。

  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.

我被告知还有其他更有效的方法可以将通知推送到手机,例如使用短信和一些IP级别的技巧。但是你要么必须做一些低级别的非便携式编程,要么遇到专利侵权的风险。使用仅HTTP解决方案,长轮询可能是最好的。

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.

这篇关于从J2ME客户端轮询HTTP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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