【物联网项目系列】——使用netty框架做一个环保hj212协议即时通讯工具(接收解析处理实时数据)

本文介绍了一个物联网项目,利用Netty框架创建了一个环保HJ212协议的即时通讯工具。内容包括SpringBoot整合Netty,详细讲解了如何设置网络端口和服务,以及数据接收和解析处理。项目代码已上传至百度网盘,提供了使用方法和效果截图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本物联网系列

一、用netty做一个环保hj212协议即时通讯工具

二、零基础用uniapp快速开发实现MQTT设备中心附后台接口
三、MQTT服务器搭建实现物联网通讯
四、springboot + rabbitmq 做智能家居以及web显示未读消息

前言

统一回答下:
我的都是做项目的时候抽空自己写的博客,带有很多跳跃性,大家假如喜欢这个系列的话,我都有写。可以去看看上面相关链接


我只能提供一些部分的思路,刚开始做项目的时候资料比较欠缺,很理解大家的心情,功夫得自己摸索
其实有这些关键的部分是可以帮很多试手的同学做出点的东西的,还有因为比如大家问的就是数据库哪里下,jar怎么导入,那些问题,我没法回答哈哈哈

使用方法 与效果截图
在这里插入图片描述

本文代码已经上传打成jar上传到我的资源【已分享百度网盘】
链接: https://pan.baidu.com/s/1ebK6nVroqmp4_JWA1XCv5g 提取码: id8h 复制这段内容后打开百度网盘手机App,操作更方便哦
(https://download.youkuaiyun.com/download/weixin_44106334/13182987)
使用方法:解压后进入cmd java -jar shbykj-handle.jar


在这里插入图片描述

具体代码比较多,我就没贴了

springboot整合netty

pom

   <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
        </dependency>

yml文件


# netty配置
netty:
  # 端口号
  port: 6666
  # 最大线程数
  maxThreads: 1024
  # 数据包的最大长度
  max_frame_length: 65535

服务器(接收数据) :TCPServer

package com.shbykj.springboot.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
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.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.MessageToByteEncoder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 网络服务器
 *
 * @author
 * @date 2020-11-04 8:32
 */
@Component
public class TCPServer {
    @Resource
    private NettyServerConfig nettyConfig;

   /* @Resource
    private DivisorService divisorService;
    @Resource
    private PointInfoService pointInfoService;
    @Resource
    private PointAndDivisorService pointAndDivisorService;*/


    public void run() {
        int port = nettyConfig.getPort();
        EventLoopGroup boosGroup = new NioEventLoopGroup();
        EventLoopGroup workGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(boosGroup, workGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline pipeline = ch.pipeline();
//                            pipeline.addLast(new TCPHandler(divisorService,pointInfoService,pointAndDivisorService));
                            pipeline.addLast("encoder", new MessageToByteEncoder<byte[]>() {
                                @Override
                                protected void encode(ChannelHandlerContext ctx, byte[] msg, ByteBuf out) throws Exception {
                                    out.writeBytes(msg);
                                }
                            });
                            pipeline.addLast(new LengthFieldBasedFrameDecoder(nettyConfig.getMaxFrameLength()
                                    , 0, 2, 0, 2));
                            pipeline.addLast(new LengthFieldPrepender(2));
                        }
                    })
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_KEEPALIVE, true);
            System.out.println("服务启动...");
            ChannelFuture channelFuture = bootstrap.bind(port).sync();
            channelFuture.channel().closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            workGroup.shutdownGracefully();
            boosGroup.shutdownGracefully();
            System.out.println("服务关闭...");
        }
    }

}

客户端(解析数据处理):TCPHandler

package com.shbykj.springboot.netty;

import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.shbykj.pojo.*;
import com.shbykj.service.DivisorService;
import com.shbykj.service.PointAndDivisorService;
import com.shbykj.service.PointInfoService;
import com.shbykj.utils.DateUtils;
import com.shbykj.utils.config.CollectionMapping;
import com.shbykj.utils.core.T212Parser;
import com.shbykj.utils.db.MongoDBConnection;
import com.shbykj.utils.db.RedisConnection;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;
import io.netty.util.internal.StringUtil;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import java.io.StringReader;
import java.math.BigInteger;
import java.net.SocketAddress;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author
 * &#
评论 29
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值