当前位置: 首页 > news >正文

物联网 基于netty构建mqtt服务udp支持

  • 物联网 基于netty构建mqtt服务udp支持
    • 简述
    • 源码
    • 代码

物联网 基于netty构建mqtt服务udp支持

简述

应用中分别启动TCP和UDP两个服务,实现协议的分离与消息互通

源码

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

代码

package com.jysemel.iot; import io.netty.bootstrap.Bootstrap; 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.NioDatagramChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.codec.mqtt.MqttDecoder; import io.netty.handler.codec.mqtt.MqttEncoder; public class DualProtocolServer { public static void main(String[] args) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); EventLoopGroup udpGroup = new NioEventLoopGroup(); // UDP工作组 try { // --- 1. MQTT over TCP 服务 (端口1883) --- ServerBootstrap tcpBootstrap = new ServerBootstrap(); tcpBootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) { ChannelPipeline p = ch.pipeline(); p.addLast(new MqttDecoder(64 * 1024)); p.addLast(MqttEncoder.INSTANCE); p.addLast("mqttHandler", new MqttServerHandler()); // MQTT业务逻辑 } }); ChannelFuture tcpFuture = tcpBootstrap.bind(1883).sync(); System.out.println("[TCP] MQTT Broker 启动, 端口 1883"); // --- 2. UDP 服务 (端口8888) 接收自定义传感器数据 --- Bootstrap udpBootstrap = new Bootstrap(); udpBootstrap.group(udpGroup) .channel(NioDatagramChannel.class) .option(ChannelOption.SO_BROADCAST, true) .handler(new ChannelInitializer<NioDatagramChannel>() { @Override protected void initChannel(NioDatagramChannel ch) { ch.pipeline().addLast("udpHandler", new UdpServerHandler()); } }); ChannelFuture udpFuture = udpBootstrap.bind(8888).sync(); System.out.println("[UDP] 自定义传感器服务启动, 端口 8888"); tcpFuture.channel().closeFuture().sync(); udpFuture.channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); udpGroup.shutdownGracefully(); } } }
package com.jysemel.iot; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.socket.DatagramPacket; import java.nio.charset.StandardCharsets; public class UdpServerHandler extends SimpleChannelInboundHandler<DatagramPacket> { @Override protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) { ByteBuf content = packet.content(); String udpData = content.toString(StandardCharsets.UTF_8); System.out.println("[UDP] Received: " + udpData); // 将UDP数据转换为MQTT消息,并广播给所有MQTT客户端 // 这里固定发布到 "udp/sensor" 主题 MqttServerHandler.broadcast("udp/sensor", udpData); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { System.out.println("UDP error: " + cause.getMessage()); } }
http://www.jsqmd.com/news/844375/

相关文章:

  • AzurLaneLive2DExtract:碧蓝航线Live2D资源提取的完整指南
  • 为什么你的QQ音乐加密文件只能在QQ音乐播放?5分钟解密全攻略
  • AI专著生成新玩法!AI写专著工具,快速产出20万字专业专著!
  • 英雄联盟免费开源录像编辑工具:League Director完整使用指南
  • C 读取RAW文件程序
  • WarcraftHelper:魔兽争霸3终极兼容性解决方案与性能优化完全指南
  • comfyui一次成功的抽卡
  • 1 还在为百度网盘离线下载繁琐操作烦恼?试试这个Python神器!
  • 蓬松去屑控油洗发水榜单:高级丰盈洗发水推荐 - 速递信息
  • LVGL 8.3.0 版本 QT 仿真工程
  • 大学生必考证书有哪些?全方位职业规划与考证指南 - GrowthUME
  • 别再只会wrk -t -c -d了!用Lua脚本玩转复杂API压力测试(附实战脚本)
  • Cadence新手必看:用Ultra Librarian下载OrCAD/Allegro封装,5分钟搞定原理图和PCB库
  • 如何5分钟解放QQ音乐加密文件:qmc-decoder终极解密指南
  • 别让路径坑了你:手把手解决Adams与MATLAB/Simulink联仿时‘Adams model file does not exist‘报错
  • 2026年工程排烟窗厂家推荐:任丘市越禾安金属制品有限公司,铝合金天幕/弧形排烟窗/导水槽天窗专业供应 - 品牌推荐官
  • ARM NEON指令集:SIMD并行计算与浮点优化指南
  • OpenClaw 全能智能体保姆级部署教程|重塑桌面办公体验
  • 雷电模拟器安卓7.0抓包保姆级教程:从Charles证书安装到ProxyDroid配置,一步一图搞定
  • Keyboard Chatter Blocker:彻底解决机械键盘连击问题的终极免费方案 [特殊字符]
  • LabVIEW PC开发全攻略:从环境搭建到应用部署
  • 2025年网盘下载新范式:开源直链助手的技术演进与生态价值
  • 【免费下载】 轻松搞定Vivado与Matlab关联配置:一站式解决方案【matlab下载】
  • 空客320机械知识|51 STRUCTURE 飞机结构
  • 【亲测免费】 Altium Designer 10完美精减版:轻松上手,高效设计
  • 重点关注:哪些问题是口腔门诊装修要重点关注的呢?
  • 硬件工程师笔记:从MIPI M-PHY到UniPro,拆解UFS 4.0高速接口的设计与调试要点
  • 别再死记硬背了!用Python可视化带你彻底搞懂sin函数的泰勒展开
  • 学校RFID图书漂流柜源头生产厂家推荐 - 聚澜智能
  • 2026年贵阳、大方、惠水百货批发与地摊货源怎么选?思洪多元深度对比指南 - 企业名录优选推荐