SpringBoot+VUE2完成WebSocket聊天(数据入库)

下载依赖

        <!-- websocket -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>

        <!-- MybatisPlus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.0</version>
        </dependency>

数据库sql

CREATE TABLE `interrogation`  (
  `ID` int(0) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `INITIATOR_ID` int(0) NULL DEFAULT NULL COMMENT '发起人的ID()',
  `DOCTOR` int(0) NULL DEFAULT NULL COMMENT '接受人ID',
  `STATUS` int(0) NULL DEFAULT NULL COMMENT '状态(1.开始聊天和2.结束)',
  PRIMARY KEY (`ID`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 9 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '聊天列表' ROW_FORMAT = Dynamic;


CREATE TABLE `interrogation_chat`  (
  `ID` int(0) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `INTERROGATION_ID` int(0) NULL DEFAULT NULL COMMENT '聊天表的id',
  `CONTENT` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '内容',
  `INITIATOR_ID` int(0) NULL DEFAULT NULL COMMENT '发送人',
  `CREATE_TIME` datetime(0) NULL DEFAULT NULL COMMENT '发送时间',
  PRIMARY KEY (`ID`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 130 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '聊天记录表' ROW_FORMAT = Dynamic;

实体类 

后续业务采用mybatils-plus写的,如果不会使用手写sql也是可以的

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

/**
 * @description:  聊天记录表
 * @author: 
 * @date: 2024/10/27 9:37
 **/
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
public class InterrogationChat {
    /**
     * ID
     **/
    @TableId(value = "ID", type = IdType.AUTO)
    private Integer id;

    /**
     * 聊天表的id
     **/
    @TableField("INTERROGATION_ID")
    private Integer interrogationId;

    /**
     * 内容
     **/
    @TableField("CONTENT")
    private String content;

    /**
     * 发送人
     **/
    @TableField("INITIATOR_ID")
    private Integer initiatorId;

    /**
     * 发送时间
     **/
    @TableField("CREATE_TIME")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;
}

websocker配置

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

/**
 * @author 
 * @date 2024/10/23
 * @apiNote
 */

@Configuration
public class WebSocketConfig {

    /**
     * 注入一个ServerEndpointExporter,该Bean会自动注册使用@ServerEndpoint注解申明的websocket endpoint
     */
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();

    }

}


import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import javax.websocket.server.ServerEndpointConfig;

/**
 * @author 
 * @date 2024/10/30
 * @apiNote
 */

public class MyEndpointConfigure extends ServerEndpointConfig.Configurator implements ApplicationContextAware {
 private static volatile BeanFactory context;

 @Override
 public <T> T getEndpointInstance(Class<T> clazz) throws InstantiationException
 {
  return context.getBean(clazz);
 }

 @Override
 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
 {
  System.out.println("auto load"+this.hashCode());
  MyEndpointConfigure.context = applicationContext;
 }
}



import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 
 * @date 2024/10/30
 * @apiNote
 */

@Configuration
public class MyConfigure {

 @Bean
 public MyEndpointConfigure newConfigure()
 {
  return new MyEndpointConfigure();
 }

}



import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.Ta
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值