我无法通过Java websocket客户端连接到我的websocket服务器。请帮忙! 正在使用Apache Tomcat 8.0.26 获取异常为“java.io.IOException:尝试对非套接字的操作进行操作。
客户等级 import java.io.IOException; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URI; import java.net.URL; import java.util.concurrent.CountDownLatch;
import javax.websocket.*;
@ClientEndpoint
public class WebSocketClient {
private static Object waitLock = new Object();
protected Session userSession = null;
private MessageHandler messageHandler;
private static void wait4TerminateSignal() {
synchronized (waitLock) {
try {
waitLock.wait();
} catch (InterruptedException e) {
}
}
}
public void Connect(String sServer) throws InterruptedException {
try {
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
System.out.println("Connecting");
WebSocketClient webSocketClient = new WebSocketClient();
userSession = container.connectToServer(webSocketClient, URI.create(sServer));
wait4TerminateSignal();
System.out.println("Connected");
} catch (DeploymentException | IOException e) {
e.printStackTrace();
}
}
/**
* 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);
}
public void SendMessage(String sMsg) throws IOException {
userSession.getBasicRemote().sendText(sMsg);
}
@OnOpen
public void onOpen(Session session) {
System.out.println("Connected");
}
@OnClose
public void onClose(Session session, CloseReason closeReason) {
}
@OnMessage
public void onMessage(Session session, String msg) {
System.out.println(msg);
}
public void Disconnect() throws IOException {
userSession.close();
}
}