• 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);
        }
        
    }
  • 相关阅读:
    [BZOJ3413]匹配
    [BZOJ3879]SvT
    [BZOJ1339] [Baltic2008] Mafia / 黑手党
    [BZOJ1834] [ZJOI2010] network 网络扩容
    [BZOJ1449] [JSOI2009]球队收益 / [BZOJ2895] 球队预算
    [BZOJ2597] [WC2007]剪刀石头布
    [BZOJ1283]序列
    [BZOJ2127]happiness
    Java公开课-04.异常
    Java公开课-03.内部类
  • 原文地址:https://www.cnblogs.com/LinTianwen/p/15528017.html
Copyright © 2020-2023  润新知