简体   繁体   English

最小的 java8 nio 安全 websocket 客户端 (wss)

[英]minimal java8 nio secure websocket client (wss)

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?我认为可以使用 SSLEngine (wss) 和纯 Java sdk 来实现一个......我对 websockets 有什么不了解的地方吗? ( i imagine it very much like ordinary sockets) (我想象它很像普通的插座)

nv-websocket-client is a new WebSocket client library written in Java. nv-websocket-client是一个用 Java 编写的新的 WebSocket 客户端库。 It supports wss and requires just Java SE 1.5, so it can run even on Android.它支持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) WebSocket 客户端库(Java SE 1.5+,Android)
http://darutk-oboegaki.blogspot.jp/2015/05/websocket-client-library-java-se-15.html http://darutk-oboegaki.blogspot.jp/2015/05/websocket-client-library-java-se-15.html

GitHub GitHub
https://github.com/TakahikoKawasaki/nv-websocket-client https://github.com/TakahikoKawasaki/nv-websocket-client

JavaDoc文档
http://takahikokawasaki.github.io/nv-websocket-client/ http://takahikokawasaki.github.io/nv-websocket-client/

Maven马文

<dependency>
    <groupId>com.neovisionaries</groupId>
    <artifactId>nv-websocket-client</artifactId>
    <version>1.3</version>
</dependency>

Tyrus client does not need to have an appserver! Tyrus客户端不需要有应用程序服务器! :) :)

Please see Tyrus documentation and 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.关于大小 - 它可以最小化更多,但在 Tyrus 代码中进行了一些重构。 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). WebSocket 和普通套接字的比较不是很准确——普通套接字不需要实现 HTTP 并且(传统上)没有 NIO 支持(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.另一部分是 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.所以我想你可以找到更简单的 API 实现,但是坚持一些维护的东西并且是 Java EE 的一部分对我来说似乎并不坏 - 你有可能选择实现(Tyrus 只是其中之一,还有其他的) 并且您的客户端将准备好包含在 Java EE 应用程序中(如果发生这种情况)。 (Editors note: I work on Tyrus, so my answer is most likely biased). (编者注:我在 Tyrus 上工作,所以我的回答很可能是有偏见的)。

Try Matthias's simple-websocket-client out.试试 Matthias 的 simple-websocket-client。 (I'm just one of his followers in Twitter) You may agree his underlying intention of that project. (我只是他在 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. JSR 356 描述了 WebSocket 服务器和客户端的标准 API,但客户端 API 非常复杂。 This project aims to offer a simplified client.该项目旨在提供一个简化的客户端。

BTW, Any WebSocket client doesn't require WebSocket server and it's possible to write your own client on pure Java SE.顺便说一句,任何 WebSocket 客户端都不需要 WebSocket 服务器,并且可以在纯 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.然而,由于易于与其他技术集成比简单更重要,您可能会觉得有一些看起来复杂和不必要的底层上下文。

GO for Java Webscoket API,简单而优雅。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM