From e136f303a725cd9b923036e93f3f7576d0d6e166 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=B9=E7=A3=8A?= <45566618@qq.com> Date: Fri, 30 Aug 2024 11:39:00 +0800 Subject: [PATCH] =?UTF-8?q?=E8=81=8A=E5=A4=A9=E5=AE=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/controller/app/AppChatSocket.java | 27 ++++++++++++++----- .../sqx/modules/chat/dao/ChatContentDao.java | 2 ++ .../chat/service/ChatContentService.java | 4 +++ .../service/impl/ChatContentServiceImpl.java | 6 +++++ .../service/impl/SysUserTokenServiceImpl.java | 2 +- .../resources/mapper/chat/ChatContentDao.xml | 5 ++++ 6 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/sqx/modules/chat/controller/app/AppChatSocket.java b/src/main/java/com/sqx/modules/chat/controller/app/AppChatSocket.java index 9e62ec0..45b47e4 100644 --- a/src/main/java/com/sqx/modules/chat/controller/app/AppChatSocket.java +++ b/src/main/java/com/sqx/modules/chat/controller/app/AppChatSocket.java @@ -17,6 +17,7 @@ import javax.websocket.server.ServerEndpoint; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -33,6 +34,9 @@ public class AppChatSocket {//用户聊天 * 以用户的id为key,WebSocket为对象保存起来 */ private static Map clients = new ConcurrentHashMap(); + + private static Map sessionMap = new ConcurrentHashMap(); + private static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); /** * 会话 @@ -76,6 +80,7 @@ public class AppChatSocket {//用户聊天 clients.remove(userId); } clients.put(userId, this); + sessionMap.put(userId,session); /*sendMessageTo("恭喜你连接成功!",wxUserId);*/ } @@ -129,13 +134,21 @@ public class AppChatSocket {//用户聊天 wxChatContent.setContent(textMessage); wxChatContent.setMessageType(messageType); wxChatContent.setCreateTime(sdf.format(new Date())); - //判断对方是否在线 - AppChatSocket chatSocket = clients.get(userId); - if (chatSocket!=null) { - chatSocket.session.getAsyncRemote().sendText(message); - } wxChatContent.setStatus(0); chatContentService.save(wxChatContent); + //判断对方是否在线 +// AppChatSocket chatSocket = clients.get(userId); +// if (chatSocket!=null) { +// chatSocket.session.getAsyncRemote().sendText(message); +// } + List toUsers = chatContentService.selectUserByConversationId(Long.parseLong(chatConversationId)); + for(ChatContent toUser :toUsers){ + Session userSession = sessionMap.get(toUser.getUserId()); + if (userSession!=null) { + userSession.getAsyncRemote().sendText(message); + System.err.println(this.userId+"发送成功:"+userId); + } + } UserEntity userEntity = userService.selectUserById(Long.parseLong(userId)); if(userEntity!=null && StringUtils.isNotBlank(userEntity.getClientid())){ UserEntity user = userService.selectUserById(Long.parseLong(this.userId)); @@ -146,6 +159,9 @@ public class AppChatSocket {//用户聊天 } userService.pushToSingle("新消息提醒",user.getUserName()+":"+textMessage,userEntity.getClientid()); } + + + /*AppChatSocket chatSocket = clients.get(userId); if (chatSocket!=null) { chatSocket.session.getAsyncRemote().sendText(message); @@ -154,7 +170,6 @@ public class AppChatSocket {//用户聊天 catch (Exception e){ log.error("发生了错误了"+e.getMessage(),e); } - } diff --git a/src/main/java/com/sqx/modules/chat/dao/ChatContentDao.java b/src/main/java/com/sqx/modules/chat/dao/ChatContentDao.java index 2f02c60..b4f9e0f 100644 --- a/src/main/java/com/sqx/modules/chat/dao/ChatContentDao.java +++ b/src/main/java/com/sqx/modules/chat/dao/ChatContentDao.java @@ -22,4 +22,6 @@ public interface ChatContentDao extends BaseMapper { int selectChatCount(@Param("userId") Long userId); + List selectUserByConversationId(@Param("chatConversationId") Long chatConversationId); + } \ No newline at end of file diff --git a/src/main/java/com/sqx/modules/chat/service/ChatContentService.java b/src/main/java/com/sqx/modules/chat/service/ChatContentService.java index 6420b35..cbdfe73 100644 --- a/src/main/java/com/sqx/modules/chat/service/ChatContentService.java +++ b/src/main/java/com/sqx/modules/chat/service/ChatContentService.java @@ -6,6 +6,8 @@ import com.sqx.common.utils.PageUtils; import com.sqx.modules.chat.entity.ChatContent; import org.apache.ibatis.annotations.Param; +import java.util.List; + public interface ChatContentService extends IService { PageUtils selectChatContentPage(Integer page, Integer limit, Long chatConversationId,String content); @@ -14,4 +16,6 @@ public interface ChatContentService extends IService { int selectChatCount(Long userId); + List selectUserByConversationId(Long chatConversationId); + } \ No newline at end of file diff --git a/src/main/java/com/sqx/modules/chat/service/impl/ChatContentServiceImpl.java b/src/main/java/com/sqx/modules/chat/service/impl/ChatContentServiceImpl.java index 17763d4..2239a73 100644 --- a/src/main/java/com/sqx/modules/chat/service/impl/ChatContentServiceImpl.java +++ b/src/main/java/com/sqx/modules/chat/service/impl/ChatContentServiceImpl.java @@ -15,6 +15,7 @@ import org.springframework.stereotype.Service; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.List; @Service @@ -36,4 +37,9 @@ public class ChatContentServiceImpl extends ServiceImpl selectUserByConversationId(Long chatConversationId) { + return baseMapper.selectUserByConversationId(chatConversationId); + } + } \ No newline at end of file diff --git a/src/main/java/com/sqx/modules/sys/service/impl/SysUserTokenServiceImpl.java b/src/main/java/com/sqx/modules/sys/service/impl/SysUserTokenServiceImpl.java index 1e40e84..8d60d0d 100644 --- a/src/main/java/com/sqx/modules/sys/service/impl/SysUserTokenServiceImpl.java +++ b/src/main/java/com/sqx/modules/sys/service/impl/SysUserTokenServiceImpl.java @@ -47,7 +47,7 @@ public class SysUserTokenServiceImpl extends ServiceImpl + \ No newline at end of file