package com.sjyt.icom.asst.context.handle.wsframe;

import com.sjyt.icom.asst.service.impl.ChannelPoolService;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 客户端发送断开请求处理
 *
 * @author renyanru
 * @version V1.0.0
 * @date 2022/6/1 16:04
 */
@Component
public class CloseWsFrameHandle extends WsFrameHandle {
    @Resource
    private ChannelPoolService channelPoolService;

    /**
     * 执行链
     *
     * @param event t
     */
    @Override
    public void doChain(WsFrameEvent event) {
        //CloseWebSocketFrame closeWebSocketFrame;
        WebSocketFrame frame = event.getFrame();
        ChannelHandlerContext ctx = event.getCtx();
        if (frame instanceof CloseWebSocketFrame) {
            // closeWebSocketFrame = (CloseWebSocketFrame) frame;
            channelPoolService.delUserChannel(ctx.channel());
            ctx.close();
            return;
        }
        handle.doChain(event);
    }
}
