Java WebSocketClient断开连接的RemoteEndpoint不可用
我正在尝试在线程中实现WebSocketClient,其中客户端持续向服务器发送消息,但我看到异常: org.eclipse.jetty.websocket.api.WebSocketException:RemoteEndpoint 不可用,当前状态[关闭],应为[打开或连接] 代码如下:Java WebSocketClient断开连接的RemoteEndpoint不可用,java,websocket,jetty,Java,Websocket,Jetty,我正在尝试在线程中实现WebSocketClient,其中客户端持续向服务器发送消息,但我看到异常: org.eclipse.jetty.websocket.api.WebSocketException:RemoteEndpoint 不可用,当前状态[关闭],应为[打开或连接] 代码如下: public class ProcessAnalysisThread extends Thread { static String dest = "ws://localhost:8086/Echo/
public class ProcessAnalysisThread extends Thread {
static String dest = "ws://localhost:8086/Echo/";
WebSocketClient client;
EAMClientSocket socket;
public void run() {
initializeWSClient();
while (true) {
System.out.println("Thread running");
try {
Thread.sleep(500);
socket.sendMessage("echo");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
void initializeWSClient() {
client = new WebSocketClient();
try {
socket = new EAMClientSocket(this);
client.start();
URI echoUri = new URI(dest);
System.out.println("URI Resolved");
ClientUpgradeRequest request = new ClientUpgradeRequest();
client.connect(socket, echoUri, request);
System.out.println("Connected and Waiting .....");
socket.getLatch().await();
System.out.println("Latched");
System.out.println("RequestURI: " + request.getRequestURI().toString());
socket.sendMessage("echo");
System.out.println("Sent Msg: echo");
socket.sendMessage("test");
System.out.println("Sent Msg: test");
// Thread.sleep(10000l);
} catch (Throwable t) {
System.out.println("Exception");
t.printStackTrace();
} finally {
try {
client.stop();
} catch (Exception e) {
System.out.println("Exception1");
e.printStackTrace();
}
}
}
}
以下是输出:
Connected and Waiting .....
Connected to server
Latched
RequestURI: ws://localhost:8086/Echo/
Sent Msg: echo
Sent Msg: test
Message received from server:echo
Message received from server:test
Thread running
Exception in thread "Thread-0" org.eclipse.jetty.websocket.api.WebSocketException: RemoteEndpoint unavailable, current state [CLOSED], expecting [OPEN or CONNECTED]
at org.eclipse.jetty.websocket.common.WebSocketSession.getRemote(WebSocketSession.java:252)
看起来我的线程没有在这里保存套接字引用您在initializemethod的finally子句中停止了客户端,这是正确的。我很尴尬。尝试了一个示例代码,但错过了finally block。您在initialize method的finally子句中停止了客户端,这是正确的。我很尴尬。尝试了一个示例代码,但最终错过了块