聊天服务器-解密生人(11)群组管理和群组聊天

聊天服务器-解密陌生人(11)群组管理和群组聊天

提示: 因为工程稍微有点大对我个人来说,所以可能在某些方面讲的不清楚或逻辑性不够强,如果有问题请@我。

原工程:https://github.com/LineChen/

 

八、群组管理

客户端可以发起多人聊天,周围一公里用户可以收到邀请,同意加入就可以进入多人聊天。一个用户默认情况下一天只能创建一个群组,群组从创建时计算24小时后自动解散。

下面是一个群组的相关信息:包括创建者、创建时间、群组Id、群组名称、主题、图标

public class GroupInfo {

public String groupHeadPath;

public String creator;//创建者

public String groupId;

public String groupName;

public String groupTopic;

public long createTime;//创建时间

public Map<String, String> membersMap;//(Id , Id)

 

public GroupInfo(){

this.membersMap = new HashMap<String, String>();

}

 

public GroupInfo(String groupHeadPath, String creator, String groupId,

String groupName, String groupTopic) {

this.groupHeadPath = groupHeadPath;

this.creator = creator;

this.groupId = groupId;

this.groupName = groupName;

this.groupTopic = groupTopic;

this.membersMap = new HashMap<String, String>();

}

public void joinGroup(String userId){

membersMap.put(userIduserId);

}

public void deleteMember(String userId){

membersMap.remove(userId);

}

public String getMember(String userId){

return membersMap.get(userId).toString();

}

}

 

创建群组:和注册用户差不多的操作

/**

 * 创建多人聊天

 * 

 * @param session

 * @param moMoMsg

 */

public void handleCreateGroup(IoSession session, iMoMoMsg moMoMsg) {

JSONObject json = JSON.parseObject(moMoMsg.msgJson);

String userId = json.getString(MsgKeys.userId);

String groupName = json.getString(MsgKeys.groupName);

String groupTopic = json.getString(MsgKeys.groupTopic);

String province = json.getString(MsgKeys.loc_province);

SqlModel model = new SqlModel();

 

GroupInfo gInfo = new GroupInfo();

gInfo.creator = model.getUserName(userIdfalse);

gInfo.createTime = System.currentTimeMillis();

gInfo.groupId = "group_" + userId + "_" + gInfo.createTime;

gInfo.groupName = groupName;

gInfo.groupTopic = groupTopic;

String groupIconPath = StaticValues.HEAD_P_PATH + gInfo.groupId

".png";

FileTools.getInstance().saveMultyFile(groupIconPathmoMoMsg.msgBytes);

 

// 返回是否创建成功消息

if (ManageGroups.addGroup(gInfo.groupIdgInfo)) {

model.UpdateVitality(userId, -1);

ManageGroups.getGroup(gInfo.groupId).joinGroup(userId);// 把创建用户添加到群组

if(!ManageGroups.isHaveGroup){

ManageGroups.keepWatching();//开启守护线程

}

iMoMoMsg moMsg = new iMoMoMsg();

moMsg.symbol = '-';

JSONObject jsonSend = new JSONObject();

jsonSend.put(MsgKeys.msgType, iMoMoMsgTypes.CREATE_GROUP_SUCCESS);

jsonSend.put(MsgKeys.groupIdgInfo.groupId);

jsonSend.put(MsgKeys.groupNamegInfo.groupName);

jsonSend.put(MsgKeys.groupTopicgInfo.groupTopic);

moMsg.msgBytes = FileTools.getInstance().getMultyFileBytes(

groupIconPath);

moMsg.msgJson = jsonSend.toJSONString();

session.write(moMsg);

 

if (ManageLocMap.isContainsProvince(province)) {

// 当前省有在线用户

ManageAProvinceLoc aProvinceLoc = ManageLocMap

.getAProvinceLoc(province);

if (aProvinceLoc.getCount() > 1) {

List<StrangerBean> list = aProvinceLoc.getDisStrangers(

trueuserId, 0);

if (list.size() > 0) {

System.out.println("周围的人 : ");

iMoMoMsg inviteMsg = new iMoMoMsg();

inviteMsg.symbol = '-';

JSONObject inviteJson = new JSONObject();

inviteJson.put(MsgKeys.msgType,

iMoMoMsgTypes.INVITE_TO_GROUP);

inviteJson.put(MsgKeys.groupCreatorgInfo.creator);

inviteJson.put(MsgKeys.groupIdgInfo.groupId);

inviteJson.put(MsgKeys.groupNamegInfo.groupName);

inviteJson.put(MsgKeys.groupTopicgInfo.groupTopic);

inviteMsg.msgBytes = FileTools.getInstance()

.getMultyFileBytes(groupIconPath);

inviteMsg.msgJson = inviteJson.toJSONString();

for (StrangerBean bean : list) {

System.out.println("Id = " + bean.strangerId);

IoSession ivSession = ManageClientSession

.getSession(bean.strangerId);

ivSession.write(inviteMsg);

}

}

}

}

else {

iMoMoMsg moMsg = new iMoMoMsg();

moMsg.symbol = '+';

JSONObject jsonSend = new JSONObject();

jsonSend.put(MsgKeys.msgType, iMoMoMsgTypes.CREATE_GROUP_FAILED);

moMsg.msgJson = jsonSend.toJSONString();

session.write(moMsg);

}

}

 

管理所有群组:新增、删除、判断有没有过期

 

public class ManageGroups {

public static Map<String, GroupInfo> groupMap = new HashMap<String, GroupInfo>();

 

public static boolean isHaveGroup = false;

static long FIVE_MINUTES = 300000;//五分钟

static long ONE_DAY = 86400000;//一天

public static GroupInfo getGroup(String groupId) {

if (isContainsGroupId(groupId)) {

//System.out.println("getGroupMembers--groupId::" + groupId);

return (GroupInfo) (groupMap.get(groupId));

}

return null;

}

 

public static boolean addGroup(String groupId,GroupInfo gInfo) {

//System.out.println("添加Group--" + groupId);

gInfo.membersMap = new HashMap<String, String>();

groupMap.put(groupIdgInfo);

return true;

}

 

public static void deleteGroup(String groupId) {

//System.out.println("删除Group--" + groupId);

if (isContainsGroupId(groupId)) {

groupMap.remove(groupId);

}

}

public static boolean isContainsGroupId(String groupId) {

return groupMap.containsKey(groupId);

}

/**

 * 开启守护进程,查看群组期限

 */

public static void keepWatching(){

isHaveGroup = true;

new Thread(new Runnable() {

@Override

public void run() {

try {

while(isHaveGroup){

Thread.sleep(FIVE_MINUTES);//每五分钟查看一次

Iterator<GroupInfo> iter = groupMap.values().iterator();

while(iter.hasNext()){

GroupInfo group = (GroupInfo)iter.next();

if(System.currentTimeMillis() - group.createTime > ONE_DAY){

deleteGroup(group.groupId);

}

}

}

catch (Exception e) {

e.printStackTrace();

}

}

}).start();

}

}

 

最后,群组聊天和单人聊天处理差不了太多,就是向该群组所有的用户转发消息即可,客户端处理就是把群组当成一个好友即可。

到此,服务器端的就差不多结束了。