From d5b3e5a413bcfccba294793ee093722f31b2448a Mon Sep 17 00:00:00 2001 From: puzhibing <393733352@qq.com> Date: 星期三, 16 八月 2023 17:02:13 +0800 Subject: [PATCH] 添加推单日志 --- zuul/src/main/java/com/sinata/zuul/util/echo/NettyServerController.java | 99 ++++++++++++++++++++++--------------------------- 1 files changed, 44 insertions(+), 55 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..9ad48bd 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 @@ -7,6 +7,7 @@ import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelId; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; @@ -56,7 +57,7 @@ * @param msg * @author TaoNingBo */ - public void JudgeOperation(ChannelHandlerContext ctx, Object msg) { + public synchronized void JudgeOperation(ChannelHandlerContext ctx, Object msg) { try { // ByteBuf转String ByteBuf byteBuf = (ByteBuf) msg; @@ -86,11 +87,6 @@ } JSONObject jsonCon = JSONObject.parseObject(jsonMsg.get("data").toString()); - if(null != ctx && ctx.channel().isActive()){ - jsonMsg.put("method", Method.pong); - sendMsgToClient(ctx, jsonMsg.toJSONString()); - } - //心跳 if(method.equals(Method.ping)) { Integer type = jsonCon.getInteger("type"); @@ -106,75 +102,65 @@ 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(token_.length() - 16)); + 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() { + boolean b = this.sendMsgToClient(data_, msg_.toJSONString());//给当前通道发送消息 + if(b){ + NettyChannelMap.remove_(data_); + } + 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(token.length() - 16), 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(); - 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() { - @Override - public void run() { - NettyChannelMap.remove_(ctx); - NettyChannelMap.remove(ctx); - } - }; - 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); + ChannelHandlerContext data_ = NettyChannelMap.getData_(token_.substring(token_.length() - 16)); + if(null != data_){ + JSONObject msg_ = new JSONObject(); + msg_.put("code", 200); + msg_.put("msg", "SUCCESS"); + msg_.put("method", "OFFLINE"); + msg_.put("data", new Object()); + boolean b = this.sendMsgToClient(data_, msg_.toJSONString());//给当前通道发送消息 + if(b){ + NettyChannelMap.remove_(data_); + } + } } + NettyChannelMap.update("DRIVER" + userId1, ctx); + NettyChannelMap.update_(token.substring(token.length() - 16), 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())); } } } + + if(null != ctx && ctx.channel().isActive()){ + jsonMsg.put("method", Method.pong); + sendMsgToClient(ctx, jsonMsg.toJSONString()); + } } //司机上传位置 if(method.equals(Method.location)){ @@ -228,7 +214,7 @@ * @param msg * @author TaoNingBo */ - public static void sendMsgToClient(ChannelHandlerContext ctx, String msg) { + public static boolean sendMsgToClient(ChannelHandlerContext ctx, String msg) { if (ctx != null && ctx.channel().isActive()) { ByteBuf buffer = Unpooled.copiedBuffer((msg).getBytes()); ChannelFuture sync; @@ -248,7 +234,9 @@ if(b){ NettyChannelMap.remove(ctx); } + return true; } + return sync.isSuccess(); } catch (Exception e) { System.err.println("推送发生异常,记录:"+msg); NettyChannelMap.remove(ctx); @@ -260,6 +248,7 @@ System.err.println("推送失败,长连接不存在"); NettyChannelMap.remove(ctx); } + return false; } // **链接断开 将推送消息记录 -- Gitblit v1.7.1