3

I have implemented a websocket server by following this tutorial: https://www.baeldung.com/java-websockets

Now how do I run this app? I am using Spring and my main function looks like this:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

and my websocket endpoint looks like this:

import java.io.IOException;

import javax.websocket.*;
import javax.websocket.server.*;

@ServerEndpoint(
        value = "/chat/{username}",
        decoders=MessageDecoder.class,
        encoders = MessageEncoder.class
)
public class Controller {
    // ...
}

now when I run this, and try to connect to the websocket endpoint like this: wsta ws://localhost:8080/chat/aa -I I get the following error:

WebSocket upgrade request

---

Host: localhost:8080

Connection: Upgrade

Upgrade: websocket

Sec-WebSocket-Version: 13

Sec-WebSocket-Key: ...

Origin: http://localhost

WebSocket upgrade response

---

404 Not Found

Vary: OriginAccess-Control-Request-MethodAccess-Control-Request-Headers

Content-Type: application/json

Transfer-Encoding: chunked

Date: Tue, 05 May 2020 12:12:52 GMT

WebSocketError: WebSocket response error

The answer here (https://stackoverflow.com/a/57924245/10551293) says that you access the webscoket endpoint over ws://localhost:8080/context/chat/aa. What is context, and how do I get my server to run and accept incoming connections?

1 Answer 1

3

In order to get the Baeldung tutorial mentioned in OP running

  1. I had to add a maven dependency for spring-boot-starter-websocket to pom.xml

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-websocket</artifactId>
    </dependency>
    
  2. Add a spring @Configuration in which @EnableWebSocket is added and a ServerEndpointExporter bean is instantiated:

    @Configuration
    @EnableWebSocket
    public class WsConfig {
    
        @Bean
        public ServerEndpointExporter serverEndpoint() {
            return new ServerEndpointExporter();
        }
    }
    
  3. Additionally the endpoint controller has to be a spring @Component:

    @ServerEndpoint(
            value = "/chat/{username}",
            decoders=MessageDecoder.class,
            encoders = MessageEncoder.class
    )
    @Component
    public class Controller {
     ...
    


Eventually the websocket endpoint was reachable under ws://localhost:8080/chat/torvalds

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, this is what I was missing!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.