| | |
| | | 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())); |
| | | } |
| | | } |
| | | } |