集成websocket 未读提示
This commit is contained in:
parent
a116c6276c
commit
026a91aecb
@ -8,6 +8,7 @@ import com.dd.admin.business.author.entity.Author;
|
|||||||
import com.dd.admin.business.author.service.AuthorService;
|
import com.dd.admin.business.author.service.AuthorService;
|
||||||
import com.dd.admin.business.chat.domain.ChatDto;
|
import com.dd.admin.business.chat.domain.ChatDto;
|
||||||
import com.dd.admin.business.chat.domain.ChatVo;
|
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.chat.service.ChatService;
|
||||||
import com.dd.admin.business.file.entity.File;
|
import com.dd.admin.business.file.entity.File;
|
||||||
import com.dd.admin.business.file.service.FileService;
|
import com.dd.admin.business.file.service.FileService;
|
||||||
@ -641,4 +642,24 @@ public class AuthApi {
|
|||||||
return ResultBean.success(chatVos);
|
return ResultBean.success(chatVos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiOperation(value = "读取消息")
|
||||||
|
@ApiOperationSupport(order = 1)
|
||||||
|
@PostMapping("/api/auth/readAuthorMessage")
|
||||||
|
@OperLog(operModule = "读取消息",operType = OperType.OTHER,operDesc = "读取消息")
|
||||||
|
public ResultBean readAuthorMessage(String authorId) {
|
||||||
|
String loginId = String.valueOf(request.getAttribute("authorId"));
|
||||||
|
chatService.readMessage(authorId,loginId);
|
||||||
|
return ResultBean.success(loginId);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ApiOperation(value = "查询我的未读消息数量")
|
||||||
|
@ApiOperationSupport(order = 1)
|
||||||
|
@GetMapping("/api/auth/getUnReadCount")
|
||||||
|
@OperLog(operModule = "查询我的未读消息数量",operType = OperType.OTHER,operDesc = "查询我的未读消息数量")
|
||||||
|
public ResultBean getUnReadCount() {
|
||||||
|
String loginId = String.valueOf(request.getAttribute("authorId"));
|
||||||
|
Integer unReadCount = chatService.selectUnReadCount(loginId);
|
||||||
|
return ResultBean.success(unReadCount);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
@ -78,4 +78,5 @@ public class ChatVo {
|
|||||||
private String authorId;
|
private String authorId;
|
||||||
private String authorName;
|
private String authorName;
|
||||||
private String authorAvatar;
|
private String authorAvatar;
|
||||||
|
private String unReadCount;
|
||||||
}
|
}
|
||||||
|
@ -66,7 +66,17 @@
|
|||||||
a.FROM_NAME AS authorName,
|
a.FROM_NAME AS authorName,
|
||||||
b.AVATAR_URL AS authorAvatar,
|
b.AVATAR_URL AS authorAvatar,
|
||||||
a.content,
|
a.content,
|
||||||
a.create_time
|
a.create_time,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(1)
|
||||||
|
FROM
|
||||||
|
business_chat ca
|
||||||
|
WHERE
|
||||||
|
ca.FROM_ID = a.FROM_ID
|
||||||
|
AND ca.to_id = #{authorId}
|
||||||
|
and ca.MESSAGE_STATUS = 0
|
||||||
|
) as unReadCount
|
||||||
FROM
|
FROM
|
||||||
business_chat a
|
business_chat a
|
||||||
LEFT JOIN
|
LEFT JOIN
|
||||||
@ -79,7 +89,8 @@
|
|||||||
a.TO_NAME AS authorName,
|
a.TO_NAME AS authorName,
|
||||||
b.AVATAR_URL AS authorAvatar,
|
b.AVATAR_URL AS authorAvatar,
|
||||||
a.content,
|
a.content,
|
||||||
a.create_time
|
a.create_time,
|
||||||
|
0 as unReadCount
|
||||||
FROM
|
FROM
|
||||||
business_chat a
|
business_chat a
|
||||||
LEFT JOIN
|
LEFT JOIN
|
||||||
|
@ -26,4 +26,6 @@ public interface ChatService extends IService<Chat> {
|
|||||||
List<ChatVo> selectChatDetail(ChatDto chatDto);
|
List<ChatVo> selectChatDetail(ChatDto chatDto);
|
||||||
List<ChatVo> selectChatList(String authorId);
|
List<ChatVo> selectChatList(String authorId);
|
||||||
|
|
||||||
|
void readMessage(String authorId,String loginId);
|
||||||
|
Integer selectUnReadCount(String authorId);
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
package com.dd.admin.business.chat.service.impl;
|
package com.dd.admin.business.chat.service.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.dd.admin.common.model.PageFactory;
|
import com.dd.admin.common.model.PageFactory;
|
||||||
@ -43,4 +46,21 @@ public class ChatServiceImpl extends ServiceImpl<ChatMapper, Chat> implements Ch
|
|||||||
public List<ChatVo> selectChatList(String authorId) {
|
public List<ChatVo> selectChatList(String authorId) {
|
||||||
return baseMapper.selectChatList(authorId);
|
return baseMapper.selectChatList(authorId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void readMessage(String authorId, String loginId) {
|
||||||
|
LambdaUpdateWrapper<Chat> queryWrapper = new LambdaUpdateWrapper();
|
||||||
|
queryWrapper.eq(Chat::getFromId,authorId);
|
||||||
|
queryWrapper.eq(Chat::getToId,loginId);
|
||||||
|
queryWrapper.set(Chat::getMessageStatus,1);
|
||||||
|
this.update(queryWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer selectUnReadCount(String authorId) {
|
||||||
|
LambdaQueryWrapper<Chat> queryWrapper = new LambdaQueryWrapper();
|
||||||
|
queryWrapper.eq(Chat::getToId,authorId);
|
||||||
|
queryWrapper.eq(Chat::getMessageStatus,0);
|
||||||
|
return baseMapper.selectCount(queryWrapper);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1 +1 @@
|
|||||||
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;
}}
|
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) {
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;
}}
|
Loading…
x
Reference in New Issue
Block a user