2

I am new to spring-boot but I am facing a problem while trying to run java web-socket application with spring-boot.

I have a Spring Application class like this :

@SpringBootApplication
public class HLACSSApplication {
    public static void main(String[] args) {
        SpringApplication.run(HLACSSApplication.class, args);
    }   
}

and a Spring application config class :

@ComponentScan("com.test.hlacssmdw")
public class HLACSSApplicationConfig {    }

Now this is how I am creating a web-socket server endpoint :

package com.test.hlacssmdw.endpoints;

@ServerEndpoint("/ss")
public class HLACSSSocketServer {

    @OnOpen
    public void open(Session session){
        System.out.println("In open()");
        System.out.println("Session Related data : " + session.getId());
        System.out.println("Out open()");
    }

    @OnMessage
    public String handleMessage(String message, Session session) {
        System.out.println("In handleMessage()");
        System.out.println("Session Related data : " + session.getId());
        System.out.println("Message Received is : " + message);
        System.out.println("Out handleMessage()");
        return "Message Received Successfully.";
    }

    @OnClose
    public void close(Session session){
        System.out.println("In close()");
        System.out.println("Session Related data : " + session.getId());
        System.out.println("Out close()");
    }

}

I have created a webpage which has the following javascript that connects to this server endpoint as follows :

var socket = new WebSocket("ws://localhost:1112/ss");
socket.onmessage = onMessage;
function onMessage(event) {
    console.log(event.data);
}

but I am unable to connect and keep getting this error on the browser console :

WebSocket connection to 'ws://localhost:1112/ss' failed: Error during WebSocket handshake: Unexpected response code: 404

and on server side I receive the following error :

org.apache.catalina.connector.ClientAbortException: java.io.IOException: An established connection was aborted by the software in your host machine
    at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:393)
    at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:426)
    at org.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:342)
    at org.apache.catalina.connector.OutputBuffer.flush(OutputBuffer.java:317)
    at org.apache.catalina.connector.CoyoteOutputStream.flush(CoyoteOutputStream.java:110)
    at com.fasterxml.jackson.core.json.UTF8JsonGenerator.flush(UTF8JsonGenerator.java:1037)
    at com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:1904)

Caused by: java.io.IOException: An established connection was aborted by the software in your host machine
    at sun.nio.ch.SocketDispatcher.write0(Native Method)
    at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:51)
    at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:94)
    at sun.nio.ch.IOUtil.write(IOUtil.java:65)
    at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:450)
    at org.apache.tomcat.util.net.NioChannel.write(NioChannel.java:127)

I am unable to understand because this same structure is working when I am not using spring boot i.e. when I create a simple war using maven. The reason I think is that I don't have a war file name when using spring boot but I am not sure.

Please help ! ! !

  • Did you set the server port to 1112? It's 8080 by default. This question/answer appear to be super relevant: https://stackoverflow.com/questions/27158106/websocket-with-sockjs-spring-4-but-without-stomp/27161986#27161986 – DaShaun Jan 22 '18 at 15:15

0 Answers0