xin.alum.aim.groups.SessionGroups Maven / Gradle / Ivy
package xin.alum.aim.groups;
import io.netty.channel.Channel;
import org.springframework.stereotype.Component;
import xin.alum.aim.AIM;
import xin.alum.aim.model.Transportable;
import java.util.concurrent.ConcurrentHashMap;
/**
* 群组Sesions 管理通道
*
* @auther alum(alum @ live.cn)
* @date 2021/8/3 14:29
*/
@Component
public class SessionGroups extends ConcurrentHashMap {
/**
* 将用户连接绑定到群组
*
* @param ch 通道
* @param key 组键值
*/
public Sessions bind(Channel ch, String key) {
Sessions nSessions = get(key);
if (nSessions.add(ch)) {
super.putIfAbsent(key, nSessions);
}
return nSessions;
}
/**
* 解绑群组连接
*
* @param ch
* @param key
* @return
*/
public void unbind(Channel ch, String key) {
Sessions userSession = get(key);
userSession.remove(ch);
if (userSession.size() == 0) {
super.remove(key);
userSession.close();
}
}
/**
* 用户退出群组
*
* @param userId
* @return
*/
public Sessions exit(String userId, String key) {
Sessions users = get(Sessions.PREFIX_BIND_USER_GROUP.concat(userId));
Sessions group = get(key);
users.forEach(c -> {
group.remove(c);
});
return group;
}
/**
* 获取群组Sessions
*
* @param key
* @return
*/
public Sessions get(String key) {
return super.getOrDefault(key, new Sessions(key));
}
/**
* 向集群指定群组推送通知
*
* @param groupKey
* @param msg
*/
public void sends(String groupKey, Transportable msg) {
if (AIM.clusterFactory != null) {
AIM.clusterFactory.push(groupKey, msg);
} else {
get(groupKey).sends(msg);
}
}
}