|
@@ -2,8 +2,12 @@ package com.persagy.socket;
|
|
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
|
|
+import com.persagy.cameractl.conf.AllStaticConfig;
|
|
|
+
|
|
|
import io.netty.bootstrap.Bootstrap;
|
|
|
+import io.netty.channel.Channel;
|
|
|
import io.netty.channel.ChannelFuture;
|
|
|
+import io.netty.channel.ChannelFutureListener;
|
|
|
import io.netty.channel.ChannelInitializer;
|
|
|
import io.netty.channel.EventLoopGroup;
|
|
|
import io.netty.channel.nio.NioEventLoopGroup;
|
|
@@ -23,53 +27,86 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
*/
|
|
|
@Slf4j
|
|
|
public class IotSocketClient {
|
|
|
+
|
|
|
+ public static final EventLoopGroup GROUP = new NioEventLoopGroup(1);
|
|
|
+
|
|
|
+ public static final Bootstrap BS = new Bootstrap();
|
|
|
|
|
|
- public static boolean connectAndSend(String host, int port, String command) {
|
|
|
- EventLoopGroup group = new NioEventLoopGroup(1);
|
|
|
- Bootstrap bs = new Bootstrap();
|
|
|
+ public static Channel channel = null;
|
|
|
+
|
|
|
+ static {
|
|
|
// 注册线程池、使用NioSocketChannel来作为连接用的channel类
|
|
|
- bs.group(group).channel(NioSocketChannel.class);
|
|
|
-
|
|
|
- boolean result = false;
|
|
|
- ChannelFuture cf = null;
|
|
|
+ BS.group(GROUP).channel(NioSocketChannel.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 连接
|
|
|
+ *
|
|
|
+ * @date 2022年8月17日 下午12:00:49
|
|
|
+ */
|
|
|
+ public static void connect() {
|
|
|
try {
|
|
|
- bs.remoteAddress(new InetSocketAddress(host, port)) // 绑定连接端口和host信息
|
|
|
+ BS.remoteAddress(new InetSocketAddress(AllStaticConfig.toupIp, AllStaticConfig.toupPort)) // 绑定连接端口和host信息
|
|
|
.handler(new ChannelInitializer<SocketChannel>() { // 绑定连接初始化器
|
|
|
@Override
|
|
|
protected void initChannel(SocketChannel ch) throws Exception {
|
|
|
log.info("正在连接中...");
|
|
|
ch.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8));
|
|
|
ch.pipeline().addLast(new StringDecoder(CharsetUtil.UTF_8));
|
|
|
- ch.pipeline().addLast(new IotClientHandler(command));
|
|
|
+ ch.pipeline().addLast(new IotClientHandler());
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- cf = bs.connect().sync(); // 异步连接服务器
|
|
|
+ ChannelFuture cf = BS.connect().sync(); // 异步连接服务器
|
|
|
+ channel = cf.channel();
|
|
|
log.info("服务端连接成功...");
|
|
|
-
|
|
|
- result = true;
|
|
|
} catch (Exception e) {
|
|
|
log.error("与服务端建立连接失败", e);
|
|
|
} finally {
|
|
|
- if (cf != null) {
|
|
|
- try {
|
|
|
- cf.channel().closeFuture().sync();
|
|
|
- log.info("连接已关闭..");
|
|
|
- } catch (Exception e2) {
|
|
|
- log.error("未知异常", e2);
|
|
|
- }
|
|
|
- }
|
|
|
- if (group != null) {
|
|
|
- group.shutdownGracefully();
|
|
|
- log.info("线程池已关闭..");
|
|
|
- }
|
|
|
+
|
|
|
}
|
|
|
-
|
|
|
- return result;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 断线重连
|
|
|
+ *
|
|
|
+ * @date 2022年8月16日 下午6:28:24
|
|
|
+ */
|
|
|
+ public static void reConnect() {
|
|
|
+ // 加入断线后自动重连监听器
|
|
|
+ channel = BS.connect(AllStaticConfig.toupIp, AllStaticConfig.toupPort).addListener(new ChannelFutureListener() {
|
|
|
+ @Override
|
|
|
+ public void operationComplete(ChannelFuture future) throws Exception {
|
|
|
+ if (future.cause() != null) {
|
|
|
+ log.info("Failed to connect: {}", future.cause());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }).channel();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 消息发送
|
|
|
+ * @param message
|
|
|
+ * @throws Exception
|
|
|
+ * @date 2022年8月16日 下午6:28:18
|
|
|
+ */
|
|
|
+ public static void sendMessage(String message) throws Exception {
|
|
|
+ if (channel.isActive() && channel.isWritable()) {
|
|
|
+ log.info("发送数据至服务端: [{}]", message);
|
|
|
+
|
|
|
+ try {
|
|
|
+ channel.writeAndFlush(message);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("发送数据异常", e);
|
|
|
+ channel.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
- IotSocketClient.connectAndSend("192.168.100.102", 30054, "(5001120003;1;report;20220617100000;17107;ZD1060;1;902;155)");
|
|
|
+ // 192.168.100.102 30054 (5001120003;1;report;20220717100000;17107;ZD1060;1;902;152)
|
|
|
+ IotSocketClient.connect();
|
|
|
+ IotSocketClient.sendMessage("(5001120003;1;report;20220717100000;17107;ZD1060;1;902;152)");
|
|
|
}
|
|
|
|
|
|
}
|