diff --git a/broker/config/wildfirechat.conf b/broker/config/wildfirechat.conf index ea508281..23fa122b 100755 --- a/broker/config/wildfirechat.conf +++ b/broker/config/wildfirechat.conf @@ -123,6 +123,9 @@ chatroom.participant_idle_time 900000 ## 用户向聊天室发送消息,自动加入聊天室 chatroom.rejoin_when_active true +## 当一个聊天室不存在时,如果有用户加入就自动创建 +chatroom.create_when_not_exist true + ## 是否允许客户端发送操作群的自定义群通知消息 group.allow_client_custom_operation_notification true diff --git a/broker/src/main/java/io/moquette/BrokerConstants.java b/broker/src/main/java/io/moquette/BrokerConstants.java index 94dff847..d02a7390 100755 --- a/broker/src/main/java/io/moquette/BrokerConstants.java +++ b/broker/src/main/java/io/moquette/BrokerConstants.java @@ -152,6 +152,8 @@ 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 GROUP_Allow_Client_Custom_Operation_Notification = "group.allow_client_custom_operation_notification"; public static final String GROUP_Visible_Quit_Kickoff_Notification = "group.visible_quit_or_kickoff_notification"; diff --git a/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java b/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java index a203c45b..86609e53 100755 --- a/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java +++ b/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java @@ -137,6 +137,7 @@ public class MemoryMessagesStore implements IMessagesStore { private long mChatroomParticipantIdleTime = 900000; private boolean mChatroomRejoinWhenActive = true; + private boolean mChatroomCreateWhenNotExist = true; private List mForbiddenClientSendTypes = new ArrayList<>(); private List mBlackListExceptionTypes = new ArrayList<>(); @@ -245,6 +246,14 @@ public class MemoryMessagesStore implements IMessagesStore { printMissConfigLog(CHATROOM_Rejoin_When_Active, mChatroomRejoinWhenActive + ""); } + try { + mChatroomCreateWhenNotExist = Boolean.parseBoolean(m_Server.getConfig().getProperty(BrokerConstants.CHATROOM_Create_When_Not_Exist)); + } catch (Exception e) { + e.printStackTrace(); + Utility.printExecption(LOG, e); + printMissConfigLog(CHATROOM_Create_When_Not_Exist, mChatroomCreateWhenNotExist + ""); + } + try { mChatroomParticipantIdleTime = Long.parseLong(m_Server.getConfig().getProperty(CHATROOM_Participant_Idle_Time, "900000")); } catch (Exception e) { @@ -3061,7 +3070,12 @@ public class MemoryMessagesStore implements IMessagesStore { public ErrorCode handleJoinChatroom(String userId, String clientId, String chatroomId) { IMap chatroomInfoMap = m_Server.getHazelcastInstance().getMap(CHATROOMS); if (chatroomInfoMap == null || chatroomInfoMap.get(chatroomId) == null) { - return ErrorCode.ERROR_CODE_NOT_EXIST; + if(mChatroomCreateWhenNotExist) { + WFCMessage.ChatroomInfo.Builder builder = WFCMessage.ChatroomInfo.newBuilder().setTitle(chatroomId); + createChatroom(chatroomId, builder.build()); + } else { + return ErrorCode.ERROR_CODE_NOT_EXIST; + } } m_Server.getStore().sessionsStore().getSession(clientId).refreshLastChatroomActiveTime(); diff --git a/distribution/src/main/resources/wildfirechat.conf b/distribution/src/main/resources/wildfirechat.conf index ce8c814c..d1d7a94d 100755 --- a/distribution/src/main/resources/wildfirechat.conf +++ b/distribution/src/main/resources/wildfirechat.conf @@ -117,6 +117,9 @@ chatroom.participant_idle_time 900000 ## 用户向聊天室发送消息,自动加入聊天室 chatroom.rejoin_when_active true +## 当一个聊天室不存在时,如果有用户加入就自动创建 +chatroom.create_when_not_exist true + ## 是否允许客户端发送操作群的自定义群通知消息 group.allow_client_custom_operation_notification true