加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 23:48:12 所属栏目:Java 来源:网络整理
导读:我花了很长时间才找到一个简单的 java websocket客户端,可以使用wss而不会乱七八糟…… 我试过https://github.com/TooTallNate/Java-WebSocket 他在descirbes中添加了依赖,复制了SSLClientExample.java以使用websocket.org echo服务器进行测试,但是在第84行
我花了很长时间才找到一个简单的 java websocket客户端,可以使用wss而不会乱七八糟……

我试过https://github.com/TooTallNate/Java-WebSocket

他在descirbes中添加了依赖,复制了SSLClientExample.java以使用websocket.org echo服务器进行测试,但是在第84行遇到编译错误没有这样的方法setSocket()……(卡在这里)

我试过tyrus(似乎这是一个由oracle直接开发的大型库)但似乎我需要运行一些appserver(websocket容器)才能使用它…

我不知道对于那些需要使用netty或glassfish或grizly的websockets来说这么困难吗?

我认为可以使用SSLEngine(wss)和纯java sdk实现一个…有什么我不知道websockets? (我想它非常像普通的插座)

解决方法

nv-websocket-client是一个用Java编写的新WebSocket客户端库.它支持wss并且只需要Java SE 1.5,因此它甚至可以在Android上运行.

nv-websocket-client-1.3.jar(2015-05-06发布)的大小为62,854字节,不需要任何外部依赖.

下面是一个“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.");
    }
}

博客
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

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

Maven的

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读