Pu Zhibing
2025-03-11 f56262ee1cb3c554878984e3536ab55a298bdedf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//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);
//            }
//        }
//    }
//}