zoukankan      html  css  js  c++  java
  • 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);
        }
        
    }
  • 相关阅读:
    菜单无限极分类核心代码
    获取页面中更新删除传过来的id
    CI循环数组问题
    ci框架model中的进行增删改的写法
    MySQL DBA的修炼与未来(参考篇)
    Linux学习笔记(13)linux软件安装rpm与yum--理论篇
    Linux学习笔记(12)linux文件目录与用户管理
    Linux学习笔记(11)linux网络管理与配置之一——配置路由与默认网关,双网卡绑定(5-6)
    Linux学习笔记(10)linux网络管理与配置之一——主机名与IP地址,DNS解析与本地hosts解析(1-4)
    自定义流水号,前置加0
  • 原文地址:https://www.cnblogs.com/LinTianwen/p/15528017.html
Copyright © 2011-2022 走看看