You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.5 KiB

package com.bx.imserver.netty;
3 years ago
import io.netty.channel.ChannelHandlerContext;
import java.util.Map;
3 years ago
import java.util.concurrent.ConcurrentHashMap;
public class UserChannelCtxMap {
3 years ago
/*
* 维护userId和ctx的关联关系格式:Map<userId,map<terminalctx>>
*/
private static Map<Long, Map<Integer,ChannelHandlerContext>> channelMap = new ConcurrentHashMap();
3 years ago
public static void addChannelCtx(Long userId,Integer channel,ChannelHandlerContext ctx){
channelMap.computeIfAbsent(userId,key -> new ConcurrentHashMap()).put(channel,ctx);
3 years ago
}
public static void removeChannelCtx(Long userId,Integer terminal){
if(userId != null && terminal != null && channelMap.containsKey(userId)){
Map<Integer,ChannelHandlerContext> userChannelMap = channelMap.get(userId);
if(userChannelMap.containsKey(terminal)){
userChannelMap.remove(terminal);
}
}
3 years ago
}
public static ChannelHandlerContext getChannelCtx(Long userId,Integer terminal){
if(userId != null && terminal != null && channelMap.containsKey(userId)){
Map<Integer,ChannelHandlerContext> userChannelMap = channelMap.get(userId);
if(userChannelMap.containsKey(terminal)){
return userChannelMap.get(terminal);
}
}
return null;
}
public static Map<Integer,ChannelHandlerContext> getChannelCtx(Long userId){
if(userId == null){
return null;
}
3 years ago
return channelMap.get(userId);
}
}