客户端(android)从服务器(java)接收消息
我正在尝试创建一个聊天应用程序。通信基于 RESTful Web 服务(泽西岛)。客户端与发送 HTTP 请求的服务器进行通信。我的问题是如何在客户端不先发送请求的情况下从服务器向客户端发送消息。我读到了有关 C2DM 通知的内容,我想使用这种方式,当客户端从服务器收到通知时,(客户端)必须向服务器发送请求才能接收数据。我也在考虑使用套接字,但我不知道这是否可能,因为我正在使用 Rest。我的意思是在服务器端我也可以使用带有 REST 代码的套接字(在较低级别运行的套接字)吗?我唯一可以使用套接字工作的是使用套接字创建另一个服务器(两个服务器 RESTful 和套接字),但这是一个好的解决方案吗?我还可以在客户端放置一些服务器代码,在服务器端放置一些客户端代码吗?它会是正确且有效的吗?你怎么认为?哪种解决方案是最好的?
I am trying to create a chat application. The communication is based on RESTful web services(Jersey). The client communicates with the server sending HTTP requests. My problem is how to send messages from server to client without the client send a request first. I read about C2DM notifications, I suppose using this way, when the client receives the notification from the server then (the client) has to send a request to the server in order to receive the data. I also was thinking about using sockets but I don't know if it is possible as I am using Rest. I mean in the server side can I also use sockets(sockets run in lower level) with the REST code? The only thing I have though that could work using sockets is to create another server with socket(two servers RESTful and socket), but is this a good solution? Also could I put some server code in the client side and some client code in the server side. Would it be correct and effective? What do you think? Which solution would be the best?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
自 2012 年 6 月 26 日起,C2DM 已被 Google 正式弃用。我们鼓励现有 C2DM 开发人员迁移到新版本的 C2DM,即 Google Cloud Messaging for Android (GCM)。
查看此链接了解更多信息
http://developer.android.com/guide /google/gcm/index.html
C2DM has been officially deprecated as of June 26, 2012 by google. Existing C2DM developers are encouraged to migrate to the new version of C2DM, called Google Cloud Messaging for Android (GCM).
Check out this link for more info
http://developer.android.com/guide/google/gcm/index.html
您描述的场景听起来非常适合 C2DM。如果您构建自己的套接字设置,您将耗尽电池和 CPU 资源来执行手机已经在执行的操作 - 维护打开的套接字以接收消息。
C2DM 的设置相对简单,而且根据我的经验,它相当可靠。
这是我见过的最好的教程:
http://www.vogella.de/ articles/AndroidCloudToDeviceMessaging/article.html
但您还需要阅读官方文档:
http://code.google.com/android/c2dm/
您的服务器端代码需要向 C2DM 服务器发送 HTTP 请求,以向设备发送消息。为了告诉它向哪里发送消息,您的服务器需要指定注册 ID(代表您的应用程序在一台特定设备上运行),因此当您的应用程序在 Android 设备上设置时,它会向 C2DM 注册,然后需要发送将其 C2DM 注册 ID 存储到您的服务器中,以备用于发送消息。
The scenario you describe sounds ideal for C2DM. If you build your own sockets set up you will drain battery and CPU resources doing something the phone is already doing - maintaining an open socket to receive messages.
C2DM is relatively straightforward to set up, and in my experience is pretty reliable.
This is the best tutorial I have seen on it:
http://www.vogella.de/articles/AndroidCloudToDeviceMessaging/article.html
But you also need to read the official docs:
http://code.google.com/android/c2dm/
Your server-side code will need to send an HTTP request to the C2DM servers to send a message to a device. To tell it where to send the message, your server needs to specify the Registration ID (which represents your app running on one specific device) and so when your app sets up on the Android device, it registers with C2DM, and then needs to send its C2DM Registration ID to your server to be stored, ready to be used to send messages to.