南昌市物联网技防平台-后台
Administrator
2021-04-07 eb3440d7c0c7baef5e6fc888fd0076b1787643d9
blade-service/blade-jfpts/src/main/java/org/springblade/jfpt/webscoket/WebSocketHandler.java
@@ -62,7 +62,15 @@
   public void channelInactive(ChannelHandlerContext ctx) throws Exception {
      //断开连接
      System.out.println("客户端断开连接:" + ctx.channel());
      ChannelSupervise.removeChannel(ctx.channel());
      //用户离线状态
      String name = ChannelSupervise.findName(ctx.channel().id().asShortText());
      if ( name != null &&!name.equals("ping") ){
         String num="0";
         //工作状态(0闲置,1工作中)
         String workSt = "0";
         webSocketHandler.suserService.updateUser(num,name,workSt);
         ChannelSupervise.removeChannel(ctx.channel());
      }
   }
   @Override
@@ -90,26 +98,19 @@
      }
      // 返回应答消息
      String request = ((TextWebSocketFrame) frame).text();
      String substring1 = request.substring(0, 2);
      //用户在线状态
      if(substring1.equals("登录")){
         String  online="1";
         String substring = request.substring(0, request.length());
         System.out.println(substring);
         String[] split = substring.split(",");//以逗号分割
         this.on=split[1];
         webSocketHandler.suserService.updateUser(online,this.on);
      if (!request.equals("ping")){
         //把用户信息添加到通道里
         ChannelSupervise.addChannel(ctx.channel(),request);
         //用户在线状态
         this.on=request;
         //在线状态(0掉线,1在线)
         String num="1";
         //工作状态(0闲置,1工作中)
         String workSt = "0";
         webSocketHandler.suserService.updateUser(num,request,workSt);
      }
      //用户离线状态
      else if (substring1.equals("退出")){
         String  online="0";
         String substring = request.substring(0, request.length());
         System.out.println(substring);
         String[] split = substring.split(",");//以逗号分割
         this.on=split[1];
         webSocketHandler.suserService.updateUser(online,this.on);
         webSocketHandler.channelInactive(ctx);
      }
      TextWebSocketFrame tws = new TextWebSocketFrame(new Date().toString()
         + ctx.channel().id() + ":" + ctx.channel());
      // 返回【谁发的发给谁】