| | |
| | | import io.netty.handler.codec.http.websocketx.WebSocketFrame; |
| | | import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker; |
| | | import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; |
| | | import io.netty.util.AttributeKey; |
| | | import io.netty.util.CharsetUtil; |
| | | import java.util.Date; |
| | | |
| | | import org.springblade.jfpt.nettyServer.NettyConfig; |
| | | import org.springblade.jfpt.suser.service.ISuserService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | |
| | | 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()); |
| | | |
| | | NettyConfig.getChannelGroup().remove(ctx.channel()); |
| | | removeUserId(ctx); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | |
| | | } |
| | | // 返回应答消息 |
| | | 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); |
| | | } |
| | | //用户离线状态 |
| | | 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()); |
| | | // 返回【谁发的发给谁】 |
| | | |
| | | ctx.channel().writeAndFlush(tws); |
| | | if (!request.equals("ping")){ |
| | | |
| | | NettyConfig.getUserChannelMap().put(request,ctx.channel()); |
| | | |
| | | //将用户id作为自定义属性加入到channel 中,方便随时channel中获取用户id |
| | | AttributeKey<String> key = AttributeKey.valueOf("userId"); |
| | | ctx.channel().attr(key).setIfAbsent(request); |
| | | |
| | | //把用户信息添加到通道里 |
| | | ChannelSupervise.addChannel(ctx.channel(),request); |
| | | //用户在线状态 |
| | | this.on=request; |
| | | //在线状态(0掉线,1在线) |
| | | String num="1"; |
| | | //工作状态(0闲置,1工作中) |
| | | String workSt = "0"; |
| | | webSocketHandler.suserService.updateUser(num,request,workSt); |
| | | } |
| | | |
| | | // TextWebSocketFrame tws = new TextWebSocketFrame(new Date().toString() |
| | | // + ctx.channel().id() + ":" + ctx.channel()); |
| | | // // 返回【谁发的发给谁】 |
| | | // |
| | | // ctx.channel().writeAndFlush(tws); |
| | | // 群发 |
| | | // ChannelSupervise.send2All(tws); |
| | | } |
| | |
| | | ctx.close(); |
| | | } |
| | | |
| | | /** |
| | | * 删除用户与channel 对应关系 |
| | | * @param ctx |
| | | */ |
| | | private void removeUserId(ChannelHandlerContext ctx){ |
| | | AttributeKey<String> key = AttributeKey.valueOf("userId"); |
| | | String userId = ctx.channel().attr(key).get(); |
| | | NettyConfig.getUserChannelMap().remove(userId); |
| | | } |
| | | |
| | | } |