java 最小的 java8 nio 安全 websocket 客户端 (wss)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29384467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
minimal java8 nio secure websocket client (wss)
提问by vach
I've spent quite some time to find simple java websocket client that could work with wss and wont be a mess...
我花了相当多的时间来找到可以与 wss 一起使用并且不会一团糟的简单 java websocket 客户端...
I've tried https://github.com/TooTallNate/Java-WebSocket
我试过https://github.com/TooTallNate/Java-WebSocket
added dependency as he descirbes, copied the SSLClientExample.java to test it with websocket.org echo server, but got compile error at line 84 no such method setSocket()... (stuck here)
在他描述时添加了依赖项,复制了 SSLClientExample.java 以使用 websocket.org 回显服务器对其进行测试,但在第 84 行出现编译错误,没有这样的方法 setSocket()...(卡在这里)
I tried tyrus (seems this is a big library developed directly by oracle) but it seems i need to have some appserver running (websocket container) to be able to use it...
我尝试了 tyrus(似乎这是一个由 oracle 直接开发的大型库),但似乎我需要运行一些应用程序服务器(websocket 容器)才能使用它...
I wonder whats so difficult about websockets that one needs to have netty or glassfish or grizly for that?
我想知道 websockets 有什么难点,以至于需要使用 netty 或 glassfish 或 grizly 才能做到这一点?
I think its possible to implement one using SSLEngine (wss) and pure java sdk... is there something i dont know about websockets? ( i imagine it very much like ordinary sockets)
我认为可以使用 SSLEngine (wss) 和纯 java sdk 来实现一个......我对 websockets 有什么不了解的地方吗?(我想象它很像普通的插座)
回答by Takahiko Kawasaki
nv-websocket-clientis a new WebSocket client library written in Java. It supports wssand requires just Java SE 1.5, so it can run even on Android.
nv-websocket-client是一个用 Java 编写的新的 WebSocket 客户端库。它支持wss并且只需要 Java SE 1.5,因此它甚至可以在 Android 上运行。
The size of nv-websocket-client-1.3.jar
(released on 2015-05-06) is 62,854 bytes and it does not require any external dependencies.
的大小nv-websocket-client-1.3.jar
(于 2015-05-06 发布)为 62,854 字节,它不需要任何外部依赖项。
Below is a "wss" example.
下面是一个“wss”示例。
import com.neovisionaries.ws.client.*;
public class HelloWSS
{
public static void main(String[] args) throws Exception
{
// Connect to "wss://echo.websocket.org" and send "Hello." to it.
// When a response from the WebSocket server is received, the
// WebSocket connection is closed.
new WebSocketFactory()
.createSocket("wss://echo.websocket.org")
.addListener(new WebSocketAdapter() {
@Override
public void onTextMessage(WebSocket ws, String message) {
// Received a response. Print the received message.
System.out.println(message);
// Close the WebSocket connection.
ws.disconnect();
}
})
.connect()
.sendText("Hello.");
}
}
Blog
WebSocket client library (Java SE 1.5+, Android)
http://darutk-oboegaki.blogspot.jp/2015/05/websocket-client-library-java-se-15.html
博客
WebSocket 客户端库(Java SE 1.5+,Android)
http://darutk-oboegaki.blogspot.jp/2015/05/websocket-client-library-java-se-15.html
GitHub
https://github.com/TakahikoKawasaki/nv-websocket-client
GitHub
https://github.com/TakahikoKawasaki/nv-websocket-client
JavaDoc
http://takahikokawasaki.github.io/nv-websocket-client/
JavaDoc
http://takahikokawasaki.github.io/nv-websocket-client/
Maven
马文
<dependency>
<groupId>com.neovisionaries</groupId>
<artifactId>nv-websocket-client</artifactId>
<version>1.3</version>
</dependency>
回答by Pavel Bucek
Tyrusclient does not need to have an appserver! :)
Tyrus客户端不需要有应用程序服务器!:)
Please see Tyrus documentationand blogpost Reducing WebSocket client jar size with ProGuard(you can get down to 500 kB with JDK 7+).
请参阅Tyrus 文档和博客文章Reducing WebSocket client jar size with ProGuard(使用 JDK 7+ 可以减少到 500 kB)。
About the size - it can be minimized even more, but with some refactoring in Tyrus code. The comparison of WebSocket and plain socket is not very accurate - plain socket does not need to implement HTTP and (traditionally) did not have NIO support (that came with Java 7). Another part is the WebSocket protocol implementation, which is not that difficult but also its not just sending byte[] to the wire - there is some opening handshake, signalling frames and mandatory strict UTF-8 encoding/decoding.
关于大小 - 它可以最小化更多,但在 Tyrus 代码中进行了一些重构。WebSocket 和普通套接字的比较不是很准确——普通套接字不需要实现 HTTP 并且(传统上)没有 NIO 支持(Java 7 附带的)。另一部分是 WebSocket 协议实现,它并不那么困难,而且不仅仅是将 byte[] 发送到线路 - 有一些打开握手、信令帧和强制严格的 UTF-8 编码/解码。
So I guess you could find more simple API implementation, but sticking to something which is maintained and is part of Java EE does not seem bad to me - you have the possibility to choose the implementation (Tyrus is just one of them, there are others) and your client will be ready for inclusion to Java EE application if that would ever happen. (Editors note: I work on Tyrus, so my answer is most likely biased).
所以我想你可以找到更简单的 API 实现,但是坚持一些维护的东西并且是 Java EE 的一部分对我来说似乎并不坏 - 你有可能选择实现(Tyrus 只是其中之一,还有其他) 并且如果发生这种情况,您的客户端将准备好包含到 Java EE 应用程序中。(编者注:我在 Tyrus 上工作,所以我的回答很可能有偏见)。
回答by Donghwan Kim
Try Matthias's simple-websocket-client out. (I'm just one of his followers in Twitter) You may agree his underlying intention of that project.
试试 Matthias 的 simple-websocket-client。(我只是他在 Twitter 上的追随者之一)您可能同意他对该项目的潜在意图。
From https://github.com/matzew/simple-websocket-client
来自https://github.com/matzew/simple-websocket-client
The JSR 356 describes a standard API for WebSocket Server and clients, but the client API is very complex. This project aims to offer a simplified client.
JSR 356 描述了 WebSocket 服务器和客户端的标准 API,但客户端 API 非常复杂。该项目旨在提供一个简化的客户端。
BTW, Any WebSocket client doesn't require WebSocket server and it's possible to write your own client on pure Java SE. However, since ease to integrate with other technologies is much important than just simpleness, you might feel there's some underlying context which looks complex and unnecessary.
顺便说一句,任何 WebSocket 客户端都不需要 WebSocket 服务器,并且可以在纯 Java SE 上编写自己的客户端。然而,由于易于与其他技术集成比简单更重要,您可能会觉得有一些看起来复杂和不必要的底层上下文。
回答by Santosh Kumar
GO for Java Webscoket API, Simple And elegant.
GO for Java Webscoket API,简单而优雅。