I am not able to connect to my websocket server through Java websocket client.Please help! Apache Tomcat 8.0.26 is being used Getting exception as "java.io.IOException: An operation was attempted on something that is not a socket.
Client Class 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();
}
}