zoukankan      html  css  js  c++  java
  • springboot--websocket简单demo(一):session chat

    最近跟着大佬 https://tycoding.cn/2019/06/16/project/boot-chat/

    敲了2个关于websocket的demo,总结一下

    从将会话信息保存在session中,缺点,项目启动后,对话必须在同一浏览器打开,才可以避免消息丢失

    1.pom文件引入相关依赖

            <!--websocket-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-websocket</artifactId>
            </dependency>
            <!--web-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!--lombok-->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
            <!--fastjson-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.54</version>
            </dependency>

    2.application.yml进行配置

     3.创建user对象--封装用户信息,Message对象,储存对话信息

     4.编写接口ChatSessionService及其impl实现类,获取在线用户、实现信息推送

     5.创建HttpSessionConfig类,从websocket对象获取HttpSession

     6.创建websocketServerEndPoint实例对象(包含websocket各生命周期),添加@ServerEndPoint注解,将HttpSessionConfig配置进去(从websocket获取session)

     7.controller层实现路由控制和接口跳转&前端页面&js实现

     可以从github上clone我的或者大佬的代码

    我的git地址:https://github.com/AliceSunCong/session-chat

    大佬的git地址:https://github.com/TyCoding/boot-chat/tree/master/session-chat

  • 相关阅读:
    MySQL主从复制(异步复制与半同步复制)
    http和https到底区别在哪
    Tcp的Flags
    机器学习之近邻算法模型(KNN)
    机器学习之linear_model (线性回归算法模型)
    数据分析之Pandas操作
    数据分析之Numpy的基本操作
    分布式爬虫
    基于CrawlSpider全栈数据爬取
    HDU-4687 Boke and Tsukkomi 带花树,枚举
  • 原文地址:https://www.cnblogs.com/koudaiyoutang/p/11446787.html
Copyright © 2011-2022 走看看