我正在尝试连接到安全的websocket服务器,我正在使用
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-client</artifactId>
<version>9.3.8.RC0</version>
</dependency>
作为一个库,我们为服务器和客户端提供了相互身份验证我有这里有错误的密钥库或者安全websocket客户端的任何其他实现。 这是我的代码,
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.jetty.*;
public class WebSocketClientTest {
public static void main(String[] args) throws IOException, URISyntaxException{
new WebSocketClientTest().run(new URI("wss:/websocket:443/secure/12234"));
}
public void run(URI destinationUri) throws IOException
{
SslContextFactory sslContextFactory = new SslContextFactory();
Resource keyStoreResorce = Resource.newResource(this.getClass().getResource("certstore.jks"));
sslContextFactory.setKeyStoreResource(keyStoreResorce);
sslContextFactory.setKeyStorePassword("changeit!");
sslContextFactory.setKeyManagerPassword("");
WebSocketClient client = new WebSocketClient();
MyWebSocket socket = new MyWebSocket();
try{
client.start();
ClientUpgradeRequest request = new ClientUpgradeRequest();
System.out.println("Connecting to : "+ destinationUri);
client.connect(socket, destinationUri, request);
System.out.println("line x1");
socket.awaitClose(10, TimeUnit.SECONDS);
System.out.println("line x2");
}catch (Throwable t){
System.out.println(t.getStackTrace());
}finally{
try{
client.stop();
}catch(Exception e){
e.printStackTrace();
}
}
}
@WebSocket
public class MyWebSocket {
private final CountDownLatch closeLatch = new CountDownLatch(1);
@OnWebSocketConnect
public void onConnect(Session session){
System.out.println("WebSocket Opened in client side");
try{
System.out.println("Sending message : Ready");
session.getRemote().sendString("Ready");
}catch (IOException e){
e.printStackTrace();
}
}
@OnWebSocketMessage
public void onMessage(String message){
System.out.println("Message from server : "+message);
}
@OnWebSocketClose
public void onClose(int statusCode, String reason){
System.out.println("WebSocket Closed. code:"+ statusCode);
}
public boolean awaitClose(int duration, TimeUnit unit) throws InterruptedException{
return this.closeLatch.await(duration, unit);
}
}
}