TooTallNate / Java-WebSocket

A barebones WebSocket client and server implementation written in 100% Java.
http://tootallnate.github.io/Java-WebSocket
MIT License
10.53k stars 2.58k forks source link

How to integrate the WebSocketServer into springboot ? #1355

Closed better-github-hub closed 1 year ago

better-github-hub commented 1 year ago

This is usually how we start:

public static void main(String[] args) {
        String host = "localhost";
        int port = 8887;
        WebSocketServer server = new SimpleWebSocketServer(new InetSocketAddress(host, port));
        server.run();
  }

BUT ,I want to create WebSocketServer bean , and start the application use springboot , as follow:

@Bean
public WebSocketServer getWebSocketServer(){
    WebSocketServer server = new SimpleWebSocketServer(new InetSocketAddress(host, port));
    return server;
}

@SpringBootApplication(scanBasePackages = "com.github")
public class FrameworkWebsocketApplication {
    public static void main(String[] args) {
        SpringApplication.run(FrameworkWebsocketApplication.class, args);
        System.out.println("Application start success !");
    }
}
better-github-hub commented 1 year ago

Anybody answer me?

better-github-hub commented 1 year ago

I need help , Who can ask me ,how to do ?

better-github-hub commented 1 year ago

I create a new Thread run WebSocketServer, this way can integrate into springboot. like this:

@Bean public WebSocketServer runWebSocketServer(LocalCache cache){ WebSocketServer server = new SimpleWebSocketServer(new InetSocketAddress(host, port),cache); ThreadPoolUtil.getCachedThreadPool().execute(() -> server.run()); return server; }

And, are there any disadvantages to this approach ?

PhilipRoman commented 1 year ago

Closing as this is off topic Might be better to ask this in a Spring discussion forum, not here