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

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

问题描述

我想将 Service 添加到我的 Android 应用程序中,该应用程序在后台持有 WebSocket 连接(可能超过几个小时甚至几天)并定期发送一些数据到服务器.

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.

现在似乎有一堆用于 Java 的 WebSocket 库,但我不确定应该使用哪个:

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

  • TooTallNate/Java-WebSocket Description from GitHub: A barebones WebSocket client and server implementation written in 100% Java. http://java-websocket.org/ -- This one is linked in my first result of googling "android websocket". However, it has quite a few open issues, especially about SSL connections, and it doesn't seem to be actively maintained at the moment.

koush/AndroidAsync来自 GitHub 的描述:用于 android 的异步套接字、http(客户端+服务器)、websocket 和 socket.io 库.基于 nio,而不是线程.-- 同样有许多未解决的问题,但似乎正在积极维护/处理.

koush/AndroidAsync Description from GitHub: Asynchronous socket, http (client+server), websocket, and socket.io library for android. Based on nio, not threads. -- Again many open issues, but seems to be activiley maintained/worked on.

泰鲁斯计划来自网站的描述:JSR 356:WebSocket 的 Java API - 参考实现-- 这是甲骨文制作的.不确定它是否适用于 Android.

Project Tyrus Description from Website: JSR 356: Java API for WebSocket - Reference Implementation -- This is made by Oracle. Not sure if it works in Android.

Jetty WebSocket 客户端 API来自网站的信息:Jetty 还提供了一个 Jetty WebSocket 客户端库来编写使与 WebSocket 服务器的对话更容易.-- 再次:不确定它是否适用于 Android.

Jetty WebSocket Client API Info from Website: Jetty also provides a Jetty WebSocket Client Library to write make talking to WebSocket servers easier. -- Again: Not sure if it works in Android.

codebutler/android-websockets来自 GitHub 的描述:适用于 Android 的最低限度的 websockets (hybi13/RFC) 客户端-- 这个在schwiz/android-websocket-example中使用,这是公认的StackOverflow 问题的答案如何让 Android 设备在没有唤醒锁的情况下保持与 Internet 的 TCP 连接?".

codebutler/android-websockets Description from GitHub: Bare minimum websockets (hybi13/RFC) client for Android -- This one is used in schwiz/android-websocket-example, which is the accepted answer for the StackOverflow-question "How to make the Android device hold a TCP connection to Internet without wake lock?".

大气/wasync来自 GitHub 的描述:带有后备传输客户端库的 WebSockets,适用于 Node.js、Android 和 Java http://async-io.org

TakahikoKawasaki/nv-websocket-client来自 GitHub 的描述:用 Java 实现高质量的 WebSocket 客户端.

TakahikoKawasaki/nv-websocket-client Description from GitHub: High-quality WebSocket client implementation in Java.

square/okhttp来自 GitHub 的描述:适用于 Android 和 Java 应用程序的 HTTP+SPDY 客户端.http://square.github.io/okhttp/-- 它有一个 Websocket 模块.正如 scorpiodawg 提到的,OkHttp 内置了自 3.5 版起支持 websocket.

square/okhttp Description from GitHub: An HTTP+SPDY client for Android and Java applications. http://square.github.io/okhttp/ -- It has a Websocket module. As mentioned by scorpiodawg, OkHttp has built-in websocket support since version 3.5.

firebase/TubeSock来自 GitHub 的描述:一个用 Java 实现的 WebSocket 客户端库

firebase/TubeSock Description from GitHub: A WebSocket client library implemented in Java

Autobahn|Android (GitHub)来自网站的描述:Autobahn|Android 是由 Autobahn 项目创建的 Java/Android 开源网络库,它实现了 WebSocket 协议和 Web 应用消息协议 (WAMP),用于创建原生移动 WebSocket/WAMP 客户端.-- cloudsurfin 指出这不支持对于 wss.

Autobahn|Android (GitHub) Description from Website: Autobahn|Android is an open-source networking library for Java/Android created by the Autobahn project that implements the WebSocket Protocol and the Web Application Messaging Protocol (WAMP) for creating native mobile WebSocket/WAMP clients. -- cloudsurfin pointed out that this has no support for wss.

此外,还有一个适用于 Android 的原生 socket.io 客户端库:

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

  • nkzawa/socket.io-client.java来自 GitHub 的描述:用于 Java 的全功能 Socket.IO 客户端库,兼容 Socket.IO v1.0 及更高版本.
  • 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.

使用 socket.io Android 客户端对我来说会很方便,因为我计划无论如何都将 nodejs/socket.io 用于 Web 前端.但是本地客户端还很年轻,并且有几个未解决的问题.除此之外,我的理解是,Android 应用程序没有任何使用 socket.io 客户端库的好处(除了与 socket.io 1.0 服务器兼容),因为可以在客户端确保 WebSocket 支持.

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.

我的要求如下:

  • 与 Android API 9 及更高版本的兼容性
  • 可以通过 SSL 连接
  • 长时间保持连接而无需持有永久唤醒锁
  • 与可用的 nodejs websocket 服务器实现或 socket.io 的兼容性

有什么建议适合这些要求的库吗?

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

推荐答案

一些注意事项.

  • koush/AndroidAsync 不执行 RFC 6455 要求的">关闭握手.请参阅了解详情.

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

Project Tyrus 适用于 Android,但请确保其许可证 (CDDL 1.1 和 GPL 2 with CPE) 及其大小 (使用 ProGuard 减少 WebSocket 客户端 jar 大小)满足您的要求.另请注意,当文本大小较大时,Tyrus 可能会引发异常(这可能是一个错误).请参阅了解详情.

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.

Jetty:2 年前的 电子邮件线程我们目前没有与 Android 兼容的 Jetty 9 WebSocket 客户端.有计划尝试将 Jetty WebSocket 客户端从 JDK 7 向后移植到 JDK 5/6 以供 Android 使用,但它的优先级低于我们完成 JSR-356 Java WebSocket API (javax.websocket) 的实现." Jetty 当前 document 关于其 WebSocket 客户端API 没有提及任何关于 Android 的内容.

Jetty: 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 不执行 关闭握手 这是RFC 6455 所要求的,并且可能关闭时抛出异常.请参阅.

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

Atmosphere/wasync 使用 AsyncHttpClient/async-http-client 作为其 WebSocket 实现.因此,应改为提及 AsyncHttpClient/async-http-client.

Atmosphere/wasync uses AsyncHttpClient/async-http-client as its WebSocket implementation. So, rather, AsyncHttpClient/async-http-client should be mentioned instead.

firebase/TubeSock 不验证 Sec-WebSocket-Accept.这违反了 RFC 6455.此外,TubeSock 在构建文本消息方面存在错误.如果您对文本消息使用多字节 UTF-8 字符,您迟早会遇到该错误.请参阅 问题 3.com/delight-im/Android-DDP" rel="nofollow noreferrer">delight-im/Android-DDP 有关 TubeSock 问题的长列表.

firebase/TubeSock does not verify Sec-WebSocket-Accept. This is a violation against RFC 6455. Also, TubeSock has a bug in building a text message. You will encounter the bug sooner or later if you use multi-byte UTF-8 characters for text messages. See Issue 3 in delight-im/Android-DDP for a long list about TubeSock problems.

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

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

  1. 合规.不少实现没有实现关闭握手RFC 6455.(如果没有实现关闭握手会发生什么?参见这个.)
  2. 所需的 Java 版本.Java SE 5、6、7、8 还是 Java EE?甚至可以在 Android 上使用?
  3. 尺寸.某些实现具有许多依赖项.
  4. wss 支持.
  5. HTTP 代理支持.
  6. 通过 HTTP 代理的 wss 支持.请参阅HTML5 Web Sockets 如何与代理服务器交互中的图 2,了解什么WebSocket 客户端库必须通过 HTTP 代理支持 wss.
  7. SSL 配置的灵活性.SSLSocketFactorySSLContext 应该能够在没有不必要限制的情况下使用.
  8. 打开握手,包括基本身份验证.
  9. HTTP 代理协商中的自定义 HTTP 标头,包括代理服务器上的身份验证.
  10. 是否能够发送所有帧类型(连续、二进制、文本、关闭、乒乓和乒乓).大多数实现不为开发人员提供发送碎片帧主动提供乒乓球框手动.
  11. 侦听器接口,用于接收各种 WebSocket 事件.糟糕的界面让开发人员感到沮丧.丰富的界面可帮助开发者编写强大的应用程序.
  12. 能否查询 WebSocket 状态.RFC 6455 定义了连接、打开、关闭和关闭状态,但很少有实现维护其内部以定义的方式进行状态转换.
  13. 能够为套接字连接设置超时值.(相当于 Socket.connect(SocketAddress endpoint, int timeout) 方法)
  14. 能够访问底层原始套接字.
  15. 直观易用的 API 与否.
  16. 是否有据可查.
  17. RFC 7692(WebSocket 压缩扩展)支持(又名 permessage-deflate).
  18. 重定向 (3xx) 支持.
  19. 摘要式身份验证支持.
  1. Compliance. Not a small number of implementations do not implement the closing handshake required by RFC 6455. (What happens if the closing handshake is not implemented? See this.)
  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. RFC 7692 (Compression Extensions for WebSocket) support (aka permessage-deflate).
  18. Redirection (3xx) support.
  19. Digest Authentication support.

nv-websocket-client 涵盖了所有以上除了最后两个.此外,其小而方便的功能之一是定期发送 ping/pong 帧.只需调用 setPingInterval/setPongInterval 方法即可实现(参见 JavaDoc).

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).

免责声明:Takahiko Kawasaki 是 nv-websocket-client 的作者.

Disclaimer: Takahiko Kawasaki is the author of nv-websocket-client.

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

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