| | |
| | | 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; |
| | |
| | | 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); |
| | | } |
| | |
| | | |
| | | 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()); |
| | | NettyConfig.getChannelGroup().add(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() |