From 24c6538084e2ae727e725548ddc4fc4a3edd7016 Mon Sep 17 00:00:00 2001 From: heavyrian2012 Date: Fri, 17 Nov 2023 09:06:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=85=8D=E7=BD=AE=EF=BC=8C?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=A4=9A=E7=AB=AF=E5=90=8C=E6=97=B6=E5=8A=A0?= =?UTF-8?q?=E5=85=A5=E8=81=8A=E5=A4=A9=E5=AE=A4=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- broker/config/wildfirechat.conf | 6 +++++ .../java/io/moquette/BrokerConstants.java | 2 +- .../persistence/MemoryMessagesStore.java | 25 ++++++++++++++++++- .../cn/wildfirechat/common/ErrorCode.java | 2 ++ .../src/main/resources/wildfirechat.conf | 6 +++++ 5 files changed, 39 insertions(+), 2 deletions(-) diff --git a/broker/config/wildfirechat.conf b/broker/config/wildfirechat.conf index 073ba47c..95081f84 100755 --- a/broker/config/wildfirechat.conf +++ b/broker/config/wildfirechat.conf @@ -172,6 +172,12 @@ chatroom.rejoin_when_active true ## 当一个聊天室不存在时,如果有用户加入就自动创建 chatroom.create_when_not_exist true +## 一个用户同一时刻只能加入一个聊天室。 +## 当一个客户端加入一个聊天室时,如果这个客户端已经加入另外一个聊天室了,会自动退出之前的聊天室。 +## 当一个客户端加入一个聊天室时,如果这个用户的其他端已经加入一个聊天室了,kickoff_other_platform为true时会自动退出之前的聊天室,为false时会返回225的错误码。 +## 当自动退出聊天室时,客户端不会收到任何通知,只是不会再收到消息。 +chatroom.kickoff_other_platform true + ## 是否允许群主不受限制地撤回自己发送的消息。false可以${message.recall_time_limit}撤回自己的消息;true可以撤回任意时间自己的消息。默认为true。 group.allow_owner_recall_self_msg true diff --git a/broker/src/main/java/io/moquette/BrokerConstants.java b/broker/src/main/java/io/moquette/BrokerConstants.java index aac4d210..630b2f93 100755 --- a/broker/src/main/java/io/moquette/BrokerConstants.java +++ b/broker/src/main/java/io/moquette/BrokerConstants.java @@ -172,7 +172,7 @@ public final class BrokerConstants { public static final String CHATROOM_Participant_Idle_Time = "chatroom.participant_idle_time"; public static final String CHATROOM_Rejoin_When_Active = "chatroom.rejoin_when_active"; public static final String CHATROOM_Create_When_Not_Exist = "chatroom.create_when_not_exist"; - + public static final String CHATROOM_Kickoff_Other_Platform = "chatroom.kickoff_other_platform"; public static final String GROUP_Allow_Owner_Recall_Self_Msg = "group.allow_owner_recall_self_msg"; public static final String GROUP_Allow_Manager_Recall_Self_Msg = "group.allow_manager_recall_self_msg"; diff --git a/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java b/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java index c8bbaea8..dd37b24a 100755 --- a/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java +++ b/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java @@ -166,6 +166,7 @@ public class MemoryMessagesStore implements IMessagesStore { private long mChatroomParticipantIdleTime = 900000; private boolean mChatroomRejoinWhenActive = true; private boolean mChatroomCreateWhenNotExist = true; + private boolean mChatroomKickoffOtherPlatform = true; private List mForbiddenClientSendTypes = new ArrayList<>(); private List mBlackListExceptionTypes = new ArrayList<>(); @@ -346,6 +347,12 @@ public class MemoryMessagesStore implements IMessagesStore { printMissConfigLog(CHATROOM_Create_When_Not_Exist, mChatroomCreateWhenNotExist + ""); } + try { + mChatroomKickoffOtherPlatform = Boolean.parseBoolean(m_Server.getConfig().getProperty(BrokerConstants.CHATROOM_Kickoff_Other_Platform, "true")); + } catch (Exception e) { + } + + try { mChatroomParticipantIdleTime = Long.parseLong(m_Server.getConfig().getProperty(CHATROOM_Participant_Idle_Time, "900000")); } catch (Exception e) { @@ -3563,9 +3570,25 @@ public class MemoryMessagesStore implements IMessagesStore { } } + MultiMap chatroomMembers = m_Server.getHazelcastInstance().getMultiMap(CHATROOM_MEMBER_IDS); + String alreadyInChatroom = (String) m_Server.getHazelcastInstance().getMap(USER_CHATROOM).get(userId); + if(alreadyInChatroom != null) { + for (UserClientEntry userClientEntry : chatroomMembers.get(alreadyInChatroom)) { + if(userClientEntry.userId.equals(userId)) { + if(!chatroomId.equals(alreadyInChatroom) || !userClientEntry.clientId.equals(clientId)) { + if(mChatroomKickoffOtherPlatform) { + handleQuitChatroom(userId, userClientEntry.clientId, chatroomId); + } else { + return ErrorCode.ERROR_CODE_OTHER_CLIENT_ALREADY_IN_CHATROOM; + } + } + } + } + } + m_Server.getStore().sessionsStore().getSession(clientId).refreshLastChatroomActiveTime(); m_Server.getHazelcastInstance().getMap(USER_CHATROOM).put(userId, chatroomId); - m_Server.getHazelcastInstance().getMultiMap(CHATROOM_MEMBER_IDS).put(chatroomId, new UserClientEntry(userId, clientId)); + chatroomMembers.put(chatroomId, new UserClientEntry(userId, clientId)); mWriteLock.lock(); chatroomMessages.put(userId, new TreeMap<>()); diff --git a/common/src/main/java/cn/wildfirechat/common/ErrorCode.java b/common/src/main/java/cn/wildfirechat/common/ErrorCode.java index 47a35e15..cc72f1da 100755 --- a/common/src/main/java/cn/wildfirechat/common/ErrorCode.java +++ b/common/src/main/java/cn/wildfirechat/common/ErrorCode.java @@ -66,6 +66,8 @@ public enum ErrorCode { ERROR_CODE_FUNCTION_DISABLED(220, "function disabled"), + ERROR_CODE_OTHER_CLIENT_ALREADY_IN_CHATROOM(225, "other client already in chatroom"), + ERROR_CODE_CHANNEL_NO_EXIST(236, "channel no exist"), ERROR_CODE_CHANNEL_NO_SECRET(237, "channel no secret"), ERROR_CODE_USER_NOT_PREPARED(238, "user not prepared"), diff --git a/distribution/src/main/resources/wildfirechat.conf b/distribution/src/main/resources/wildfirechat.conf index a825af5a..e1f75767 100755 --- a/distribution/src/main/resources/wildfirechat.conf +++ b/distribution/src/main/resources/wildfirechat.conf @@ -170,6 +170,12 @@ chatroom.rejoin_when_active true ## 当一个聊天室不存在时,如果有用户加入就自动创建 chatroom.create_when_not_exist true +## 一个用户同一时刻只能加入一个聊天室。 +## 当一个客户端加入一个聊天室时,如果这个客户端已经加入另外一个聊天室了,会自动退出之前的聊天室。 +## 当一个客户端加入一个聊天室时,如果这个用户的其他端已经加入一个聊天室了,kickoff_other_platform为true时会自动退出之前的聊天室,为false时会返回225的错误码。 +## 当自动退出聊天室时,客户端不会收到任何通知,只是不会再收到消息。 +chatroom.kickoff_other_platform true + ## 是否允许群主不受限制地撤回自己发送的消息。false可以${message.recall_time_limit}撤回自己的消息;true可以撤回任意时间自己的消息。默认为true。 group.allow_owner_recall_self_msg true