gpt4 book ai didi

java - 在websocket客户端上配置Jetty密码教程

转载 作者:行者123 更新时间:2023-12-02 04:49:42 33 4
gpt4 key购买 nike

我正在关注tutorial使用 Eclipse 让 Web 套接字客户端在 Java 中工作,但每当我尝试运行程序代码时,我都会遇到此消息:

用法 - java org.eclipse.jetty.security.Password []

如果密码是?,系统将提示用户输入密码

这是客户端的代码


public class SimpleEchoClient
{
public static void main(String[] args)
{
String destUri = "ws://echo.websocket.org";
if (args.length > 0)
{
destUri = args[0];
}

WebSocketClient client = new WebSocketClient();
SimpleEchoSocket socket = new SimpleEchoSocket();
try
{
client.start();

URI echoUri = new URI(destUri);
ClientUpgradeRequest request = new ClientUpgradeRequest();
client.connect(socket,echoUri,request);
System.out.printf("Connecting to : %s%n",echoUri);

// wait for closed socket connection.
socket.awaitClose(5,TimeUnit.SECONDS);
}
catch (Throwable t)
{
t.printStackTrace();
}
finally
{
try
{
client.stop();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}

这是 websocket 连接的代码

@WebSocket(maxTextMessageSize = 64 * 1024)
public class SimpleEchoSocket
{
private final CountDownLatch closeLatch;
@SuppressWarnings("unused")
private Session session;

public SimpleEchoSocket()
{
this.closeLatch = new CountDownLatch(1);
}

public boolean awaitClose(int duration, TimeUnit unit) throws InterruptedException
{
return this.closeLatch.await(duration,unit);
}

@OnWebSocketClose
public void onClose(int statusCode, String reason)
{
System.out.printf("Connection closed: %d - %s%n",statusCode,reason);
this.session = null;
this.closeLatch.countDown(); // trigger latch
}

@OnWebSocketConnect
public void onConnect(Session session)
{
System.out.printf("Got connect: %s%n",session);
this.session = session;
try
{
Future<Void> fut;
fut = session.getRemote().sendStringByFuture("Hello");
fut.get(2,TimeUnit.SECONDS); // wait for send to complete.

fut = session.getRemote().sendStringByFuture("Thanks for the conversation.");
fut.get(2,TimeUnit.SECONDS); // wait for send to complete.

session.close(StatusCode.NORMAL,"I'm done");
}
catch (Throwable t)
{
t.printStackTrace();
}
}

@OnWebSocketMessage
public void onMessage(String msg)
{
System.out.printf("Got msg: %s%n",msg);
}
}

有人知道如何解决这个问题吗?

最佳答案

该教程和代码示例没有密码要求。

您收到的消息是因为您直接运行 org.eclipse.jetty.security.Password 类,而不是您的 websocket 客户端代码。

更改运行已编译项目的方式以运行正确的类(从外观上看,这将是您的类 SimpleEchoClient,但您没有显示包 namespace ,因此类名可以是不完整)

关于java - 在websocket客户端上配置Jetty密码教程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56454005/

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