修改ws上下文对应的key规则,修改无tableId购物车缓存策略
This commit is contained in:
@@ -88,13 +88,14 @@ public class PushToAppChannelHandlerAdapter extends NettyChannelHandlerAdapter {
|
||||
// 遍历webSocketMap,查找并移除对应的ChannelHandlerContext
|
||||
String key = ctxToUserIdMap.get(ctx);
|
||||
if (StringUtils.isNotBlank(key)) {
|
||||
String[] split = key.split(":");
|
||||
String[] split = key.split("-");
|
||||
ConcurrentHashMap<String, ChannelHandlerContext> tableMap = webSocketMap.get(split[0]);
|
||||
if (tableMap != null && !tableMap.isEmpty() && tableMap.size() > 0) {
|
||||
tableMap.remove(split[1]);
|
||||
if (tableMap.isEmpty() || tableMap.size() == 0) {
|
||||
webSocketMap.remove(split[0]);
|
||||
redisUtils.deleteByKey(RedisCst.TABLE_CART.concat(split[0]));
|
||||
// 删除购物车缓存
|
||||
redisUtils.deleteByKey(split[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -133,12 +134,13 @@ public class PushToAppChannelHandlerAdapter extends NettyChannelHandlerAdapter {
|
||||
channelInactive(ctx);
|
||||
return;
|
||||
}
|
||||
String key = tableId + "-" + shopId;
|
||||
|
||||
String tableCartKey = RedisCst.getTableCartKey(shopId, tableId, Integer.valueOf(userId));
|
||||
log.info("netty连接 接收到数据 建立连接参数 param:{}",jsonObject);
|
||||
this.tableId=tableId;
|
||||
this.shopId=shopId;
|
||||
if (webSocketMap.containsKey(key)) {
|
||||
ConcurrentHashMap<String, ChannelHandlerContext> userSocketMap = webSocketMap.get(key);
|
||||
if (webSocketMap.containsKey(tableCartKey)) {
|
||||
ConcurrentHashMap<String, ChannelHandlerContext> userSocketMap = webSocketMap.get(tableCartKey);
|
||||
ChannelHandlerContext channelHandlerContext = userSocketMap.get(userId);
|
||||
if (channelHandlerContext != null) {
|
||||
channelHandlerContext.close();
|
||||
@@ -147,9 +149,9 @@ public class PushToAppChannelHandlerAdapter extends NettyChannelHandlerAdapter {
|
||||
} else {
|
||||
ConcurrentHashMap<String, ChannelHandlerContext> userSocketMap = new ConcurrentHashMap<>();
|
||||
userSocketMap.put(userId, ctx);
|
||||
webSocketMap.put(key,userSocketMap);
|
||||
webSocketMap.put(tableCartKey,userSocketMap);
|
||||
}
|
||||
ctxToUserIdMap.put(ctx, key + ":" + userId);
|
||||
ctxToUserIdMap.put(ctx, tableCartKey + "-" + userId);
|
||||
JSONObject jsonObject1 = new JSONObject();
|
||||
jsonObject1.put("status", "success");
|
||||
jsonObject1.put("msg", "连接成功");
|
||||
@@ -163,11 +165,13 @@ public class PushToAppChannelHandlerAdapter extends NettyChannelHandlerAdapter {
|
||||
log.info("netty连接 接收到接口数据:meg:{}",msg);
|
||||
jsonObject.put("tableId", this.tableId);
|
||||
jsonObject.put("shopId", this.shopId);
|
||||
Integer userId = jsonObject.getInteger("userId");
|
||||
if("sku".equals(type)){
|
||||
boolean exist = redisUtils.exists(RedisCst.TABLE_CART.concat(jsonObject.getString("tableId").concat("-").concat(shopId)));
|
||||
String tableCartKey = RedisCst.getTableCartKey(shopId, tableId, userId);
|
||||
boolean exist = redisUtils.exists(tableCartKey);
|
||||
Integer num = 0;
|
||||
if (exist){
|
||||
String message = redisUtils.getMessage(RedisCst.TABLE_CART.concat(jsonObject.getString("tableId").concat("-").concat(shopId)));
|
||||
String message = redisUtils.getMessage(tableCartKey);
|
||||
JSONArray array = JSON.parseArray(message);
|
||||
for (int i = 0; i < array.size(); i++) {
|
||||
JSONObject object = array.getJSONObject(i);
|
||||
@@ -226,11 +230,11 @@ public class PushToAppChannelHandlerAdapter extends NettyChannelHandlerAdapter {
|
||||
}
|
||||
|
||||
@Async
|
||||
public void AppSendInfo(String message, String tableId,String userId, boolean userFlag) {
|
||||
log.info("netty连接 发送消息 tableId:{} userId:{} userFlag:{} message:{}",tableId,userId,userFlag, JSONUtil.toJSONString(message));
|
||||
public void AppSendInfo(String message, String redisKey,String userId, boolean userFlag) {
|
||||
log.info("netty连接 发送消息 tableId:{} userId:{} userFlag:{} message:{}",redisKey,userId,userFlag, JSONUtil.toJSONString(message));
|
||||
if (userFlag) {
|
||||
if (webSocketMap.containsKey(tableId)) {
|
||||
ConcurrentHashMap<String, ChannelHandlerContext> webSockets = webSocketMap.get(tableId);
|
||||
if (webSocketMap.containsKey(redisKey)) {
|
||||
ConcurrentHashMap<String, ChannelHandlerContext> webSockets = webSocketMap.get(redisKey);
|
||||
if(!webSockets.isEmpty()){
|
||||
if (StringUtils.isNotBlank(userId)) {
|
||||
ChannelHandlerContext ctx = webSockets.get(userId);
|
||||
@@ -241,15 +245,15 @@ public class PushToAppChannelHandlerAdapter extends NettyChannelHandlerAdapter {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (StringUtils.isEmpty(tableId)) {
|
||||
if (StringUtils.isEmpty(redisKey)) {
|
||||
// 向所有用户发送信息
|
||||
for (ConcurrentHashMap<String, ChannelHandlerContext> value : webSocketMap.values()) {
|
||||
for (ChannelHandlerContext ctx : value.values()) {
|
||||
sendMesToApp(message,ctx);
|
||||
}
|
||||
}
|
||||
} else if (webSocketMap.containsKey(tableId)) {
|
||||
ConcurrentHashMap<String, ChannelHandlerContext> webSockets = webSocketMap.get(tableId);
|
||||
} else if (webSocketMap.containsKey(redisKey)) {
|
||||
ConcurrentHashMap<String, ChannelHandlerContext> webSockets = webSocketMap.get(redisKey);
|
||||
if(!webSockets.isEmpty()) {
|
||||
for (String user : webSockets.keySet()) {
|
||||
ChannelHandlerContext ctx = webSockets.get(user);
|
||||
@@ -261,10 +265,10 @@ public class PushToAppChannelHandlerAdapter extends NettyChannelHandlerAdapter {
|
||||
}
|
||||
}
|
||||
}else {
|
||||
log.info("netty连接 发送消息 桌码群发 tableId:{} 失败",tableId);
|
||||
log.info("netty连接 发送消息 桌码群发 tableId:{} 失败",redisKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user