智慧保安后台管理-外网-验收版本
tangzy
2021-12-06 cc057177b2fb17aee9a173a6adbabdc578fd74c7
src/main/java/org/springblade/modules/webscoket/WebSocketHandler.java
@@ -15,7 +15,6 @@
import io.netty.util.AttributeKey;
import io.netty.util.CharsetUtil;
import org.springblade.modules.nettyServer.NettyConfig;
import org.springblade.modules.suser.service.ISuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -26,10 +25,7 @@
   private WebSocketServerHandshaker handshaker;
   private  String on=null;
   @Autowired
   private  ISuserService suserService;
   private String on = null;
   private static WebSocketHandler webSocketHandler;
@@ -63,7 +59,7 @@
      //用户离线状态
      String name = ChannelSupervise.findName(ctx.channel().id().asShortText());
      System.out.println(name);
      if ( name != null &&!name.equals("ping") ){
      if (name != null && !name.equals("ping")) {
         NettyConfig.getChannelGroup().remove(ctx.channel());
         removeUserId(ctx);
      }
@@ -95,15 +91,15 @@
      // 返回应答消息
      String request = ((TextWebSocketFrame) frame).text();
      if (!request.equals("ping")){
      if (!request.equals("ping")) {
         JSONObject jsonObj = JSON.parseObject(request);
         String type = jsonObj.get("type").toString();
         if (type != null && type.equals("login")){
         if (type != null && type.equals("login")) {
            //登录链接
            String id = jsonObj.get("id").toString();
            NettyConfig.getUserChannelMap().put(id,ctx.channel());
            NettyConfig.getUserChannelMap().put(id, ctx.channel());
            System.out.println(jsonObj.get("type"));
            System.out.println(jsonObj.get("id"));
@@ -112,14 +108,8 @@
            AttributeKey<String> key = AttributeKey.valueOf("userId");
            ctx.channel().attr(key).setIfAbsent(id);
            //把用户信息添加到通道里
            ChannelSupervise.addChannel(ctx.channel(),id);
            ChannelSupervise.addChannel(ctx.channel(), id);
         }
      }
//      TextWebSocketFrame tws = new TextWebSocketFrame(new Date().toString()
@@ -146,7 +136,7 @@
      }
      //握手
      WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
         "ws://localhost:9034/websocket", null, false);
         "ws://localhost:2086/websocket", null, false);
      handshaker = wsFactory.newHandshaker(req);
      if (handshaker == null) {
         WebSocketServerHandshakerFactory
@@ -174,6 +164,7 @@
//         f.addListener(ChannelFutureListener.CLOSE);
//      }
   }
   @Override
   public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
      cause.printStackTrace();
@@ -182,9 +173,10 @@
   /**
    * 删除用户与channel 对应关系
    *
    * @param ctx
    */
   private void removeUserId(ChannelHandlerContext ctx){
   private void removeUserId(ChannelHandlerContext ctx) {
      AttributeKey<String> key = AttributeKey.valueOf("userId");
      String userId = ctx.channel().attr(key).get();
      NettyConfig.getUserChannelMap().remove(userId);