代码调整

This commit is contained in:
2024-12-19 20:39:33 +08:00
parent 8835249c1b
commit d37d44123f
8 changed files with 33 additions and 21 deletions

View File

@@ -78,10 +78,7 @@ public class SocketSourceResponseService {
webSocketHandler.sendMsgToUser(param.getUserPageId(), "msg");
String s = param.getUserPageId() + "_Dev";
//开始设备通讯检测(发送设备初始化)
Channel channel = SocketManager.getChannelByUserId(s);
if(channel==null || !channel.isActive()){
NettyClient.socketClient(ip, port, param.getUserPageId(), new NettyDevClientHandler(param, socketDevResponseService));
}
List<PreDetection> devList = iPqDevService.getDevInfo(param.getDevIds());
Map<String, List<PreDetection>> map = new HashMap(1);
map.put("deviceList", devList);
@@ -90,7 +87,8 @@ public class SocketSourceResponseService {
socketMsg.setOperateCode(SourceOperateCodeEnum.DEV_INIT_GATHER_01.getValue());
socketMsg.setData(jsonString);
String json = JSON.toJSONString(socketMsg);
SocketManager.sendMsg(s,json);
NettyClient.socketClient(ip, port, param.getUserPageId(),json, new NettyDevClientHandler(param, socketDevResponseService));
// SocketManager.sendMsg(s,json);
break;
case UNPROCESSED_BUSINESS:
break;

View File

@@ -34,7 +34,7 @@ public enum SourceResponseCodeEnum {
NO_INIT_DEV(10556,"不存在上线的设备"),
//自定义前端展示消息
SOCKET_ERROR(20000,"连接服务端失败")
SOCKET_ERROR(20000,"服务端连接失败")

View File

@@ -2,10 +2,10 @@ package com.njcn.gather.detection.pojo.vo;
import com.njcn.gather.detection.pojo.enums.SourceResponseCodeEnum;
import lombok.Data;
import org.apache.poi.ss.formula.functions.T;
@Data
public class WebSocketVO {
public class WebSocketVO<T> {
private Integer code;

View File

@@ -101,14 +101,14 @@ public class PreDetectionServiceImpl implements PreDetectionService {
Channel channel = SocketManager.getChannelByUserId(s);
socketDevResponseService.xuClear();
socketDevResponseService.initList(param);
if(channel==null || !channel.isActive()){
NettyClient.socketClient(ip, port, param.getUserPageId(), new NettySourceClientHandler(param, sourceResponseService));
}
msg= new SocketMsg();
msg.setRequestId(SourceOperateCodeEnum.YJC_YTXJY.getValue());
msg.setOperateCode(SourceOperateCodeEnum.INIT_GATHER.getValue());
msg.setData(JSON.toJSONString(sourceParam));
SocketManager.sendMsg(s, JSON.toJSONString(msg));
NettyClient.socketClient(ip, port, param.getUserPageId(), JSON.toJSONString(msg),new NettySourceClientHandler(param, sourceResponseService));
} else {
throw new BusinessException(DetectionResponseEnum.SOURCE_INFO_NOT);
}
@@ -129,9 +129,9 @@ public class PreDetectionServiceImpl implements PreDetectionService {
String ddId = param.getUserPageId() + "_Source";
Channel channel = SocketManager.getChannelByUserId(ddId);
if (channel == null || !channel.isActive()) {
NettyClient.socketClient(ip, port, param.getUserPageId(), new NettySourceClientHandler(param, sourceResponseService));
NettyClient.socketClient(ip, port, param.getUserPageId(),"start\n", new NettySourceClientHandler(param, sourceResponseService));
}
SocketManager.sendMsg(ddId, "start\n");
}
};
runnable.run();

View File

@@ -39,7 +39,7 @@ import java.util.concurrent.TimeUnit;
@Getter
public class NettyClient {
public static void socketClient(String ip, Integer port, String userPageId, SimpleChannelInboundHandler<String> handler) {
public static void socketClient(String ip, Integer port, String userPageId,String msg, SimpleChannelInboundHandler<String> handler) {
NioEventLoopGroup group = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
try {
@@ -70,11 +70,13 @@ public class NettyClient {
}
});
bootstrap.connect(ip, port).sync().addListener((ChannelFutureListener) ch -> {
ChannelFuture channelFuture = bootstrap.connect(ip, port).sync();
channelFuture.addListener((ChannelFutureListener) ch -> {
if (!ch.isSuccess()) {
System.out.println("链接服务端失败...");
} else {
System.out.println("链接服务端成功...");
channelFuture.channel().writeAndFlush(msg);
}
});
} catch (Exception e) {

View File

@@ -3,6 +3,7 @@ package com.njcn.gather.detection.util.socket.cilent;
import com.njcn.gather.detection.handler.SocketDevResponseService;
import com.njcn.gather.detection.pojo.param.PreDetectionParam;
import com.njcn.gather.detection.util.socket.SocketManager;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.TimeoutException;
@@ -12,6 +13,7 @@ import org.springframework.beans.factory.annotation.Value;
import java.io.IOException;
import java.net.ConnectException;
import java.net.ProtocolException;
import java.util.Objects;
/**
@@ -40,7 +42,11 @@ public class NettyDevClientHandler extends SimpleChannelInboundHandler<String> {
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("客户端通道已建立" + ctx.channel().remoteAddress());
System.out.println("客户端通道已建立" + ctx.channel().id());
Channel channel = SocketManager.getChannelByUserId(param.getUserPageId()+"_Dev");
if(Objects.nonNull(channel)){
channel.close();
}
SocketManager.addUser(param.getUserPageId()+"_Dev",ctx.channel());
}

View File

@@ -6,6 +6,7 @@ import com.njcn.gather.detection.pojo.enums.SourceOperateCodeEnum;
import com.njcn.gather.detection.pojo.param.PreDetectionParam;
import com.njcn.gather.detection.pojo.vo.SocketMsg;
import com.njcn.gather.detection.util.socket.SocketManager;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleState;
@@ -16,6 +17,7 @@ import lombok.RequiredArgsConstructor;
import java.io.IOException;
import java.net.ConnectException;
import java.net.ProtocolException;
import java.util.Objects;
/**
@@ -36,9 +38,13 @@ public class NettySourceClientHandler extends SimpleChannelInboundHandler<Strin
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("客户端通道已建立" + ctx.channel().remoteAddress());
System.out.println("客户端通道已建立" + ctx.channel().id());
Channel channel = SocketManager.getChannelByUserId(webUser.getUserPageId()+"_Source");
if(Objects.nonNull(channel)){
channel.close();
}
SocketManager.addUser(webUser.getUserPageId()+"_Source",ctx.channel());
System.out.println("一存储");
}
/**
@@ -91,7 +97,7 @@ public class NettySourceClientHandler extends SimpleChannelInboundHandler<Strin
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
System.out.println("有通道接入" + ctx.channel());
System.out.println("有通道接入" + ctx.channel().id());
}

View File

@@ -24,7 +24,7 @@ public class NettyServerChannelInitializer extends ChannelInitializer<NioSocketC
// .addLast(new IdleStateHandler(10, 0, 0, TimeUnit.SECONDS))
.addLast(new StringDecoder(CharsetUtil.UTF_8))
.addLast(new StringEncoder(CharsetUtil.UTF_8))
.addLast(NettyServerHandler.INSTANCE);
.addLast(new NettyServerHandler());
}