springboot 整合websocket

   
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-websocket</artifactId>
 </dependency>
@Configuration
public class WebsocketConfig {

    @Bean
    public ServerEndpointExporter serverEndpointConfig() {
        return new ServerEndpointExporter();
    }
}
@Component
@ServerEndpoint("/message")
@Slf4j
public class WebSocket {

    @OnOpen
    public void onOpen(Session session) {
        log.info("onOpen:{}", session.getId());
    }

    @OnClose
    public void onClose(Session session) {
        log.info("onClose:{}", session.getId());
    }

    @OnMessage
    public void onMessage(String message) {
        log.info("onMessage:{}", message);
    }

    @OnError
    public void onError(Throwable cause, Session session) {
        log.info("onError:{}", session.getId());
    }
}
  const ws = new WebSocket("ws://localhost:7777/message");
        ws.onopen = function(){
            console.log("open");
        }

 

posted on 2023-06-05 23:28  知了不了了之  阅读(25)  评论(0编辑  收藏  举报