简体   繁体   English

Android实时应用

[英]Android Real-Time application

I'm working on app the share tasks between users. 我正在研究用户之间的共享任务。 By Google Cloud Messaging, I can notify the user target that he has a new task shared. 通过Google Cloud Messaging,我可以通知用户目标他共享了一个新任务。 The problem is : GCM does not offer a delivery guarantee. 问题是:GCM不提供送货保证。 Would someone use an app like WhatsApp if he took minutes to deliver a message or not come to hand? 如果有人花了几分钟时间传递消息还是没来得及使用,它将使用WhatsApp之类的应用程序吗? That's my problem with GCM. 那是我的GCM问题。

So I've got a solution : Use Socket!!! 所以我有一个解决方案:使用套接字!!!

Using Socket.oi and Node.js, my dream came become really works like magic !!! 使用Socket.oi和Node.js,我的梦想变成了像魔术一样真正的作品! But as nothing is free, keep a socket connection has a very high cost for the battery. 但是,由于没有什么是免费的,因此保持插座连接的电池成本非常高。 Some people argue that the use of Sockets when there is no communication, nothing in or out, no cycles, so there is no consumption. 有人认为,在没有通信,无进出出,无循环的情况下使用套接字,因此没有消耗。

My friends, I've read a lot of text and do not know what approach should I follow. 我的朋友们,我读了很多文章,不知道应该采用哪种方法。 I ask your help. 我请你帮忙。 Soket.oi? Soket.oi? WebSocket ??? WebSocket ???

How to maintain a connection to my server permante preserving the most of the battery? 如何保持与服务器的连接永久保留大部分电池?

I appreciate everyone's help! 我感谢大家的帮助!

Socket.io is good, and certainly useful in many real-time applications, but what happens when the app is terminated by the user? Socket.io很好,并且在许多实时应用程序中肯定有用,但是当应用程序被用户终止时会发生什么? Or if the user restarts their phone? 还是用户重新启动手机? How would you receive notifications then? 那么您将如何接收通知?

For all purposes, GCM is good enough. 对于所有目的,GCM都足够好。

You need to use mix of a socket connection and GCM. 您需要混合使用套接字连接和GCM。 Both connection types do no guarantee delivery so you need to implement mechanism which checks consistency of messages history. 两种连接类型都不能保证传递,因此您需要实现检查消息历史记录一致性的机制。

Simplified scenario could look like this: 简化方案如下所示:

  1. a user launches your client 用户启动您的客户端
  2. the client app registers at GCM and sends google id to your server. 客户端应用在GCM上注册,并将Google ID发送到您的服务器。
  3. the client app establishes socket connection 客户端应用程序建立套接字连接
  4. your server sends messages to a client through GCM and socket connection (if it is establish with particular connection) 您的服务器通过GCM 套接字连接向客户端发送消息(如果已通过特定连接建立)
  5. each message has unique id, therefore the client could just ignore second identical message from Google of a socket connection 每条消息都有唯一的ID,因此客户端可以忽略来自套接字连接的Google的第二条相同的消息

About not delivered messages: 关于未传递的消息:

When client connects to the server through socket connection it should receive response where history of messages should be put. 当客户端通过套接字连接连接到服务器时,它应该收到响应,并在其中放置消息的历史记录。 It shouldn't be full history, it could be just last message (in case you develop chat app). 它不应该是完整的历史记录,而应该只是最后一条消息(以防您开发聊天应用程序)。 Then a client just checks if he has notified user about last message or not. 然后,客户仅检查他是否已通知用户有关最后一条消息的信息。 If not then your client makes request(http or through socket) to your server and receives undelivered messages. 如果不是,则您的客户端向服务器发出请求(http或通过套接字),并接收未传递的消息。

Battery consumption: 电池消耗:

Do not acquire wake lock to maintain socket connection! 不要获取唤醒锁来保持套接字连接! A device must go sleep. 设备必须进入睡眠状态。 GCM will wake up handset. GCM将唤醒手机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM