WebSocket接收大数据时,分片帧的处理

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
import io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame;
import io.netty.handler.codec.http.websocketx.PongWebSocketFrame;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;


public class WebSocketHandlerDemo extends SimpleChannelInboundHandler<WebSocketFrame>{
    private static final Logger logger = LoggerFactory.getLogger(WebSocketHandlerDemo.class);
    
    StringBuilder frameBuffer = new StringBuilder();

    /**
     * 收到信息
     */
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame msg) throws Exception {
        
        WebSocketFrame frame = (WebSocketFrame) msg;
        logger.info("Received incoming frame [{}]", frame.getClass().getName());
        
        if (frame instanceof CloseWebSocketFrame) {
            logger.info("【PIS-client】 received CloseWebSocketFrame");
            if (frameBuffer != null) {
                handleMessageCompleted(ctx, frameBuffer.toString());
            }
            frameBuffer = null;
            return;
        } else if (frame instanceof PongWebSocketFrame) {
            logger.info("【PIS-client】 received pong");
            return;
        } else if (frame instanceof BinaryWebSocketFrame) {
            logger.info("【PIS-client】 received Binary");
            return;
        }

        // 判断、拼接数据帧给frameBuffer
        if (frame instanceof TextWebSocketFrame) {
            frameBuffer = new StringBuilder();
            TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;
            String text = textFrame.text();
            logger.info("【PIS-client】收到 TextWebSocketFrame message:{}", text);
            frameBuffer.append(text);
        } else if (frame instanceof ContinuationWebSocketFrame) {
            ContinuationWebSocketFrame continuationWebSocketFrame = (ContinuationWebSocketFrame) frame;
            String cwsText = continuationWebSocketFrame.text();
            logger.info("【PIS-client】 收到 ContinuationWebSocketFrame ,value:{}", cwsText);
            if (frameBuffer != null) {
                frameBuffer.append(cwsText);
            } else {
                logger.warn("【PIS-client】未接收初始帧的延续帧  .");
            }
        } else {
            logger.warn("【PIS-client】 收到不支持的帧: {}", frame.toString());
            return;
        }

        // 检查是否是最后一帧
        if (frame.isFinalFragment()) {
            handleMessageCompleted(ctx, frameBuffer.toString());
            frameBuffer = null;
        }
        
    }
    
    /**
     * 业务处理
     */
    protected void handleMessageCompleted(ChannelHandlerContext ctx, String msg) throws Exception {
        logger.info("收到消息:{}",msg);
    }
    
}

 

posted @ 2021-11-09 11:32  析草的残梦  阅读(1685)  评论(0编辑  收藏  举报