博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot集成WebSocket
阅读量:4042 次
发布时间:2019-05-24

本文共 2754 字,大约阅读时间需要 9 分钟。

1、代码目录

在这里插入图片描述

2、application.yml

server:  port: 18092

3、pom.xml

  • 子模块
com.ct.tutorial
com.ct
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-websocket
org.projectlombok
lombok
  • 父工程
org.springframework.boot
spring-boot-starter-parent
2.5.0

4、启动类

/** * @Author LaoHa * @Date 2021/6/24 */@SpringBootApplicationpublic class AppWebSocket {    public static void main(String[] args) {        SpringApplication.run(AppWebSocket.class, args);    }}

5、WebSocket配置文件

/** * @Author LaoHa * @Date 2021/6/24 */@Configurationpublic class WebSocketConfig {    /**     * 注入一个ServerEndpointExporter,该Bean会自动注册使用@ServerEndpoint注解申明的websocket endpoint     */    @Bean    public ServerEndpointExporter serverEndpointExporter() {        return new ServerEndpointExporter();    }}

6、消息接收、推送类

/** * 前后端交互的类实现消息的接收推送(自己发送给自己) * * @ServerEndpoint(value = "/test/one") 前端通过此URI和后端交互,建立连接 */@Slf4j@ServerEndpoint(value = "/test/one")@Componentpublic class OneWebSocket {    /**     * 记录当前在线连接数     */    private static AtomicInteger onlineCount = new AtomicInteger(0);    /**     * 连接建立成功调用的方法     */    @OnOpen    public void onOpen(Session session) {        onlineCount.incrementAndGet(); // 在线数加1        log.info("有新连接加入:{},当前在线人数为:{}", session.getId(), onlineCount.get());    }    /**     * 连接关闭调用的方法     */    @OnClose    public void onClose(Session session) {        onlineCount.decrementAndGet(); // 在线数减1        log.info("有一连接关闭:{},当前在线人数为:{}", session.getId(), onlineCount.get());    }    /**     * 收到客户端消息后调用的方法     *     * @param message 客户端发送过来的消息     */    @OnMessage    public void onMessage(String message, Session session) {        log.info("服务端收到客户端[{}]的消息:{}", session.getId(), message);        this.sendMessage("Hello, " + message, session);    }    @OnError    public void onError(Session session, Throwable error) {        log.error("发生错误");        error.printStackTrace();    }    /**     * 服务端发送消息给客户端     */    private void sendMessage(String message, Session toSession) {        try {            log.info("服务端给客户端[{}]发送消息{}", toSession.getId(), message);            toSession.getBasicRemote().sendText("来自服务端的消息:"+message);        } catch (Exception e) {            log.error("服务端发送消息给客户端失败:{}", e);        }    }}

7、测试index.html

    My WebSocket

8、结果

  • 服务端

    在这里插入图片描述

  • 浏览器

    在这里插入图片描述

参考

转载地址:http://qlmdi.baihongyu.com/

你可能感兴趣的文章
coursesa课程 Python 3 programming Functions can call other functions 函数调用另一个函数
查看>>
coursesa课程 Python 3 programming The while Statement
查看>>
course_2_assessment_6
查看>>
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
查看>>
在unity中建立最小的shader(Minimal Shader)
查看>>
1.3 Debugging of Shaders (调试着色器)
查看>>
关于phpcms中模块_tag.class.php中的pc_tag()方法的含义
查看>>
vsftp 配置具有匿名登录也有系统用户登录,系统用户有管理权限,匿名只有下载权限。
查看>>
linux安装usb wifi接收器
查看>>
多线程使用随机函数需要注意的一点
查看>>
getpeername,getsockname
查看>>
让我做你的下一行Code
查看>>
浅析:setsockopt()改善程序的健壮性
查看>>
关于对象赋值及返回临时对象过程中的构造与析构
查看>>
VS 2005 CRT函数的安全性增强版本
查看>>
SQL 多表联合查询
查看>>
Visual Studio 2010:C++0x新特性
查看>>
drwtsn32.exe和adplus.vbs进行dump文件抓取
查看>>
cppcheck c++静态代码检查
查看>>