Android设备需要连接到服务器 - C2DM,轮询或第三的东西? [英] Android device needs to be connected to server - C2DM, polling or something third?

查看:166
本文介绍了Android设备需要连接到服务器 - C2DM,轮询或第三的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在发展中有一定的非常的苛刻要求。

I'm currently in the process of developing an app which has some very demanding needs.

项目

需要可以与服务器进行通信的应用程序。
小消息已被发送到它可以显示一个通知或启动活动的应用程式。

An application which can communicate with a server is needed. Small messages has to be send to the app which could display a notification or start an activity.

要求

客户的需求,以确保手机在任何时候都已连接。
结果客户期望应用程序可以告诉当它不再连接(或能够连接),它告诉用户该服务器。

Client needs to be sure that the phone is 'connected' at all times.
The client expects that the app can tell when it's no longer connected (or able to connect) to the server it tells the user.

客户端需要能够将消息发送到单个设备
结果,如果客户需要广播消息到连接的设备和个人设备

Client needs to be able to send a message to individual devices
If the client needs to broadcast a message to the connected devices and to the individual devices.

我的thougts(或问题)

目前,该应用程序是轮询一次一分钟的HTTP请求的服务器 - 如果应用程序是无法连接到用户得到通知的服务器。轮询能够判断哪些设备呼吁,并告诉它,如果它有一个消息吧。

Currently, the app is polling the server with a http request once a minute - if the app isn't able to connect to the server the user gets notified. The polling is able to tell which device is calling and telling it if it has a message for it.

但是......

IMO这是一个可怕的设计 - 它会产生大量过剩的流量,使用这可能是没有必要的资源,它提供了很多的问题进行连接(我不知道我会永远拿过去不管使用),该方法

IMO this is a terrible design - it generates a lot of excess traffic, uses resources which probably wasn't necessary and it offers a lot of issues with connectivity (which I'm not sure I'll ever get past no matter which method is used).

我需要你的经验来选择我的项目的解决方案。

I need your experience to select the right solution for my project.

我一直在想C2DM,但我不知道这会掩盖我的需要?
是我的唯一真正的解决方案投票?
有没有我都没有想过第三种选择?

I've been thinking of C2DM, but I'm not sure this could cover my needs? Is polling for my only real solution? Is there a third option I haven't thought about?

推荐答案

我会考虑MQTT是一个很好的解决方案,虽然这是我最熟悉的。免责声明 - 我写一个开源MQTT经纪人。你可以实现我想你想是这样的:

I'd consider MQTT as a good solution, although it is what I'm most familiar with. Disclaimer - I write an Open Source MQTT broker. You could achieve what I think you want like this:

有一个MQTT经纪人某处运行。在手机上的服务连接到代理,并订阅了独特的话题,例如设备/ 23412364,其中23412364是每个设备唯一的ID,并提到为<手机ID>下面。当你的客户想要将消息发送到特定的电话,邮件传递给设备/<电话标识取代。如果你想要的消息去的所有手机,那么手机也可以订阅设备/ all为例。

Have an MQTT broker running somewhere. The service on the phone connects to the broker and subscribe to a unique topic, e.g. device/23412364, where 23412364 is the per device unique id and referred to as <phone id> below. When your client wants to send a message to a specific phone, the message goes to device/<phone id>. If you want messages to go to all phones, then the phones could also subscribe to device/all for example.

如果你让你的订阅和消息的有1或2个服务质量,并为客户端以虚假的干净会话选项,然后消息将在券商排队,如果手机断开无论出于何种原因。服务质量= 1表示至少一次和QoS = 2的意思是只有一次。当手机重新连接,这些信息将被传递。

If you make your subscriptions and messages have Quality of Service of 1 or 2 and set the "clean session" option for the clients to false, then messages will be queued up on the broker if the phone disconnects for whatever reason. QoS=1 means "at least once" and QoS=2 means "exactly once". When the phone reconnects, those messages will be delivered.

您也可以有手机发送这样的消息注册&LT;手机ID&gt;中只是后连接(的注册,例如主题)的固定话题。 MQTT的一个特点,这是非常有用的是最后遗嘱。当您连接客户端,您可以指定遗嘱消息,它应交付的,在客户端断开意外(即没有告诉它要断开经纪人)的情况下该主题的选项。所以,你可以设置将邮件被注销&LT;手机ID&gt;中和被传递到相同的固定主题如上述(在这个例子中注册)。然后,您可以有在服务器端的另一MQTT客户机坐在订阅注册。当手机连接时,它发送一个注册&LT;手机ID&gt;中消息,当它被断开经纪人给注销&LT;手机ID&gt;中。因此MQTT客户机可以跟踪连接的客户端的在服务器侧

You could also have the phone send a message like "register <phone id>" to a fixed topic just after it connected (topic of "register" for example). A feature of MQTT that is very useful is the "Last Will and Testament". When you connect a client, you have the option of specifying a Will message and the topic that it should be delivered on, in the case that the client disconnects unexpectedly (ie. without telling the broker that it is going to disconnect). So you could set the will message to be "unregister <phone id>" and to be delivered to the same fixed topic as above ("register" in this example). You could then have another MQTT client at the server end sitting subscribed to "register". When a phone connects, it sends a "register <phone id>" message, when it gets disconnected the broker sends "unregister <phone id>". The MQTT client can therefore keep track of the connected clients at the server side.

目前物联网的手机最后,你可以得到,如果TCP连接通过正常的网络code下山通知。

At the phone end of things, you can get notified if the TCP connection goes down through normal network code.

一些相关链接:

  • Using MQTT in Android mobile applications: http://dalelane.co.uk/blog/?p=1599
  • Trivial MQTT Android project based on above: http://mosquitto.org/2011/11/android-mqtt-example-project/
  • MQTT Power Profiling: http://stephendnicholas.com/archives/219
  • MQTT features: http://mosquitto.org/man/mqtt-7.html
  • Facebook using MQTT: http://www.facebook.com/notes/facebook-engineering/building-facebook-messenger/10150259350998920

这篇关于Android设备需要连接到服务器 - C2DM,轮询或第三的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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