SpringBoot整合Websocket

SpringBoot整合Websocket

整合步骤很简单,请看下文分解。

  1. 引入Maven依赖,如下:

    <dependency>
    	<groupId>org.springframework.bootgroupId>
    	<artifactId>spring-boot-starter-websocketartifactId>
    	<version>2.3.3.RELEASEversion>
    dependency>
    
  2. 新建一个类:WebSocketTextMsgConfig,实现WebSocketConfigurer接口,如下:

    package com.example.textMessageWebsocket;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.socket.config.annotation.EnableWebSocket;
    import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
    import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
    
    @Configuration
    @EnableWebSocket
    public class WebSocketTextMsgConfig implements WebSocketConfigurer {
         
    
        @Autowired
        WebSocketTextmsgHandler webSocketTextmsgHandler;
    
        @Override
        public void registerWebSocketHandlers(WebSocketHandlerRegistry registry

你可能感兴趣的:(Websocket,SpringBoot,websocket,spring,boot,java)