优化聊天室逻辑,添加聊天室空闲退出时间和发消息重新加入配置

This commit is contained in:
heavyrain.lee
2020-01-05 12:36:36 +08:00
parent e4da3780f5
commit 7c4fe831b3
6 changed files with 60 additions and 6 deletions
+6
View File
@@ -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
#*********************************************************************
@@ -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() {
}
}
@@ -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();
@@ -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<WFCMessage.UserSettingEntry> getUserSetting(String userId, int scope);
@@ -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;
}
@@ -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
#*********************************************************************