无法连接到 wss [更新]

Posted

技术标签:

【中文标题】无法连接到 wss [更新]【英文标题】:Unable to connect to wss [Update] 【发布时间】:2016-01-03 08:18:01 【问题描述】:

我是java新手。 我想像这样连接到网络套接字:

import java.net.URI;
import java.net.URISyntaxException;

public class PrimeBot 

public static void main(String[] args) 
    // TODO Auto-generated method stub
    try 
        // open websocket
        final WebsocketClientEndpoint clientEndPoint = new WebsocketClientEndpoint(new URI("wss://example.com"));

        // add listener
        clientEndPoint.addMessageHandler(new WebsocketClientEndpoint.MessageHandler() 
            public void handleMessage(String message) 
                System.out.println(message);
            
        );

        // send message to websocket
        clientEndPoint.sendMessage("''");

        // wait 3 seconds for messages from websocket
        Thread.sleep(3000);

     catch (InterruptedException ex) 
        System.err.println("InterruptedException exception: " + ex.getMessage());
     catch (URISyntaxException ex) 
        System.err.println("URISyntaxException exception: " + ex.getMessage());
    


二等

import java.net.URI;
import javax.websocket.ClientEndpoint;
import javax.websocket.CloseReason;
import javax.websocket.ContainerProvider;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;

@ClientEndpoint
public class WebsocketClientEndpoint 

Session userSession = null;
private MessageHandler messageHandler;

public WebsocketClientEndpoint(URI endpointURI) 
    try 
        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
        container.connectToServer(this, endpointURI);
     catch (Exception e) 
        throw new RuntimeException(e);
    


/**
 * Callback hook for Connection open events.
 *
 * @param userSession the userSession which is opened.
 */
@OnOpen
public void onOpen(Session userSession) 
    System.out.println("opening websocket");
    this.userSession = userSession;


/**
 * Callback hook for Connection close events.
 *
 * @param userSession the userSession which is getting closed.
 * @param reason the reason for connection close
 */
@OnClose
public void onClose(Session userSession, CloseReason reason) 
    System.out.println("closing websocket");
    this.userSession = null;


/**
 * Callback hook for Message Events. This method will be invoked when a client send a message.
 *
 * @param message The text message
 */
@OnMessage
public void onMessage(String message) 
    if (this.messageHandler != null) 
        this.messageHandler.handleMessage(message);
    


/**
 * register message handler
 *
 * @param message
 */
public void addMessageHandler(MessageHandler msgHandler) 
    this.messageHandler = msgHandler;


/**
 * Send a message.
 *
 * @param user
 * @param message
 */
public void sendMessage(String message) 
    this.userSession.getAsyncRemote().sendText(message);


/**
 * Message handler.
 *
 * @author Jiji_Sasidharan
 */
public static interface MessageHandler 

    public void handleMessage(String message);


我包括: - gson-2.5.jar - javax.websocket-client-api-1.1.jar - tyrus-client-1.12.jar - tyrus-container-grizzly-client-1.12.jar

仍然出现错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/glassfish/tyrus/spi/ClientContainer
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.glassfish.tyrus.container.grizzly.client.GrizzlyContainerProvider.getContainer(GrizzlyContainerProvider.java:53)
at javax.websocket.ContainerProvider.getWebSocketContainer(ContainerProvider.java:67)
at WebsocketClientEndpoint.<init>(WebsocketClientEndpoint.java:24)
at PrimeBot.main(PrimeBot.java:10)
Caused by: java.lang.ClassNotFoundException
org.glassfish.tyrus.spi.ClientContainer
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 16 more

【问题讨论】:

您如何包含您的客户端 jar 以及您如何运行此应用程序? 这样我包含 jars:右键单击项目 -> 属性 -> Java 构建路径 -> 库 -> 添加外部 jars -> 确定我通过单击运行来运行此应用程序并选择“运行为Java 应用程序” 【参考方案1】:

我建议你要么开始使用像 maven/ivy/gradle 这样的工具,这样你就可以获得所有的依赖关系,或者你将处理单个依赖关系,并且以后会面临类似的问题。

为了解决您的问题,您需要在您的类路径中添加tyrus-spi-1.12.jar,您可以从here 下载它

【讨论】:

以上是关于无法连接到 wss [更新]的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 MQTT 连接到 wss?

无法使用 Flutter 连接到 wss

ClientWebSocket wss - 无法连接到远程服务器

无法从 https 客户端通过 wss 连接到 emqx mqtt 代理

棘轮和 WSS 无法正常工作

WebSocket 连接到“wss://argus.chi.shopify.io”失败:WebSocket 在连接建立之前关闭