创建spring boot集成webSocket前后端消息发送的实现

整体功能说明

前后台可以对webSocket连接进行数据变更的传播。支持心跳和重连

ws1:

ws2:

 

 

 

1.创建项目

 

 

2.新建web目录

新建web目录存放前端静态文件

 

3.注入依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.3.1.RELEASE</version>
        </dependency>
        <!--websocket连接需要使用到的包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
            <version>2.3.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.3.1.RELEASE</version>
            <scope>test</scope>
        </dependency>

静态文件打包(红字)

    <build>
        <resources>
            <resource>
                <directory>src/main/web</directory>
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/**</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

 

4.整体项目结构

 

git地址  https://github.com/linHongWenGithub/webSocketDemo

 

posted @ 2020-08-10 15:24  林被熊烟岛  阅读(222)  评论(0编辑  收藏  举报