我正在学习websocket编程,当我去连接WebSocketStompClient时,我得到了这个异常org.springframework.messaging.simp.stomp.ConnectionLostException: Connection closed
所以它没有正确连接可以帮助我吗?
这是我的代码:
public class HelloClient {
private final static WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
public void connect() throws InterruptedException {
Transport webSocketTransport = new WebSocketTransport(new StandardWebSocketClient());
List<Transport> transports = Collections.singletonList(webSocketTransport);
SockJsClient sockJsClient = new SockJsClient(transports);
sockJsClient.setMessageCodec(new Jackson2SockJsMessageCodec());
WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);
String auth = "t:t";
String base64Credentials = Base64.getEncoder().encodeToString(auth.getBytes());
headers.add("Authorization", "Basic " + base64Credentials);
String url = "ws://188.75.73.196:7777/datxpapi/dtxws";
stompClient.setInboundMessageSizeLimit(1000);
ListenableFuture<StompSession> s= stompClient.connect(url, headers, new MySessionHandler());
if(s.isDone()){
System.out.println("s done working");
}
s.addCallback(new ListenableFutureCallback<StompSession>() {
@Override
public void onSuccess(StompSession response) {
System.out.println("Success");
System.out.println(response);
}
@Override
public void onFailure(Throwable t) {
System.out.println("error");
}
});
}
public class MySessionHandler extends StompSessionHandlerAdapter {
@Override
public void afterConnected(StompSession session,
StompHeaders connectedHeaders) {
System.out.println("after connect");
}
@Override
public void handleException(StompSession stompSession, StompCommand stompCommand, StompHeaders stompHeaders, byte[] bytes, Throwable throwable) {
System.out.println("err0");
}
@Override
public void handleTransportError(StompSession stompSession, Throwable throwable) {
if (throwable instanceof ConnectionLostException) {
System.out.println(throwable); }
}
}
public static void main(String[] args) throws Exception {
HelloClient helloClient = new HelloClient();
helloClient.connect();
}
}
完整追踪: 在org.springframework.messaging.simp.stomp.DefaultStompSession.afterConnectionClosed(DefaultStompSession.java:459) 在org.springframework.web.socket.messaging.WebSocketStompClient $ WebSocketTcpConnectionHandlerAdapter.afterConnectionClosed(WebSocketStompClient.java:353) 在org.springframework.web.socket.sockjs.client.AbstractClientSockJsSession.afterTransportClosed(AbstractClientSockJsSession.java:321) 在org.springframework.web.socket.sockjs.client.WebSocketTransport $ ClientSockJsWebSocketHandler.afterConnectionClosed(WebSocketTransport.java:172) 在org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter.onClose(StandardWebSocketHandlerAdapter.java:143) at org.eclipse.jetty.websocket.jsr356.endpoints.JsrEndpointEventDriver.onClose(JsrEndpointEventDriver.java:128) at org.eclipse.jetty.websocket.jsr356.endpoints.AbstractJsrEventDriver.onClose(AbstractJsrEventDriver.java:80) 在org.eclipse.jetty.websocket.common.WebSocketSession.notifyClose(WebSocketSession.java:369) 在org.eclipse.jetty.websocket.common.WebSocketSession.onConnectionStateChange(WebSocketSession.java:401) 在org.eclipse.jetty.websocket.common.io.IOState.notifyStateListeners(IOState.java:185) 在org.eclipse.jetty.websocket.common.io.IOState.onAbnormalClose(IOState.java:220) at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection $ OnCloseLocalCallback.onLocalClose(AbstractWebSocketConnection.java:164) at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection $ OnCloseLocalCallback.writeSuccess(AbstractWebSocketConnection.java:154) 在org.eclipse.jetty.websocket.common.io.FrameFlusher.notifyCallbackSuccess(FrameFlusher.java:406) 在org.eclipse.jetty.websocket.common.io.FrameFlusher $ Flusher.succeedEntries(FrameFlusher.java:241) 在org.eclipse.jetty.websocket.common.io.FrameFlusher $ Flusher.succeeded(FrameFlusher.java:231) 在org.eclipse.jetty.io.WriteFlusher.write(WriteFlusher.java:332) 在org.eclipse.jetty.io.AbstractEndPoint.write(AbstractEndPoint.java:141) 在org.eclipse.jetty.websocket.common.io.FrameFlusher $ Flusher.flush(FrameFlusher.java:152) 在org.eclipse.jetty.websocket.common.io.FrameFlusher $ Flusher.process(FrameFlusher.java:216) 在org.eclipse.jetty.util.IteratingCallback.processing(IteratingCallback.java:241) 在org.eclipse.jetty.util.IteratingCallback.iterate(IteratingCallback.java:224) 在org.eclipse.jetty.websocket.common.io.FrameFlusher.enqueue(FrameFlusher.java:381) at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.outgoingFrame(AbstractWebSocketConnection.java:581) at org.eclipse.jetty.websocket.client.io.WebSocketClientConnection.outgoingFrame(WebSocketClientConnection.java:97) 在org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.close(AbstractWebSocketConnection.java:271) at org.eclipse.jetty.websocket.client.io.ConnectionManager.shutdownAllConnections(ConnectionManager.java:161) 在org.eclipse.jetty.websocket.client.io.ConnectionManager.doStop(ConnectionManager.java:192) 在org.eclipse.jetty.util.component.AbstractLifeCycle.stop(AbstractLifeCycle.java:89) 在org.eclipse.jetty.util.component.ContainerLifeCycle.stop(ContainerLifeCycle.java:143) at org.eclipse.jetty.util.component.ContainerLifeCycle.doStop(ContainerLifeCycle.java:161) 在org.eclipse.jetty.websocket.client.WebSocketClient.doStop(WebSocketClient.java:300) 在org.eclipse.jetty.util.component.AbstractLifeCycle.stop(AbstractLifeCycle.java:89) 在org.eclipse.jetty.util.component.ContainerLifeCycle.stop(ContainerLifeCycle.java:143) at org.eclipse.jetty.util.component.ContainerLifeCycle.doStop(ContainerLifeCycle.java:161) 在org.eclipse.jetty.websocket.jsr356.ClientContainer.doStop(ClientContainer.java:213) org.springframework.messaging.simp.stomp.ConnectionLostException:连接已关闭 在org.eclipse.jetty.util.component.AbstractLifeCycle.stop(AbstractLifeCycle.java:89) 在org.eclipse.jetty.util.thread.ShutdownThread.run(ShutdownThread.java:138)