洪城义警-正式版后台
zengh
2021-11-05 b39cc2beacc1ec2e37cff349cae42e420105c8a6
src/main/java/org/springblade/modules/webscoket/WebSocketHandler.java
@@ -1,5 +1,7 @@
package org.springblade.modules.webscoket;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
@@ -60,13 +62,8 @@
      System.out.println("客户端断开连接:" + ctx.channel());
      //用户离线状态
      String name = ChannelSupervise.findName(ctx.channel().id().asShortText());
      System.out.println(name);
      if ( name != null &&!name.equals("ping") ){
         String num="0";
         //工作状态(0闲置,1工作中)
         String workSt = "0";
         webSocketHandler.suserService.updateUser(num,name,workSt);
         //ChannelSupervise.removeChannel(ctx.channel());
         NettyConfig.getChannelGroup().remove(ctx.channel());
         removeUserId(ctx);
      }
@@ -100,21 +97,29 @@
      if (!request.equals("ping")){
         NettyConfig.getUserChannelMap().put(request,ctx.channel());
         JSONObject jsonObj = JSON.parseObject(request);
         String type = jsonObj.get("type").toString();
         //将用户id作为自定义属性加入到channel 中,方便随时channel中获取用户id
         AttributeKey<String> key = AttributeKey.valueOf("userId");
         ctx.channel().attr(key).setIfAbsent(request);
         if (type != null && type.equals("login")){
            //登录链接
            String id = jsonObj.get("id").toString();
            NettyConfig.getUserChannelMap().put(id,ctx.channel());
         //把用户信息添加到通道里
         ChannelSupervise.addChannel(ctx.channel(),request);
         //用户在线状态
         this.on=request;
         //在线状态(0掉线,1在线)
         String num="1";
         //工作状态(0闲置,1工作中)
         String workSt = "0";
         webSocketHandler.suserService.updateUser(num,request,workSt);
            System.out.println(jsonObj.get("type"));
            System.out.println(jsonObj.get("id"));
            //将用户id作为自定义属性加入到channel 中,方便随时channel中获取用户id
            AttributeKey<String> key = AttributeKey.valueOf("userId");
            ctx.channel().attr(key).setIfAbsent(id);
            //把用户信息添加到通道里
            ChannelSupervise.addChannel(ctx.channel(),id);
         }
      }
//      TextWebSocketFrame tws = new TextWebSocketFrame(new Date().toString()