物联网 基于netty核心实战-握手与认证

物联网 基于netty核心实战-握手与认证

简述

MQTT PUBLISH 报文 的二进制结构(固定头、可变头、有效载荷),并基于 Netty 实现一个支持 QoS 0 的发布/订阅 Broker

源码(netty-sample-05-auth)

https://gitee.com/kcnf-iot/iot-sample/tree/master/netty/netty-sample-05

协议结构

MQTT 报文结构 Netty 中的对应 Java 类 关键字段/作用
固定报头 (Fixed Header) MqttFixedHeader 包含 messageType (如 CONNECT), qosLevel, isRetain 等标志位
可变报头 (Variable Header) MqttConnectVariableHeader 包含协议名、版本号、连接标志(如 Clean Session)、保活时间
有效载荷 (Payload) MqttConnectPayload 包含实际数据,如客户端ID、遗嘱消息、用户名和密码
完整连接请求 MqttConnectMessage 一个对象就包含了上述三部分,可以直接在处理器中使用

简易demo

服务端
复制代码
package com.jysemel.iot;

import com.jysemel.iot.handler.MqttAuthHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.mqtt.MqttDecoder;
import io.netty.handler.codec.mqtt.MqttEncoder;

public class MqttAuthServer {
    public static void main(String[] args) throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) {
                            ChannelPipeline p = ch.pipeline();
                            // (A) 使用 Netty 内置的 MqttDecoder
                            p.addLast("mqttDecoder", new MqttDecoder(1024));
                            // (B) 使用单例的 MqttEncoder
                            p.addLast("mqttEncoder", MqttEncoder.INSTANCE);
                            // (C) 实现我们自己的业务逻辑处理器
                            p.addLast("authHandler", new MqttAuthHandler());
                        }
                    });
            ChannelFuture f = b.bind(1883).sync();
            System.out.println("MQTT Auth Server started on port 1883");
            f.channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
客户端
复制代码
package com.jysemel.iot;

import com.jysemel.iot.handler.MqttAuthClientHandler;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.mqtt.MqttDecoder;
import io.netty.handler.codec.mqtt.MqttEncoder;

public class MqttAuthClient {
    public static void main(String[] args) throws Exception {
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) {
                            ChannelPipeline p = ch.pipeline();
                            p.addLast("mqttDecoder", new MqttDecoder(1024));
                            p.addLast("mqttEncoder", MqttEncoder.INSTANCE);
                            p.addLast("clientHandler", new MqttAuthClientHandler());
                        }
                    });
            ChannelFuture f = b.connect("127.0.0.1", 1883).sync();
            f.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully();
        }
    }
}
相关推荐
沙蒿同学5 小时前
不引 Ghostscript:我用纯 Go 给 PDF 做了一场「压缩手术」
前端·后端·github
缓慢更新6 小时前
审批的加签与会签:多人意见怎么合
android·java·数据库·低代码·rxjava
前进的李工7 小时前
飞书企业知识库设计+后端高频八股
数据库·redis·后端·mysql·性能优化·飞书·rag
yuniko-n7 小时前
【JUC】volatile 非原子性深入解析
java
sun_weitao7 小时前
极简版 MCP 工具实现方案
java·linux·服务器
fly-iot8 小时前
【下一个 AI 方向】:中小企业的数字化建设,物联网解决方案思考,使用开源项目快速搭建降低成本。
人工智能·物联网
金銀銅鐵8 小时前
[Java] 展示增删元素时 LinkedList 的内部变化
java·后端·ai编程
Binovate缤江数智9 小时前
电动自行车入户充电如何识别?数智开关与AI负荷识别的社区应用实践
人工智能·物联网
布吉岛的石头9 小时前
Java 程序员第 50 阶段3:数据工程:预训练语料清洗与去重
java·开发语言·人工智能·深度学习·transformer
大文说跨境9 小时前
防关联浏览器的指纹覆盖到什么程度:六类参数的实测与自检方法
java·指纹浏览器·tiktok运营