GCM与插座的android插座、GCM、android

2023-09-06 02:04:05 作者:萌酱の小可爱

在我所知,大部分的Andr​​oid教程和例子在那里依靠使用GCM的从服务器向Android设备发送数据;并使用PHP脚本以及交/ get方法,从设备的数据发送到服务器。

In my knowledge, most of android tutorials and examples out there rely on use of GCM for sending data from server to android device; and use php scripts along with post/get methods to send data from device to server.

我的一个朋友(人谁不具备的Andr​​oid编程方面的知识),简单地问我,为什么我们不能用Socket类中的Java?在传统的Java编程,使用套接字(IP联系地址+端口号)实现类似于GCM功能(单台服务器多个客户端 - 使用正确的技术可以保证消息不会丢失,就像GCM)。

A friend of mine(someone who doesn't have any knowledge of android programming) simply asked me, why can't we use Socket class in java? In traditional java programming, you use sockets (IP adress + port no.) to achieve functionality similar to GCM (single server multiple clients - using proper techniques you can ensure messages are not lost, just like gcm).

可以这样传统插座的方式在Android的实现呢?如果是这样,什么是GCM和插座的利弊?在哪些情况下会使用插座是合适的? WhatsApp的和许多其他应用程序的,据我所知,使用GCM。是否有其使用套接字应用程序?

Can this traditional socket approach be implemented in android as well? If so, what are the pros and cons of GCM and Sockets? In which situations would the use of sockets be appropriate? Whatsapp and lot of other apps, to my knowledge, use GCM. Are there apps which use sockets?

P.S:我只是一个C.S.学生;道歉,如果我有我的观念错了地方

P.S: I'm just a C.S. student; apologies if I've got my concepts wrong somewhere

推荐答案

GCM使用套接字太多。然而,它们是由系统管理,因此可以做的事情一个常规的应用程序不能做到的。一般地,对于任何类型的推的通知时,系统需要有很长的寿命的插座,以及通知是刚好在该插座接收某些数据。所有的这并不是太难实现一个始终连接机,对诸如桌面或服务器,但也有在移动了额外的挑战。例如,交换网络(3G&所述; - >的WiFi),外出的范围,保持不保持该设备唤醒所有的时间(和杀电池的过程中)的连接。所以,是的,你可以实现使用服务,插座和自己的服务器类似的东西,但它不是小事上移动。

GCM uses sockets too. However, they are managed by the system and thus can do things a regular app cannot do. Generally, for any type of 'push' notifications, the system needs to have a long-lived socket, and a 'notification' is just some data received on that socket. All of this is not too hard to implement on an always connected machine, such as a desktop or a server, but there are additional challenges on mobile. For example, switching networks (3G<->WiFi), going out of range, maintaining a connection without keeping the device awake all the time (and killing the battery in the process). So yes, you could implement something similar using a service , sockets and your own server, but it's not trivial on mobile.

查看 http://mqtt.org/ 一个开源实现。

 
精彩推荐
图片推荐