Simple Websocket Client in Java - Connection issue

20,628

I found the issue - it was that the ws address required a '/' on the end of it when being run from a java client.

JavaScript ws://192.168.0.25:9000

Java ws://192.168.0.25:9000/

Not sure why this is required in Java - but it solved the issue...

Share:
20,628
flinthart
Author by

flinthart

Updated on July 09, 2022

Comments

  • flinthart
    flinthart almost 2 years

    I'm trying to write a simple websocket client in Java to connect to a 3rd party WebSocket server (I have no access to the server).

    I can connect and communicate with the websocket server using javascript in a browser, but when I try and do the same thing using a Java client it fails to connect.

    With the same java client I can connect to a tomcat websocket server I have running, so I think it's some sort of compatibility issue between the client and server. (I'm using Tomcat 7.0.56 libraries for the websocket client).

    This is the error I get...

    Error

    javax.websocket.DeploymentException: The HTTP request to initiate the WebSocket connection failed
        at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServer(WsWebSocketContainer.java:344)
        at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServer(WsWebSocketContainer.java:166)
        at com.cvr.instant.ws.WebSocketClient.Connect(WebSocketClient.java:44)
        at com.cvr.instant.ws.TestWS.Start(TestWS.java:17)
        at com.cvr.instant.ws.TestWS.main(TestWS.java:37)
    Caused by: java.io.EOFException
        at org.apache.tomcat.websocket.WsWebSocketContainer.processResponse(WsWebSocketContainer.java:595)
        at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServer(WsWebSocketContainer.java:317)
    ... 4 more
    

    WebSocketClient

    import java.io.IOException;
    import java.net.URI;
    import java.net.URISyntaxException;
    
    import javax.websocket.ClientEndpoint;
    import javax.websocket.CloseReason;
    import javax.websocket.ContainerProvider;
    import javax.websocket.DeploymentException;
    import javax.websocket.OnClose;
    import javax.websocket.OnMessage;
    import javax.websocket.OnOpen;
    import javax.websocket.Session;
    import javax.websocket.WebSocketContainer;
    
    @ClientEndpoint
    public class WebSocketClient {
    
        protected   WebSocketContainer container;
        protected   Session userSession = null;
    
        public WebSocketClient() {
            container = ContainerProvider.getWebSocketContainer();
        }
    
        public void Connect(String sServer) {
    
              try {
                  userSession = container.connectToServer(this, new URI(sServer));
    
                } catch (DeploymentException | URISyntaxException | IOException e) {
                    e.printStackTrace();
                }
    
        }
    
        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();
        }
    }
    

    Running the client

    import java.io.IOException;
    
    public class TestWS {
    
        public WebSocketClient wsc;
        public TestWS() {
        }
    
        public void Start() throws InterruptedException, IOException {
    
            wsc = new WebSocketClient();
            wsc.callback = this;
            wsc.Connect("ws://192.168.0.25:9000");
            Thread.sleep(1000);
            wsc.Disconnect();
        }
    
    
        public static void main(String[] args) throws InterruptedException, IOException 
        {
            TestWS t = new TestWS();
            t.Start();
            Thread.sleep(1000);
        }
    
    }    
    

    I'm very new to websocket clients (and fairly new to websockets in general). Any help you can give would be appreciated!