//package com.ruoyi.dataInterchange;
|
//
|
//import com.ruoyi.dataInterchange.model.UPConnectRsp;
|
//import com.ruoyi.dataInterchange.model.enu.DataType;
|
//import com.ruoyi.dataInterchange.util.jtt809.decoder.Jtt809Decoder;
|
//import com.ruoyi.dataInterchange.util.jtt809.encoder.Jtt809Encoder;
|
//import com.ruoyi.dataInterchange.util.jtt809.packet.common.OuterPacket;
|
//import io.netty.bootstrap.Bootstrap;
|
//import io.netty.channel.*;
|
//import io.netty.channel.nio.NioEventLoopGroup;
|
//import io.netty.channel.socket.SocketChannel;
|
//import io.netty.channel.socket.nio.NioSocketChannel;
|
//import io.netty.handler.timeout.IdleStateHandler;
|
//import org.junit.jupiter.api.Test;
|
//import org.springframework.boot.test.context.SpringBootTest;
|
//
|
//import java.net.InetSocketAddress;
|
//import java.util.concurrent.TimeUnit;
|
//
|
//
|
//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = RuoYiDataInterchangeApplication.class)
|
//public class RuoYiOrderApplicationTests {
|
//
|
// @Test
|
// public void test() {
|
// EventLoopGroup nioEventLoopGroup = null;
|
// try {
|
// //创建Bootstrap对象用来引导启动客户端
|
// Bootstrap bootstrap = new Bootstrap();
|
// //创建EventLoopGroup对象并设置到Bootstrap中,EventLoopGroup可以理解为是一个线程池,这个线程池用来处理连接、接受数据、发送数据
|
// nioEventLoopGroup = new NioEventLoopGroup();
|
// //创建InetSocketAddress并设置到Bootstrap中,InetSocketAddress是指定连接的服务器地址
|
// bootstrap.group(nioEventLoopGroup).channel(NioSocketChannel.class).remoteAddress(new InetSocketAddress("127.0.0.1", 1000))
|
// .handler(new ChannelInitializer<SocketChannel>() {
|
// //添加一个ChannelHandler,客户端成功连接服务器后就会被执行
|
// @Override
|
// protected void initChannel(SocketChannel socketChannel)
|
// throws Exception {
|
// ChannelPipeline pipeline = socketChannel.pipeline();
|
// // 解码器
|
// pipeline.addLast("decoder", new Jtt809Decoder());
|
// // 编码器
|
// pipeline.addLast("encoder", new Jtt809Encoder());
|
// // 空闲检测处理器 触发空闲状态事件 读空闲:5秒 写空闲:7秒 读写空闲:10秒
|
// pipeline.addLast(new IdleStateHandler(5, 7, 3, TimeUnit.SECONDS));
|
// // 处理器
|
//// pipeline.addLast("handler", new NettyHandle());
|
// }
|
// });
|
// // • 调用Bootstrap.connect()来连接服务器
|
// ChannelFuture f = bootstrap.connect().sync();
|
// //将通道添加到缓存中,便于后期直接使用
|
// Channel channel = f.channel();
|
// // • 最后关闭EventLoopGroup来释放资源
|
// f.channel().closeFuture().sync();
|
//
|
//
|
// //解析封装原元数据
|
// UPConnectRsp upConnectRsp = new UPConnectRsp();
|
// upConnectRsp.setResult(0x00);
|
// // 随机一个校验码
|
// upConnectRsp.setVerifyCode(1234);
|
// //主链路登录应答
|
// byte[] body = upConnectRsp.encode();
|
// OuterPacket out = new OuterPacket(DataType.UP_CONNECT_RSP.getCode(), body);
|
// channel.writeAndFlush(out);
|
// channel.flush();
|
//
|
// } catch (Exception e) {
|
// e.printStackTrace();
|
// } finally {
|
// try {
|
// nioEventLoopGroup.shutdownGracefully().sync();
|
// } catch (InterruptedException e) {
|
// throw new RuntimeException(e);
|
// }
|
// }
|
// }
|
//}
|