Phonegap 应用程序中使用 Node.js/Socket.IO 服务器和客户端的 Android WebSockets
我正在 PhoneGap 中制作 Android 应用程序。我想做的是让应用程序通过 WebSockets 与我的 Node.js 服务器通信。我的 Node.js 服务器使用 Socket.IO,当我打开应用程序时,它会自动回退到轮询,这与桌面 Chrome 应用程序相反,桌面 Chrome 应用程序会愉快地打开 WebSocket 并通过它进行通信。
我已阅读这篇博文 关于将实际的 WebSocket API 与 Phonegap 集成。问题是我没有手动重写“onConnect、onMessage”函数,而是 Socket.IO 为我完成了所有这些工作。
有什么方法可以将 WebSockets 集成到我的 Android Phonegap 应用程序中吗?
I am making an Android application in PhoneGap. What I'm trying to do is let the application talk to my Node.js server through WebSockets. My Node.js server uses Socket.IO which automatically falls back to polling when I open the application up, in contrary to the desktop Chrome application which happily opens up a WebSocket and communicates through it just fine.
I've read this blogpost about integrating the actual WebSocket API with Phonegap. The problem there is that I'm not overriding 'onConnect, onMessage' functions manually, instead Socket.IO does all that for me.
Is there some way to integrate WebSockets into my Android Phonegap application?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
简短回答:Cordova WebView 不支持 WebSocket,并且 socket.io 不连接到基于标准的 WebSocket 客户端。
对于您的客户来说,如果您想要真正的 websocket,您仍然需要使用特定于 Android 版本或 iOS 版本的 Cordova 插件。尝试此搜索,其中包含 Android 插件的 anismiles 存储库,相同OP 引用的博主。
因此,考虑到这一点,socket.io 将不适用于您的服务器。不幸的是,socket.io 服务器不支持使用基于 Html5 标准的 websocket 连接来连接到客户端,您必须使用他们的客户端库。正如您所看到的,您无法在 Cordova 中使用他们的客户端库...好吧,您可以,它只会回退到轮询。
所以现在你的 websocket 客户端是一个基于标准的 Cordova 插件,你需要一个支持基于标准的 websocket 连接的服务器。你应该看看SockJs,Worlize,Miksago 或 Einaros。还有其他的。我目前正在使用 Worlize。
另一件需要记住的事情是,目前支持真正的 Websocket 连接的云主机有一个简短的列表。我推荐 DotCloud 或 Nodejitsu。
如果这回答了您的问题,请单击复选标记:)
Short answer: Cordova WebView doesn't support WebSockets and socket.io doesn't connect to standards-based WebSocket clients.
For your client, it still appears that if you want real websockets, you need to use a Cordova plugin that is specific to an Android build or an iOS build. Try this search, which includes anismiles repo for an Android plugin, the same blogger referenced by the OP.
So with that in mind, socket.io will not work for your server. Unfortunately, socket.io server does not support connecting to clients with an Html5 standards-based websocket connection, you have to use their client library. As you've seen, you can't use their client library in Cordova...well you can, it'll just fallback to polling.
So now your websocket client is a standards-based Cordova plugin, you need a server that supports a standards-based websocket connection. You should take a look at SockJs, Worlize, Miksago, or Einaros. There are others. I'm currently using Worlize.
Another thing to keep in mind is that there is a short list of cloud hosts currently supporting true websocket connections. I recommend DotCloud or Nodejitsu.
If this answers your question please click the check mark :)
更新答案,这个插件与socket.io一起使用,并且使用起来更容易(仅限PhoneGap 3.x)。
https://github.com/mkuklis/phonegap-websocket
Updating the answers, this plugin works with socket.io and it's much easier to use (PhoneGap 3.x only).
https://github.com/mkuklis/phonegap-websocket
该存储库将很快集成到phonegap中(至少听起来像自述文件中的那样)
它还提供了使Web套接字在phonegap / android中工作的步骤。
看一下:https://github.com/anismiles/websocket-android-phonegap
This repository will be integrated in phonegap very soon (at least it sounds like that in the readme)
It also also provides the steps for making web sockets work in phonegap / android.
Take a look: https://github.com/anismiles/websocket-android-phonegap
这是我目前正在评估的另一个 websockets Android 客户端。
http://jwebsocket.org/mobile/android/android_part1.htm
恐怕我不知道它是否对phonegap项目有用,因为我不熟悉它。
Phonegap 需要允许您将外部 java 库合并到您的项目中,并且您需要用 java 为其构建一个接口。
Here is another websockets Android client, that I am currently evaluating.
http://jwebsocket.org/mobile/android/android_part1.htm
I'm afraid I have no idea if it will be useful to a phonegap project, not being familiar with it.
Phonegap would need to allow you to incorporate an external java library into your project and you would need to build an interface for it in java.