| New file |
| | |
| | | package com.xiaopeng.pushMessage.webSocket.config; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.boot.CommandLineRunner; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | /** |
| | | * WebSocket服务器配置类 |
| | | * 在Spring Boot应用启动时自动启动WebSocket服务器 |
| | | */ |
| | | @Component |
| | | @Slf4j |
| | | public class WebSocketServerConfig implements CommandLineRunner { |
| | | |
| | | @Autowired |
| | | private WebSocketServer webSocketServer; |
| | | |
| | | @Override |
| | | public void run(String... args) throws Exception { |
| | | // 在新线程中启动WebSocket服务器,避免阻塞主线程 |
| | | new Thread(() -> { |
| | | try { |
| | | log.info("正在启动WebSocket服务器..."); |
| | | webSocketServer.run(); |
| | | } catch (InterruptedException e) { |
| | | log.error("WebSocket服务器启动失败", e); |
| | | Thread.currentThread().interrupt(); |
| | | } |
| | | }, "WebSocket-Server-Thread").start(); |
| | | } |
| | | } |