• MQTT---HiveMQ源代码具体解释(八)Netty-WebSocket


    源博客地址:http://blog.csdn.net/pipinet123


    MQTT交流群:221405150


    基于netty实现Webscoket相对来说就是相当简单,所以本讲中就不搞太复杂的了,给大家看一个类图,和一段给pipeline加入handler,就能够全然了解hivemq怎样处理的了,事实上跟差点儿全部须要使用netty去支持websocket的处理一致。


    websocket

    
    public class WebsocketTransportChannelInitializer {
        public static final int MAX_CONTENT_LENGTH = 65536;
        private final WebsocketListener listener;
    
        public WebsocketTransportChannelInitializer(WebsocketListener listener) {
            this.listener = listener;
        }
    
        public void initChannel(Channel channel) {
    //加入http协议编码/解码
        channel.pipeline().addBefore(Pipelines.ALL_CHANNEL_GROUP_HANDLER, Pipelines.HTTP_SERVER_CODEC, new HttpServerCodec());
    //加入HttpRequest消息聚合器
            channel.pipeline().addAfter(Pipelines.HTTP_SERVER_CODEC, Pipelines.HTTP_OBJECT_AGGREGATOR, new HttpObjectAggregator(MAX_CONTENT_LENGTH));
        //加入WebSocketServerProtocolHandler
            String path = this.listener.getPath();
            String subProtocols = getSubProtocols();
            boolean allowExtensions = this.listener.getAllowExtensions();
    channel.pipeline().addAfter(Pipelines.HTTP_OBJECT_AGGREGATOR, Pipelines.WEBSOCKET_SERVER_PROTOCOL_HANDLER, new WebSocketServerProtocolHandler(path, subProtocols, allowExtensions, Integer.MAX_VALUE));
    //加入Websocket字节码Handler
    channel.pipeline().addAfter(Pipelines.WEBSOCKET_SERVER_PROTOCOL_HANDLER, Pipelines.WEBSOCKET_BINARY_FRAME_HANDLER, new WebsocketBinaryFrameHandler());
    //加入websocket帧类型handler
    channel.pipeline().addAfter(Pipelines.WEBSOCKET_BINARY_FRAME_HANDLER, Pipelines.WEBSOCKET_CONTINUATION_FRAME_HANDLER, new WebsocketContinuationFrameHandler());
    //加入websocket文本handler
    channel.pipeline().addAfter(Pipelines.WEBSOCKET_BINARY_FRAME_HANDLER, Pipelines.WEBSOCKET_TEXT_FRAME_HANDLER, new WebsocketTextFrameHandler());
    //加入mqtt message转换成websocket
    channel.pipeline().addAfter(Pipelines.WEBSOCKET_TEXT_FRAME_HANDLER, Pipelines.MQTT_WEBSOCKET_ENCODER, new MqttWebsocketEncoder());
        }
    
        @VisibleForTesting
        String getSubProtocols() {
            return Joiner.on(",").join(this.listener.getSubprotocols());
        }
    }
  • 相关阅读:
    stm32 SPI DMA读取ADS8345数据
    Minigui3.0.12完美安装,折腾了一天。终于看到了
    qvfb2的安装,在ubuntu10.4上安装成功
    户口从杭州人才市场迁移到武汉万科魅力之城的过程
    禁止minigui 3.0的屏幕保护
    想穿越回到儿时记录那些幸福
    TIM2定时闪灯程序。。。
    关于minigui的皮肤控件无法显示问题
    插件框架内核的设计
    用“序列图”描述技术方案
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/8455849.html
Copyright © 2020-2023  润新知