From 97f4845952c1e42804ecca481d0cf109c6369c1f Mon Sep 17 00:00:00 2001 From: puzhibing <393733352@qq.com> Date: 星期五, 07 四月 2023 17:39:48 +0800 Subject: [PATCH] 修改配置和长连接bug --- zuul/src/main/java/com/sinata/zuul/util/echo/NettyServerController.java | 62 +++++++++++------------------- 1 files changed, 23 insertions(+), 39 deletions(-) diff --git a/zuul/src/main/java/com/sinata/zuul/util/echo/NettyServerController.java b/zuul/src/main/java/com/sinata/zuul/util/echo/NettyServerController.java index 1ebf342..b82f217 100644 --- a/zuul/src/main/java/com/sinata/zuul/util/echo/NettyServerController.java +++ b/zuul/src/main/java/com/sinata/zuul/util/echo/NettyServerController.java @@ -106,72 +106,56 @@ if(StringUtil.isNotEmpty(token)){ String token_ = redisUtil.getValue("USER_APP_"+ userId1);//获取缓存中最新的数据 if(StringUtil.isNotEmpty(token_) && !token.equals(token_)){//不在同一设备上登录,向其他设备发送数据 - JSONObject msg_ = new JSONObject(); + ChannelHandlerContext data_ = NettyChannelMap.getData_(token_.substring(0, 23)); + JSONObject msg_ = new JSONObject(); msg_.put("code", 200); msg_.put("msg", "SUCCESS"); msg_.put("method", "OFFLINE"); msg_.put("data", new Object()); - this.sendMsgToClient(ctx, msg_.toJSONString());//给当前通道发送消息 - TimerTask timerTask = new TimerTask() { + this.sendMsgToClient(data_, msg_.toJSONString());//给当前通道发送消息 + new Timer().schedule(new TimerTask() { @Override public void run() { - NettyChannelMap.remove_(ctx); - NettyChannelMap.remove(ctx); + NettyChannelMap.remove_(data_); } - }; - Timer timer = new Timer(); - timer.schedule(timerTask, 3000); - timer.cancel(); - }else{ - NettyChannelMap.update_(token.substring(0, 23), ctx); - NettyChannelMap.update("USER" + userId1, ctx); - String s = NettyMsg.setMsg(Method.ok, new HashMap<String, Object>()); - ctx.writeAndFlush(Unpooled.copiedBuffer((s).getBytes())); - } - if(StringUtil.isEmpty(token_)){//确保登录的时候存储token失败的情况 - redisUtil.setStrValue("USER_APP_" + userId1, token); + }, 5000); } + NettyChannelMap.update_(token.substring(0, 23), ctx); + NettyChannelMap.update("USER" + userId1, ctx); + redisUtil.setStrValue("USER_APP_" + userId1, token); } + //存储通讯通道 + if(null != ctx && ctx.channel().isActive()){ + NettyChannelMap.update("USER" + userId1, ctx); + } }else{ //确保账号在单个设备上登录 - String value = redisUtil.getValue("DEVICE_" + userId1); - if(StringUtil.isNotEmpty(token) && StringUtil.isEmpty(device) && StringUtil.isEmpty(value)){//APP端登录的操作 + if(StringUtil.isNotEmpty(token)){//APP端登录的操作 String token_ = redisUtil.getValue("DRIVER_" + userId1);//缓存中拿最新数据 if(StringUtil.isNotEmpty(token_) && !token.equals(token_)){//不在同一设备上登录,向当前设备发送数据 - JSONObject msg_ = new JSONObject(); + ChannelHandlerContext data_ = NettyChannelMap.getData_(token_.substring(0, 23)); + JSONObject msg_ = new JSONObject(); msg_.put("code", 200); msg_.put("msg", "SUCCESS"); msg_.put("method", "OFFLINE"); msg_.put("data", new Object()); - this.sendMsgToClient(ctx, msg_.toJSONString());//给当前通道发送消息 - TimerTask timerTask = new TimerTask() { + this.sendMsgToClient(data_, msg_.toJSONString());//给当前通道发送消息 + new Timer().schedule(new TimerTask() { @Override public void run() { - NettyChannelMap.remove_(ctx); - NettyChannelMap.remove(ctx); + NettyChannelMap.remove_(data_); } - }; - Timer timer = new Timer(); - timer.schedule(timerTask, 3000); - timer.cancel(); - }else{ - NettyChannelMap.update("DRIVER" + userId1, ctx); - NettyChannelMap.update_(token.substring(0, 23), ctx); - String s = NettyMsg.setMsg(Method.ok, new HashMap<String, Object>()); - ctx.writeAndFlush(Unpooled.copiedBuffer((s).getBytes())); - } - if(StringUtil.isEmpty(token_)){//确保登录的时候存储token失败的情况 - redisUtil.setStrValue("DRIVER_" + userId1, token); + }, 5000); } + NettyChannelMap.update("DRIVER" + userId1, ctx); + NettyChannelMap.update_(token.substring(0, 23), ctx); + redisUtil.setStrValue("DRIVER_" + userId1, token); } - //存储通讯通道 if(null != ctx && ctx.channel().isActive()){ NettyChannelMap.update("DRIVER" + userId1, ctx); - String s = NettyMsg.setMsg(Method.ok, new HashMap<String, Object>()); - ctx.writeAndFlush(Unpooled.copiedBuffer((s).getBytes())); } } } -- Gitblit v1.7.1