From 7c4fe831b3e052cbd3925187c8563cf3a4f6e324 Mon Sep 17 00:00:00 2001 From: "heavyrain.lee" Date: Sun, 5 Jan 2020 12:32:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=81=8A=E5=A4=A9=E5=AE=A4?= =?UTF-8?q?=E9=80=BB=E8=BE=91=EF=BC=8C=E6=B7=BB=E5=8A=A0=E8=81=8A=E5=A4=A9?= =?UTF-8?q?=E5=AE=A4=E7=A9=BA=E9=97=B2=E9=80=80=E5=87=BA=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E5=92=8C=E5=8F=91=E6=B6=88=E6=81=AF=E9=87=8D=E6=96=B0=E5=8A=A0?= =?UTF-8?q?=E5=85=A5=E9=85=8D=E7=BD=AE?= 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 | 4 ++ .../persistence/MemoryMessagesStore.java | 45 ++++++++++++++++--- .../java/io/moquette/spi/IMessagesStore.java | 3 ++ .../moquette/spi/impl/MessagesPublisher.java | 2 +- .../src/main/resources/wildfirechat.conf | 6 +++ 6 files changed, 60 insertions(+), 6 deletions(-) diff --git a/broker/config/wildfirechat.conf b/broker/config/wildfirechat.conf index 7e44c5ae..9dfaa1b4 100755 --- a/broker/config/wildfirechat.conf +++ b/broker/config/wildfirechat.conf @@ -74,6 +74,12 @@ friend.repeat_request_duration 604800000 ##好友请求被拒绝后再次发送的间隔,单位是毫秒,默认是30天,0为不限制 friend.reject_request_duration 2592000000 +## 聊天室观众空闲退出时间,单位为毫秒,默认为15分钟,0为永远不退出 +chatroom.participant_idle_time 900000 + +## 用户向聊天室发送消息,自动加入聊天室 +chatroom.rejoin_when_active true + #********************************************************************* # hazelcase configuration #********************************************************************* diff --git a/broker/src/main/java/io/moquette/BrokerConstants.java b/broker/src/main/java/io/moquette/BrokerConstants.java index 52d910b8..115d9481 100755 --- a/broker/src/main/java/io/moquette/BrokerConstants.java +++ b/broker/src/main/java/io/moquette/BrokerConstants.java @@ -125,6 +125,10 @@ public final class BrokerConstants { public static final String FRIEND_Repeat_Request_Duration = "friend.repeat_request_duration"; public static final String FRIEND_Reject_Request_Duration = "friend.reject_request_duration"; + public static final String CHATROOM_Participant_Idle_Time = "chatroom.participant_idle_time"; + public static final String CHATROOM_Rejoin_When_Active = "chatroom.rejoin_when_active"; + + private BrokerConstants() { } } diff --git a/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java b/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java index 13841806..40c8adaa 100755 --- a/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java +++ b/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java @@ -124,6 +124,9 @@ public class MemoryMessagesStore implements IMessagesStore { private long mFriendRejectDuration = 30 * 24 * 60 * 60 * 1000; private boolean mDisableStrangerChat = false; + private long mChatroomParticipantIdleTime = 900000; + private boolean mChatroomRejoinWhenActive = true; + MemoryMessagesStore(Server server, DatabaseStore databaseStore) { m_Server = server; this.databaseStore = databaseStore; @@ -148,6 +151,18 @@ public class MemoryMessagesStore implements IMessagesStore { } catch (Exception e) { e.printStackTrace(); } + + try { + mChatroomRejoinWhenActive = Boolean.parseBoolean(m_Server.getConfig().getProperty(BrokerConstants.CHATROOM_Rejoin_When_Active)); + } catch (Exception e) { + e.printStackTrace(); + } + + try { + mChatroomParticipantIdleTime = Long.parseLong(m_Server.getConfig().getProperty(CHATROOM_Participant_Idle_Time, "900000")); + } catch (Exception e) { + e.printStackTrace(); + } } @Override @@ -575,13 +590,21 @@ public class MemoryMessagesStore implements IMessagesStore { @Override public boolean checkUserClientInChatroom(String user, String clientId, String chatroomId) { MemorySessionStore.Session session = m_Server.getStore().sessionsStore().getSession(clientId); - if (session == null || System.currentTimeMillis() - session.getLastChatroomActiveTime() > 5 * 60 * 1000) { - handleQuitChatroom(user, clientId, chatroomId); - return false; - } String existChatroomId = (String)m_Server.getHazelcastInstance().getMap(USER_CHATROOM).get(user); - if (StringUtil.isNullOrEmpty(existChatroomId) && (chatroomId == null || !existChatroomId.equals(chatroomId))) { + if (StringUtil.isNullOrEmpty(existChatroomId) || !existChatroomId.equals(chatroomId)) { + if (mChatroomRejoinWhenActive) { + if (!StringUtil.isNullOrEmpty(existChatroomId)) { + handleQuitChatroom(user, clientId, existChatroomId); + } + handleJoinChatroom(user, clientId, chatroomId); + } else { + return false; + } + } + + if (!mChatroomRejoinWhenActive && !checkChatroomParticipantIdelTime(session)) { + handleQuitChatroom(user, clientId, chatroomId); return false; } @@ -2158,6 +2181,18 @@ public class MemoryMessagesStore implements IMessagesStore { return ErrorCode.ERROR_CODE_SUCCESS; } + @Override + public boolean checkChatroomParticipantIdelTime(MemorySessionStore.Session session) { + if (session == null) { + return false; + } + + if(mChatroomParticipantIdleTime > 0 && System.currentTimeMillis() - session.getLastChatroomActiveTime() > mChatroomParticipantIdleTime) { + return false; + } + return true; + } + @Override public ErrorCode deleteFriend(String userId, String friendUid, long[] head) { HazelcastInstance hzInstance = m_Server.getHazelcastInstance(); diff --git a/broker/src/main/java/io/moquette/spi/IMessagesStore.java b/broker/src/main/java/io/moquette/spi/IMessagesStore.java index 4ce37429..5aa5dcb1 100755 --- a/broker/src/main/java/io/moquette/spi/IMessagesStore.java +++ b/broker/src/main/java/io/moquette/spi/IMessagesStore.java @@ -21,6 +21,7 @@ import cn.wildfirechat.proto.WFCMessage; import com.xiaoleilu.loServer.model.FriendData; import cn.wildfirechat.pojos.InputOutputUserBlockStatus; import io.moquette.persistence.DatabaseStore; +import io.moquette.persistence.MemorySessionStore; import io.moquette.persistence.UserClientEntry; import io.moquette.spi.impl.subscriptions.Topic; import io.netty.buffer.ByteBuf; @@ -173,6 +174,8 @@ public interface IMessagesStore { ErrorCode handleJoinChatroom(String userId, String clientId, String chatroomId); ErrorCode handleQuitChatroom(String userId, String clientId, String chatroomId); + boolean checkChatroomParticipantIdelTime(MemorySessionStore.Session session); + ErrorCode getUserSettings(String userId, long version, WFCMessage.GetUserSettingResult.Builder builder); WFCMessage.UserSettingEntry getUserSetting(String userId, int scope, String key); List getUserSetting(String userId, int scope); diff --git a/broker/src/main/java/io/moquette/spi/impl/MessagesPublisher.java b/broker/src/main/java/io/moquette/spi/impl/MessagesPublisher.java index 35c9dc35..c791646c 100755 --- a/broker/src/main/java/io/moquette/spi/impl/MessagesPublisher.java +++ b/broker/src/main/java/io/moquette/spi/impl/MessagesPublisher.java @@ -216,7 +216,7 @@ public class MessagesPublisher { targetSession.refreshLastChatroomActiveTime(); } - if(System.currentTimeMillis() - targetSession.getLastChatroomActiveTime() > 5*60*1000) { + if (!m_messagesStore.checkChatroomParticipantIdelTime(targetSession)) { m_messagesStore.handleQuitChatroom(user, targetSession.getClientID(), target); continue; } diff --git a/distribution/src/main/resources/wildfirechat.conf b/distribution/src/main/resources/wildfirechat.conf index 01f0e4a9..886ebf92 100755 --- a/distribution/src/main/resources/wildfirechat.conf +++ b/distribution/src/main/resources/wildfirechat.conf @@ -64,6 +64,12 @@ friend.repeat_request_duration 604800000 ##好友请求被拒绝后再次发送的间隔,单位是毫秒,默认是30天,0为不限制 friend.reject_request_duration 2592000000 +## 聊天室观众空闲退出时间,单位为毫秒,默认为15分钟,0为永远不退出 +chatroom.participant_idle_time 900000 + +## 用户向聊天室发送消息,自动加入聊天室 +chatroom.rejoin_when_active true + #********************************************************************* # hazelcase configuration #*********************************************************************