集成websocket
This commit is contained in:
parent
32f77e5382
commit
b15914f219
@ -6,6 +6,9 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.core.metadata.OrderItem;
|
||||
import com.dd.admin.business.author.entity.Author;
|
||||
import com.dd.admin.business.author.service.AuthorService;
|
||||
import com.dd.admin.business.chat.domain.ChatDto;
|
||||
import com.dd.admin.business.chat.domain.ChatVo;
|
||||
import com.dd.admin.business.chat.service.ChatService;
|
||||
import com.dd.admin.business.file.entity.File;
|
||||
import com.dd.admin.business.file.service.FileService;
|
||||
import com.dd.admin.business.follow.domain.FollowDto;
|
||||
@ -79,6 +82,8 @@ public class AuthApi {
|
||||
ReplyService replyService;
|
||||
@Autowired
|
||||
UpReplysService upReplysService;
|
||||
@Autowired
|
||||
ChatService chatService;
|
||||
|
||||
@ApiOperation(value = "获取所有笔记")
|
||||
@ApiOperationSupport(order = 1)
|
||||
@ -612,4 +617,31 @@ public class AuthApi {
|
||||
return ResultBean.success(isUp);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ApiOperation(value = "获取聊天记录")
|
||||
@ApiOperationSupport(order = 1)
|
||||
@GetMapping("/api/auth/getChatList")
|
||||
@OperLog(operModule = "获取所有笔记",operType = OperType.QUERY,operDesc = "获取聊天记录")
|
||||
public ResultBean<IPage<ChatVo>> getChatList( ChatDto chatDto) {
|
||||
String followId = String.valueOf(request.getAttribute("authorId"));
|
||||
Author follow = authorService.getById(followId);
|
||||
chatDto.setToId(followId);
|
||||
chatDto.setToName(follow.getAuthorName());
|
||||
IPage<ChatVo> chatVoIPage = chatService.selectChatPage(chatDto);
|
||||
|
||||
return ResultBean.success(chatVoIPage);
|
||||
}
|
||||
|
||||
|
||||
@ApiOperation(value = "获取聊天列表")
|
||||
@ApiOperationSupport(order = 1)
|
||||
@GetMapping("/api/auth/getMessageList")
|
||||
@OperLog(operModule = "获取聊天列表",operType = OperType.QUERY,operDesc = "获取聊天列表")
|
||||
public ResultBean<List<ChatVo>> getMessageList( ChatDto chatDto) {
|
||||
String followId = String.valueOf(request.getAttribute("authorId"));
|
||||
List<ChatVo> chatVos = chatService.selectChatList(followId);
|
||||
return ResultBean.success(chatVos);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,88 @@
|
||||
package com.dd.admin.business.chat.controller;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import com.dd.admin.common.model.UpdateGroup;
|
||||
import com.dd.admin.common.model.result.ResultBean;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import com.dd.admin.business.chat.entity.Chat;
|
||||
import com.dd.admin.business.chat.domain.ChatVo;
|
||||
import com.dd.admin.business.chat.domain.ChatDto;
|
||||
import com.dd.admin.business.chat.service.ChatService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 前端控制器
|
||||
* </p>
|
||||
*
|
||||
* @author 727869402@qq.com
|
||||
* @since 2024-12-28
|
||||
*/
|
||||
@Api(tags = "")
|
||||
@RestController
|
||||
public class ChatController {
|
||||
|
||||
@Autowired
|
||||
ChatService chatService;
|
||||
|
||||
@ApiOperation(value = "-分页列表")
|
||||
@ApiOperationSupport(order = 1)
|
||||
@GetMapping("/admin/chat/page")
|
||||
public ResultBean<IPage<ChatVo>> page(ChatDto chatDto) {
|
||||
IPage<ChatVo> pageInfo = chatService.selectChatPage(chatDto);
|
||||
return ResultBean.success(pageInfo);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "-列表")
|
||||
@ApiOperationSupport(order = 2)
|
||||
@GetMapping("/admin/chat/list")
|
||||
public ResultBean<List<ChatVo>> list(ChatDto chatDto) {
|
||||
List<ChatVo> list = chatService.selectChatDetail(chatDto);
|
||||
return ResultBean.success(list);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "-添加")
|
||||
@ApiOperationSupport(order = 3)
|
||||
@PostMapping("/admin/chat/add")
|
||||
public ResultBean<Chat> add(@RequestBody @Validated ChatDto chatDto) {
|
||||
Chat chat = BeanUtil.copyProperties(chatDto, Chat.class);
|
||||
chatService.save(chat);
|
||||
return ResultBean.success(chat);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "-查询")
|
||||
@ApiOperationSupport(order = 4)
|
||||
@GetMapping("/admin/chat/{chatId}")
|
||||
public ResultBean<ChatVo> get(@PathVariable @NotBlank String chatId) {
|
||||
Chat chat = chatService.getById(chatId);
|
||||
ChatVo chatVo = BeanUtil.copyProperties(chat,ChatVo.class);
|
||||
return ResultBean.success(chatVo);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "-修改")
|
||||
@ApiOperationSupport(order = 5)
|
||||
@PostMapping("/admin/chat/update")
|
||||
public ResultBean<Chat> update(@RequestBody @Validated(UpdateGroup.class) ChatDto chatDto) {
|
||||
Chat chat = BeanUtil.copyProperties(chatDto, Chat.class);
|
||||
chatService.updateById(chat);
|
||||
return ResultBean.success(chat);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "-删除")
|
||||
@ApiOperationSupport(order = 6)
|
||||
@GetMapping("/admin/chat/delete/{chatId}")
|
||||
public ResultBean<Chat> delete(@PathVariable @NotBlank String chatId) {
|
||||
Boolean b = chatService.removeById(chatId);
|
||||
return ResultBean.success(b);
|
||||
}
|
||||
}
|
74
src/main/java/com/dd/admin/business/chat/domain/ChatDto.java
Normal file
74
src/main/java/com/dd/admin/business/chat/domain/ChatDto.java
Normal file
@ -0,0 +1,74 @@
|
||||
package com.dd.admin.business.chat.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import java.util.Date;
|
||||
import com.baomidou.mybatisplus.annotation.Version;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||||
import com.baomidou.mybatisplus.annotation.TableLogic;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import java.io.Serializable;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import com.dd.admin.common.model.UpdateGroup;
|
||||
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 返回对象
|
||||
* </p>
|
||||
*
|
||||
* @author 727869402@qq.com
|
||||
* @since 2024-12-28
|
||||
*/
|
||||
@Data
|
||||
@ApiModel(value="接收对象")
|
||||
public class ChatDto {
|
||||
|
||||
|
||||
@ApiModelProperty(value = "消息id")
|
||||
@NotBlank(message = "id不能为空",groups = UpdateGroup.class)
|
||||
private String chatId;
|
||||
|
||||
@ApiModelProperty(value = "消息发送者")
|
||||
private String fromId;
|
||||
|
||||
@ApiModelProperty(value = "消息发送者")
|
||||
private String fromName;
|
||||
|
||||
@ApiModelProperty(value = "接收者")
|
||||
private String toId;
|
||||
|
||||
@ApiModelProperty(value = "接收者")
|
||||
private String toName;
|
||||
|
||||
@ApiModelProperty(value = "0文字 1图片 2语音 3视频")
|
||||
private Integer messageType;
|
||||
|
||||
@ApiModelProperty(value = "0发送 1已读")
|
||||
private Integer messageStatus;
|
||||
|
||||
@ApiModelProperty(value = "内容")
|
||||
private String content;
|
||||
|
||||
@ApiModelProperty(value = "资源地址")
|
||||
private String resourceUrl;
|
||||
|
||||
@ApiModelProperty(value = "0正常 1删除")
|
||||
private Integer deleted;
|
||||
|
||||
@ApiModelProperty(value = "创建时间")
|
||||
private Date createTime;
|
||||
|
||||
@ApiModelProperty(value = "ip地址")
|
||||
private String ipAddress;
|
||||
|
||||
@ApiModelProperty(value = "真实ip地址")
|
||||
private String ipRealAddress;
|
||||
|
||||
|
||||
}
|
76
src/main/java/com/dd/admin/business/chat/domain/ChatVo.java
Normal file
76
src/main/java/com/dd/admin/business/chat/domain/ChatVo.java
Normal file
@ -0,0 +1,76 @@
|
||||
package com.dd.admin.business.chat.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import java.util.Date;
|
||||
import com.baomidou.mybatisplus.annotation.Version;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||||
import com.baomidou.mybatisplus.annotation.TableLogic;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import java.io.Serializable;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 返回对象
|
||||
* </p>
|
||||
*
|
||||
* @author 727869402@qq.com
|
||||
* @since 2024-12-28
|
||||
*/
|
||||
@Data
|
||||
@ApiModel(value="返回对象")
|
||||
public class ChatVo {
|
||||
|
||||
|
||||
@ApiModelProperty(value = "消息id")
|
||||
private String chatId;
|
||||
|
||||
@ApiModelProperty(value = "消息发送者")
|
||||
private String fromId;
|
||||
|
||||
@ApiModelProperty(value = "消息发送者")
|
||||
private String fromName;
|
||||
|
||||
@ApiModelProperty(value = "接收者")
|
||||
private String toId;
|
||||
|
||||
@ApiModelProperty(value = "接收者")
|
||||
private String toName;
|
||||
|
||||
@ApiModelProperty(value = "0文字 1图片 2语音 3视频")
|
||||
private Integer messageType;
|
||||
|
||||
@ApiModelProperty(value = "0发送 1已读")
|
||||
private Integer messageStatus;
|
||||
|
||||
@ApiModelProperty(value = "内容")
|
||||
private String content;
|
||||
|
||||
@ApiModelProperty(value = "资源地址")
|
||||
private String resourceUrl;
|
||||
|
||||
@ApiModelProperty(value = "0正常 1删除")
|
||||
private Integer deleted;
|
||||
|
||||
@ApiModelProperty(value = "创建时间")
|
||||
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
|
||||
// @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
|
||||
private Date createTime;
|
||||
|
||||
@ApiModelProperty(value = "ip地址")
|
||||
private String ipAddress;
|
||||
|
||||
@ApiModelProperty(value = "真实ip地址")
|
||||
private String ipRealAddress;
|
||||
|
||||
private String fromAvatar;
|
||||
private String toAvatar;
|
||||
}
|
87
src/main/java/com/dd/admin/business/chat/entity/Chat.java
Normal file
87
src/main/java/com/dd/admin/business/chat/entity/Chat.java
Normal file
@ -0,0 +1,87 @@
|
||||
package com.dd.admin.business.chat.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import java.util.Date;
|
||||
import com.baomidou.mybatisplus.annotation.Version;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||||
import com.baomidou.mybatisplus.annotation.TableLogic;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import java.io.Serializable;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* </p>
|
||||
*
|
||||
* @author 727869402@qq.com
|
||||
* @since 2024-12-28
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@TableName("business_chat")
|
||||
@ApiModel(value="Chat对象", description="")
|
||||
public class Chat implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@ApiModelProperty(value = "消息id")
|
||||
@TableId(value = "CHAT_ID", type = IdType.ASSIGN_UUID)
|
||||
private String chatId;
|
||||
|
||||
@ApiModelProperty(value = "消息发送者")
|
||||
@TableField("FROM_ID")
|
||||
private String fromId;
|
||||
|
||||
@ApiModelProperty(value = "消息发送者")
|
||||
@TableField("FROM_NAME")
|
||||
private String fromName;
|
||||
|
||||
@ApiModelProperty(value = "接收者")
|
||||
@TableField("TO_ID")
|
||||
private String toId;
|
||||
|
||||
@ApiModelProperty(value = "接收者")
|
||||
@TableField("TO_NAME")
|
||||
private String toName;
|
||||
|
||||
@ApiModelProperty(value = "0文字 1图片 2语音 3视频")
|
||||
@TableField("MESSAGE_TYPE")
|
||||
private Integer messageType;
|
||||
|
||||
@ApiModelProperty(value = "0发送 1已读")
|
||||
@TableField("MESSAGE_STATUS")
|
||||
private Integer messageStatus;
|
||||
|
||||
@ApiModelProperty(value = "内容")
|
||||
@TableField("CONTENT")
|
||||
private String content;
|
||||
|
||||
@ApiModelProperty(value = "资源地址")
|
||||
@TableField("RESOURCE_URL")
|
||||
private String resourceUrl;
|
||||
|
||||
@ApiModelProperty(value = "0正常 1删除")
|
||||
@TableField("DELETED")
|
||||
@TableLogic
|
||||
private Integer deleted;
|
||||
|
||||
@ApiModelProperty(value = "创建时间")
|
||||
@TableField(value = "CREATE_TIME", fill = FieldFill.INSERT)
|
||||
private Date createTime;
|
||||
|
||||
@ApiModelProperty(value = "ip地址")
|
||||
@TableField("IP_ADDRESS")
|
||||
private String ipAddress;
|
||||
|
||||
@ApiModelProperty(value = "真实ip地址")
|
||||
@TableField("IP_REAL_ADDRESS")
|
||||
private String ipRealAddress;
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.dd.admin.business.chat.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.dd.admin.business.chat.entity.Chat;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.dd.admin.business.chat.domain.ChatVo;
|
||||
import com.dd.admin.business.chat.domain.ChatDto;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Mapper 接口
|
||||
* </p>
|
||||
*
|
||||
* @author 727869402@qq.com
|
||||
* @since 2024-12-28
|
||||
*/
|
||||
@Mapper
|
||||
public interface ChatMapper extends BaseMapper<Chat> {
|
||||
|
||||
IPage<ChatVo> selectChatPage(Page<ChatVo> page, @Param("chatDto") ChatDto chatDto);
|
||||
ChatVo selectChat(@Param("chatId") String chatId);
|
||||
List<ChatVo> selectChatDetail(@Param("chatDto") ChatDto chatDto);
|
||||
List<ChatVo> selectChatList(@Param("toId")String toId);
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.dd.admin.business.chat.mapper.ChatMapper">
|
||||
|
||||
<!-- 通用查询映射结果 -->
|
||||
<resultMap id="BaseResultMap" type="com.dd.admin.business.chat.entity.Chat">
|
||||
<id column="CHAT_ID" property="chatId" />
|
||||
<result column="FROM_ID" property="fromId" />
|
||||
<result column="FROM_NAME" property="fromName" />
|
||||
<result column="TO_ID" property="toId" />
|
||||
<result column="TO_NAME" property="toName" />
|
||||
<result column="MESSAGE_TYPE" property="messageType" />
|
||||
<result column="MESSAGE_STATUS" property="messageStatus" />
|
||||
<result column="CONTENT" property="content" />
|
||||
<result column="RESOURCE_URL" property="resourceUrl" />
|
||||
<result column="DELETED" property="deleted" />
|
||||
<result column="CREATE_TIME" property="createTime" />
|
||||
<result column="IP_ADDRESS" property="ipAddress" />
|
||||
<result column="IP_REAL_ADDRESS" property="ipRealAddress" />
|
||||
</resultMap>
|
||||
|
||||
<!-- 通用查询结果列 -->
|
||||
<sql id="Base_Column_List">
|
||||
CHAT_ID, FROM_ID, FROM_NAME, TO_ID, TO_NAME, MESSAGE_TYPE, MESSAGE_STATUS, CONTENT, RESOURCE_URL, DELETED, CREATE_TIME, IP_ADDRESS, IP_REAL_ADDRESS
|
||||
</sql>
|
||||
|
||||
<select id="selectChatPage" resultType="com.dd.admin.business.chat.domain.ChatVo">
|
||||
select
|
||||
a.*,
|
||||
b.AVATAR_URL as toAvatar,
|
||||
c.AVATAR_URL as fromAvatar
|
||||
from business_chat a
|
||||
left join business_author b on a.to_id = b.author_id
|
||||
left join business_author c on a.from_id = c.author_id
|
||||
where
|
||||
( (a.from_id = #{chatDto.fromId} and a.to_id = #{chatDto.toId})
|
||||
or (a.from_id = #{chatDto.toId} and a.to_id = #{chatDto.fromId}))
|
||||
order by create_time desc
|
||||
</select>
|
||||
|
||||
<select id="selectChatDetail" resultType="com.dd.admin.business.chat.domain.ChatVo">
|
||||
select
|
||||
*
|
||||
from business_chat where 1 = 1
|
||||
</select>
|
||||
<select id="selectChat" resultType="com.dd.admin.business.chat.domain.ChatVo"
|
||||
parameterType="com.dd.admin.business.chat.domain.ChatDto">
|
||||
select
|
||||
a.*,
|
||||
b.AVATAR_URL as toAvatar,
|
||||
c.AVATAR_URL as fromAvatar
|
||||
from business_chat a
|
||||
left join business_author b on a.to_id = b.author_id
|
||||
left join business_author c on a.from_id = c.author_id
|
||||
where 1 = 1
|
||||
<if test="chatId!= null and chatId!= ''">
|
||||
and a.chat_id = #{chatId}
|
||||
</if>
|
||||
limit 1
|
||||
</select>
|
||||
<select id="selectChatList" resultType="com.dd.admin.business.chat.domain.ChatVo"
|
||||
parameterType="java.lang.String">
|
||||
SELECT
|
||||
*, b.AVATAR_URL AS fromAvatar
|
||||
FROM
|
||||
business_chat a
|
||||
LEFT JOIN business_author b ON a.FROM_ID = b.AUTHOR_ID
|
||||
WHERE
|
||||
a.TO_ID = #{toId}
|
||||
GROUP BY
|
||||
a.TO_ID = #{toId}
|
||||
ORDER BY
|
||||
a.create_time DESC
|
||||
</select>
|
||||
</mapper>
|
@ -0,0 +1,29 @@
|
||||
package com.dd.admin.business.chat.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.dd.admin.business.chat.entity.Chat;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.dd.admin.business.chat.domain.ChatVo;
|
||||
import com.dd.admin.business.chat.domain.ChatDto;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 服务类
|
||||
* </p>
|
||||
*
|
||||
* @author 727869402@qq.com
|
||||
* @since 2024-12-28
|
||||
*/
|
||||
public interface ChatService extends IService<Chat> {
|
||||
|
||||
//-分页列表
|
||||
IPage<ChatVo> selectChatPage(ChatDto chatDto);
|
||||
ChatVo selectChat(String chatId);
|
||||
//-列表
|
||||
List<ChatVo> selectChatDetail(ChatDto chatDto);
|
||||
List<ChatVo> selectChatList(String toId);
|
||||
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package com.dd.admin.business.chat.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.dd.admin.common.model.PageFactory;
|
||||
import com.dd.admin.business.chat.entity.Chat;
|
||||
import com.dd.admin.business.chat.mapper.ChatMapper;
|
||||
import com.dd.admin.business.chat.service.ChatService;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.dd.admin.business.chat.domain.ChatVo;
|
||||
import com.dd.admin.business.chat.domain.ChatDto;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 服务实现类
|
||||
* </p>
|
||||
*
|
||||
* @author 727869402@qq.com
|
||||
* @since 2024-12-28
|
||||
*/
|
||||
@Service
|
||||
public class ChatServiceImpl extends ServiceImpl<ChatMapper, Chat> implements ChatService {
|
||||
|
||||
@Override
|
||||
public IPage<ChatVo> selectChatPage(ChatDto chatDto) {
|
||||
Page page = PageFactory.defaultPage();
|
||||
return baseMapper.selectChatPage(page,chatDto);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatVo selectChat(String chatId) {
|
||||
return baseMapper.selectChat(chatId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ChatVo> selectChatDetail(ChatDto chatDto) {
|
||||
return baseMapper.selectChatDetail(chatDto);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ChatVo> selectChatList(String toId) {
|
||||
return baseMapper.selectChatList(toId);
|
||||
}
|
||||
}
|
@ -1 +1 @@
|
||||
package com.dd.admin.business.webSocket;
import com.alibaba.fastjson.JSON;
import com.dd.admin.common.utils.AddressUtils;
import com.dd.admin.common.utils.IPUtils;
import com.dd.admin.common.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.http.common.HttpRequest;
import org.tio.http.common.HttpResponse;
import org.tio.utils.lock.SetWithLock;
import org.tio.websocket.common.WsRequest;
import org.tio.websocket.server.handler.IWsMsgHandler;
import org.tio.websocket.starter.TioWebSocketServerBootstrap;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@Component
public class MyWebSocketMsgHandler implements IWsMsgHandler {
MyWebSocketMsgHandler handler;
private static Logger log = LoggerFactory.getLogger(MyWebSocketMsgHandler.class);
@PostConstruct
public void init() {
handler = this;
}
@Autowired
public TioWebSocketServerBootstrap bootstrap;
@Autowired
Map<String, MsgHandlerInterface> handlerInterfaceMap;
@Autowired
HttpServletRequest servletRequest;
@Override
public HttpResponse handshake(HttpRequest request, HttpResponse httpResponse, ChannelContext channelContext) throws Exception {
String authorId = request.getParam("authorId");
Tio.bindUser(channelContext,authorId);
String ipAddr = IPUtils.getIpAddr(servletRequest);
String realAddress = AddressUtils.getRealAddress(ipAddr);
System.out.println(authorId+":进入了Tio id:"+authorId+" ip:"+ ipAddr);
SetWithLock<ChannelContext> channelContexts = Tio.getAllChannelContexts(bootstrap.getServerGroupContext());
Set<ChannelContext> contextList = channelContexts.getObj();
System.out.println("当前在线用户:");
for(ChannelContext context:contextList){
System.out.println(context.getId()+"\t");
}
Integer count = channelContexts.size();
System.out.println(count);
return httpResponse;
}
@Override
public void onAfterHandshaked(HttpRequest httpRequest, HttpResponse httpResponse, ChannelContext channelContext) throws Exception {
// System.out.println("握手成功进入群组");
}
@Override
public Object onBytes(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) throws Exception {
System.out.println("接收到bytes消息");
return null;
}
@Override
public Object onClose(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) throws Exception {
return null;
}
@Override
public Object onText(WsRequest wsRequest, String text, ChannelContext channelContext) throws Exception {
if(text.equals("心跳内容")) return null;
System.out.println("接收到文本消息:"+text);
Map map = JSON.parseObject(text,Map.class);
String handlerType =(String)map.get("handlerType");
if(!StringUtil.isEmpty(handlerType)){
MsgHandlerInterface msgHandler = (MsgHandlerInterface) handlerInterfaceMap.get(handlerType);
if(msgHandler!=null){
msgHandler.handler(map,channelContext);
}else{
log.debug("非法请求...");
}
}else{
log.debug("非法请求...");
}
System.out.println(map);
return null;
}
}
|
||||
package com.dd.admin.business.webSocket;
import com.alibaba.fastjson.JSON;
import com.dd.admin.common.utils.AddressUtils;
import com.dd.admin.common.utils.IPUtils;
import com.dd.admin.common.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.http.common.HttpRequest;
import org.tio.http.common.HttpResponse;
import org.tio.utils.lock.SetWithLock;
import org.tio.websocket.common.WsRequest;
import org.tio.websocket.server.handler.IWsMsgHandler;
import org.tio.websocket.starter.TioWebSocketServerBootstrap;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@Component
public class MyWebSocketMsgHandler implements IWsMsgHandler {
MyWebSocketMsgHandler handler;
private static Logger log = LoggerFactory.getLogger(MyWebSocketMsgHandler.class);
@PostConstruct
public void init() {
handler = this;
}
@Autowired
public TioWebSocketServerBootstrap bootstrap;
@Autowired
Map<String, MsgHandlerInterface> handlerInterfaceMap;
@Override
public HttpResponse handshake(HttpRequest request, HttpResponse httpResponse, ChannelContext channelContext) throws Exception {
String authorId = request.getParam("authorId");
String authorName = request.getParam("authorName");
Tio.bindUser(channelContext,authorId);
String ipAddr = request.getClientIp();
String realAddress = AddressUtils.getRealAddress(ipAddr);
System.out.println(authorId+":进入了Tio id:"+authorId+" ip:"+ ipAddr);
SetWithLock<ChannelContext> channelContexts = Tio.getAllChannelContexts(bootstrap.getServerGroupContext());
Set<ChannelContext> contextList = channelContexts.getObj();
System.out.println("当前在线用户:");
for(ChannelContext context:contextList){
System.out.println(context.userid+"\t");
}
Integer count = channelContexts.size();
System.out.println(count);
return httpResponse;
}
@Override
public void onAfterHandshaked(HttpRequest httpRequest, HttpResponse httpResponse, ChannelContext channelContext) throws Exception {
// System.out.println("握手成功进入群组");
}
@Override
public Object onBytes(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) throws Exception {
System.out.println("接收到bytes消息");
return null;
}
@Override
public Object onClose(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) throws Exception {
return null;
}
@Override
public Object onText(WsRequest wsRequest, String text, ChannelContext channelContext) throws Exception {
if(text.equals("心跳内容")) return null;
System.out.println("接收到文本消息:"+text);
Map map = JSON.parseObject(text,Map.class);
String handlerType =(String)map.get("handlerType");
if(!StringUtil.isEmpty(handlerType)){
MsgHandlerInterface msgHandler = (MsgHandlerInterface) handlerInterfaceMap.get(handlerType);
if(msgHandler!=null){
msgHandler.handler(map,channelContext);
}else{
log.debug("非法请求...");
}
}else{
log.debug("非法请求...");
}
System.out.println(map);
return null;
}
}
|
@ -1 +1 @@
|
||||
package com.dd.admin.business.webSocket.handler;
import cn.hutool.core.bean.BeanUtil;
import com.dd.admin.business.chat.service.ChatService;
import com.dd.admin.business.webSocket.MsgHandlerInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.utils.lock.SetWithLock;
import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.Map;
import java.util.Set;
@Component
@Slf4j
public class P2PMessageHandler implements MsgHandlerInterface {
public static P2PMessageHandler handler;
@Autowired
ChatService chatService;
@PostConstruct
public void init() {
handler = this;
handler.chatService = this.chatService;
}
@Override
public Object handler(Map map, ChannelContext context ){
Map<String, String> mine = (Map) map.get("mine");
Map<String, String> to = (Map) map.get("to");
//
// String fromId = mine.get("id");
// String content = mine.get("content");
// String avatar = mine.get("avatar");
// String userName = mine.get("username");
//
// String type = to.get("type");
// String toId = to.get("id");
// boolean isMine = false;
// Long timestamp = new Date().getTime();
//
// ChatmessageParam message = new ChatmessageParam();
// message.setUsername(userName);
// message.setAvatar(avatar);
// message.setContent(content);
// message.setToUid(Long.valueOf(toId));
// message.setType(type);
// message.setTimestamp(timestamp);
// message.setFromid(Long.valueOf(fromId));
//// handler.chatmessageService.add(message);
//
// Map messageMap = BeanUtil.beanToMap(message);
// messageMap.put("mine",false);
// messageMap.put("id",fromId);
//
//
// //t-io支持多点登录,获取的是一个集合,因为此账号可能存在多个连接哦
// SetWithLock<ChannelContext> contexts = Tio.getChannelContextsByUserid(context.getGroupContext(), toId);
// //用户在线
// if(contexts!=null && contexts.size() > 0) {
// Set<ChannelContext> contextList = contexts.getObj();
// //t-io支持多点登录,获取的是一个集合,向集合发送聊天信息
// for (ChannelContext con : contextList) {
// TioUtil.sendMessage(con, HandlerType.P2P_REQ, messageMap);
// message.setUnreadPoint(1);
// }
// }else{
// message.setUnreadPoint(2);
// }
// handler.chatmessageService.add(message);
return null;
}}
|
||||
package com.dd.admin.business.webSocket.handler;
import cn.hutool.core.bean.BeanUtil;
import com.dd.admin.business.chat.domain.ChatVo;
import com.dd.admin.business.chat.entity.Chat;
import com.dd.admin.business.chat.service.ChatService;
import com.dd.admin.business.webSocket.MsgHandlerInterface;
import com.dd.admin.business.webSocket.util.TioUtil;
import com.dd.admin.common.utils.AddressUtils;
import com.dd.admin.common.utils.HttpContext;
import com.dd.admin.common.utils.IPUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.http.common.HttpRequest;
import org.tio.utils.lock.SetWithLock;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;
import java.util.Set;
@Component
@Slf4j
@Service("5")
public class P2PMessageHandler implements MsgHandlerInterface {
public static P2PMessageHandler handler;
@Autowired
ChatService chatService;
@Override
public Object handler(Map map, ChannelContext context ){
Chat chat = BeanUtil.toBean(map, Chat.class);
chat.setIpAddress(context.getClientNode().getIp());
chat.setIpRealAddress(AddressUtils.getRealAddress(chat.getIpAddress())); //ip真实地址
chatService.save(chat);
ChatVo chatVo = chatService.selectChat(chat.getChatId());
//t-io支持多点登录,获取的是一个集合,因为此账号可能存在多个连接哦
SetWithLock<ChannelContext> contexts = Tio.getChannelContextsByUserid(context.getGroupContext(), chat.getToId());
//用户在线
if(contexts!=null && contexts.size() > 0) {
Set<ChannelContext> contextList = contexts.getObj();
//t-io支持多点登录,获取的是一个集合,向集合发送聊天信息
for (ChannelContext con : contextList) {
chat.setMessageStatus(1);
TioUtil.sendMessage(con, "5", chatVo);
}
}
//也要给我自己发用于数据回显
//t-io支持多点登录,获取的是一个集合,因为此账号可能存在多个连接哦
SetWithLock<ChannelContext> contexts1 = Tio.getChannelContextsByUserid(context.getGroupContext(), chat.getFromId());
//用户在线
if(contexts1!=null && contexts1.size() > 0) {
Set<ChannelContext> contextList = contexts1.getObj();
//t-io支持多点登录,获取的是一个集合,向集合发送聊天信息
for (ChannelContext con : contextList) {
TioUtil.sendMessage(con, "5", chatVo);
}
}
return null;
}}
|
@ -1 +1 @@
|
||||
package com.dd.admin.business.webSocket.util;
import com.alibaba.fastjson.JSON;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.websocket.common.WsResponse;
public class TioUtil {
/**
* 快速生成WsResponse
* @param action
* @param body
* @return
*/
public static WsResponse madeWsResponse(String handlerType, Object body){
String reponseMsg = JSON.toJSONString(body);
return WsResponse.fromText(reponseMsg,"utf-8");
}
public static boolean sendMessage(ChannelContext con, String handlerType, Object body){
WsResponse weResponse = madeWsResponse(handlerType,body);
return Tio.send(con,weResponse);
}
}
|
||||
package com.dd.admin.business.webSocket.util;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.websocket.common.WsResponse;
public class TioUtil {
/**
* 快速生成WsResponse
* @param action
* @param body
* @return
*/
public static WsResponse madeWsResponse(String handlerType, Object body){
// String responseMsg = JSON.toJSONString(body);
// System.out.println(responseMsg);
// 创建ObjectMapper对象
ObjectMapper objectMapper = new ObjectMapper();
// 开启美化输出(格式化)功能
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
// 将List<Person>对象转换为JSON字符串
try {
String responseMsg = objectMapper.writeValueAsString(body);
System.out.println(responseMsg);
return WsResponse.fromText(responseMsg,"utf-8");
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
public static boolean sendMessage(ChannelContext con, String handlerType, Object body){
WsResponse weResponse = madeWsResponse(handlerType,body);
return Tio.send(con,weResponse);
}
}
|
Loading…
x
Reference in New Issue
Block a user