gpt4 book ai didi

java - 无法连接到 wss [更新]

转载 作者:行者123 更新时间:2023-12-01 10:37:45 27 4
gpt4 key购买 nike

我是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

最佳答案

我建议您要么开始使用 maven/ivy/gradle 等工具,以便获得所有依赖项,要么您将处理单独的依赖项,并在以后遇到类似的问题。

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

关于java - 无法连接到 wss [更新],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34574611/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com