本物联网系列
二、零基础用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
* &#