其中的WebSocket库在Android应用程序使用? [英] Which WebSocket library to use in Android app?

查看:576
本文介绍了其中的WebSocket库在Android应用程序使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个服务添加到我的Andr​​oid应用程序,它在后台运行,拿着<一个HREF =htt​​p://en.wikipedia.org/wiki/WebSocket>的WebSocket 连接(可能在几个小时甚至几天),并定期发送一些数据到服务器。

现在似乎有一堆的WebSocket库的Java,我不知道我应该使用哪一个:

  • TooTallNate / Java的WebSocket的 说明从GitHub上:用100%纯Java准系统的WebSocket客户端和服务器实现。 http://java-websocket.org/ - 这一个是在谷歌上搜索我的第一个结果机器人WebSocket的。但是,它有相当多的悬而未决的问题,特别是有关SSL连接,它似乎并没有得到积极的维护的时刻。

  • koush / AndroidAsync 说明从GitHub上:异步接口,HTTP(客户端+服务器),WebSocket的,和socket.io库机器人。基于NIO,而不是线程。 - 此外许多悬而未决的问题,但似乎是activiley维护/制作

  • 项目泰鲁斯 说明从网站:JSR 356:Java进行的WebSocket API - 参考实现 - 这是由甲骨文进行。不知道它在Android的。

  • 码头的WebSocket客户端API 信息从网站:码头还提供了一个码头的WebSocket客户端库写入使交谈的WebSocket服务器更容易。 - 还是那句话:。不知道,如果它工作在Android的

  • codebutler / Android系统的WebSockets 说明从GitHub上:裸最小的WebSockets(hybi13 / RFC)客户端Android版 - 这一次是用在 schwiz / Android系统的WebSocket-例如,这对于StackOverflow-接受的答案问题:<一href="http://stackoverflow.com/questions/13534732/how-to-make-the-android-device-hold-a-tcp-connection-to-internet-without-wake-lo">How使Android设备没有持有唤醒锁TCP连接到互联网?。

  • 大气/ wasync 从GitHub上说明:的WebSockets与备用传输的Node.js的,Android和Java的 http://async-io.org

  • <一个href="https://github.com/TakahikoKawasaki/nv-websocket-client">TakahikoKawasaki/nv-websocket-client 说明从GitHub的:在Java中高品质的WebSocket客户端实现

  • 平方米/ okhttp 说明从GitHub上:Android和Java应用程序的HTTP + SPDY客户端。 http://square.github.io/okhttp/ - 它有一个的WebSocket模块

此外,还有一个原生 socket.io 客户端Android版库:

要使用socket.io Android客户端将方便我,因为我打算使用nodejs / socket.io为Web前端反正。但是,本地客户端是相当年轻,有几个悬而未决的问题。而且除了这,这是我的理解是一个Android应用程序没有使用socket.io客户端库(除了符合socket.io 1.0服务器兼容)的任何好处,因为WebSocket的支持,可以保证在客户端

我的要求如下:

  • 在与Android API 9和更高
  • 兼容性
  • 的可能性通过SSL连接
  • 在保持长时间的连接,而无需持有永久wakelock
  • 在一个可用nodejs的WebSocket服务器实现或socket.io
  • 兼容性

其中之一任何建议是正确的库这些要求?

解决方案

一些注意事项。

  • koush / AndroidAsync不执行收盘握手这是需要通过的 RFC 6455 。请参见了解详细信息。

  • 项目泰鲁斯 - 适用于Android的,但要确保其许可证( CDDL 1.1和GPL 2 CPE )和它的大小(减少的WebSocket客户端jar大小使用ProGuard )满足您的要求。还要注意的是,当一个文字大小大(它可能是一个错误),泰鲁斯 - 可能会抛出异常。请参见了解详细信息。

  • 在码头用户2年同期电子邮件线程邮件列表说的我们目前还没有Android的兼容码头9 WebSocket的客户端,有计划地试图从JDK 7的反向移植码头的WebSocket客户端的JDK 5/6为Android应用,但其优先级低于整理我们的实现对JSR-356的Java的WebSocket API(javax.websocket)。的码头目前的<一href="http://www.eclipse.org/jetty/documentation/current/jetty-websocket-client-api.html">document关于它的WebSocket客户端API并没有提及任何关于Android的东西。

  • codebutler / Android系统的WebSocket不执行收盘握手这是需要通过< A HREF =htt​​ps://tool​​s.ietf.org/html/rfc6455> RFC 6455 并可以扔在靠近异常。请参见

审议的问题

在选择用Java编写的WebSocket的客户端实现的思考点:

  1. 符合。不是一个小数目的实现不实现收盘握手所需的RFC 6455
  2. 所需的Java版本。的Java SE 5,6,7,8或Java EE?即使在Android上工作?
  3. 尺寸。有些实现有许多依赖关系。
  4. WSS 的支持。
  5. HTTP代理的支持。
  6. WSS over HTTP代理的支持。参见图2中如何HTML5的网络套接字互动与代理服务器有关什么的WebSocket客户端库有也支持WSS通过HTTP代理服务器。
  7. 在SSL配置的灵活性 SSLSocketFactory的的SSL连接应该能够避免不必要的限制使用。
  8. 开幕握手 ,其中包括基本身份验证自定义HTTP标头。
  9. 自定义HTTP标头在HTTP代理谈判,包括在代理服务器认证。
  10. 能够发送所有的帧类型(延续,二进制,文本,近距,ping和乒乓球)与否。大多数实现不为开发人员提供工具发送零散帧并的不请自来的的< A HREF =htt​​p://tool​​s.ietf.org/html/rfc6455#section-5.5.3>乒乓帧手动。
  11. Listener接口,以接收各种WebSocket的事件。一个可怜的界面使开发者感到沮丧。丰富的接口可以帮助开发人员在编写健壮的应用程序。
  12. 能打听的WebSocket状态或没有。 RFC 6455定义进行连接,打开,关闭和关闭状态,但很少实现维持定义的方式其内部状态转换。
  13. 能够设置套接字连接的超时值。 (相当于插座的第二个参数。<一个href="http://docs.oracle.com/javase/8/docs/api/java/net/Socket.html#connect-java.net.SocketAddress-int-">connect(SocketAddress端点,INT超时)法)
  14. 能够访问底层的原始套接字
  15. 直观易于使用的API 或没有。
  16. 详细记录或没有。
  17. 重定向(3XX)的支持。
  18. 摘要式身份验证的支持。

NV-的WebSocket客户端 涵盖上述所有,除了最后两个。此外,它的小而方便的功能之一是定期发送平/乒乓球帧。 // takahikokawasaki:它可以只通过调用 setPingInterval / setPongInterval 方法(见的JavaDoc )。

I want to add a Service to my Android app which runs in the background holding a WebSocket connection (possibly over several hours or even days) and regularly sends some data to a server.

Now there seems to be a bunch of WebSocket libraries for Java, and I'm not sure which one I should use:

In addition, there is a native socket.io client library for Android:

  • nkzawa/socket.io-client.java Description from GitHub: Full-featured Socket.IO Client Library for Java, which is compatible with Socket.IO v1.0 and later.

To use the socket.io Android client would be handy for me, because I plan to use nodejs/socket.io for the web frontend anyway. But the native client is quite young and has several open issues. And in addition to that, it is my understanding that an android app does not have any benefit of using the socket.io client library (apart from being compatible with socket.io 1.0 server), because WebSocket support can be assured at the client side.

My requirements are as follows:

  • Compatibility with Android API 9 and higher
  • Possibility to connect via SSL
  • Keep the connection for a long time without having to hold a permanent wakelock
  • Compatibility with an available nodejs websocket server implementation or with socket.io

Any suggestions which one is the right library for these requirements?

解决方案

Some notes.

  • koush/AndroidAsync does not perform the closing handshake which is required by RFC 6455. See this for details.

  • Project Tyrus works on Android, but make sure that its license (CDDL 1.1 and GPL 2 with CPE) and its size (Reducing WebSocket client jar size with ProGuard) meet your requirements. Also note that Tyrus may throw an exception when a text size is large (it's probably a bug). See this for details.

  • A 2-year-ago email thread in jetty-users mailing list says "We currently have no Android compatible Jetty 9 WebSocket client. There are plans to attempt to backport the Jetty WebSocket Client from JDK 7 to JDK 5/6 for android use, but its a lower priority than finishing our implementation of JSR-356 Java WebSocket API (javax.websocket)." Jetty's current document about its WebSocket Client API does not mention anything about Android.

  • codebutler/android-websocket does not perform the closing handshake which is required by RFC 6455 and may throw an exception on close. See this.

Consideration Points

Consideration points in selecting a WebSocket client implementation written in Java:

  1. Compliance. Not a small number of implementations do not implement the closing handshake required by RFC 6455.
  2. Required Java version. Java SE 5, 6, 7, 8 or Java EE? Works even on Android?
  3. Size. Some implementations have many dependencies.
  4. wss support.
  5. HTTP proxy support.
  6. wss over HTTP proxy support. See Figure 2 in How HTML5 Web Sockets Interact With Proxy Servers about what a WebSocket client library has to do to support wss over HTTP proxy.
  7. Flexibility on SSL configuration. SSLSocketFactory and SSLContext should be able to be utilized without unnecessary restrictions.
  8. Custom HTTP headers in the opening handshake, including Basic Authentication.
  9. Custom HTTP headers in HTTP proxy negotiation, including authentication at the proxy server.
  10. Capable of sending all the frame types (continuation, binary, text, close, ping and pong) or not. Most implementations do not provide developers with means to send fragmented frames and unsolicited pong frames manually.
  11. Listener interface to receive various WebSocket events. A poor interface makes developers frustrated. A rich interface helps developers write robust applications.
  12. Able to inquire WebSocket state or not. RFC 6455 defines CONNECTING, OPEN, CLOSING and CLOSED states, but few implementations maintain their internal state transition in the defined way.
  13. Able to set a timeout value for socket connection. (Equivalent to the second argument of Socket.connect(SocketAddress endpoint, int timeout) method)
  14. Able to access the underlying raw socket.
  15. Intuitive easy-to-use API or not.
  16. Well-documented or not.
  17. Redirection (3xx) support.
  18. Digest Authentication support.

nv-websocket-client covers all the above except the last two. In addition, one of its small but convenient features is to send ping/pong frames periodically. It can be achieved just by calling setPingInterval/setPongInterval methods (See JavaDoc).

这篇关于其中的WebSocket库在Android应用程序使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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