代码调整
This commit is contained in:
@@ -78,10 +78,7 @@ public class SocketSourceResponseService {
|
|||||||
webSocketHandler.sendMsgToUser(param.getUserPageId(), "msg");
|
webSocketHandler.sendMsgToUser(param.getUserPageId(), "msg");
|
||||||
String s = param.getUserPageId() + "_Dev";
|
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());
|
List<PreDetection> devList = iPqDevService.getDevInfo(param.getDevIds());
|
||||||
Map<String, List<PreDetection>> map = new HashMap(1);
|
Map<String, List<PreDetection>> map = new HashMap(1);
|
||||||
map.put("deviceList", devList);
|
map.put("deviceList", devList);
|
||||||
@@ -90,7 +87,8 @@ public class SocketSourceResponseService {
|
|||||||
socketMsg.setOperateCode(SourceOperateCodeEnum.DEV_INIT_GATHER_01.getValue());
|
socketMsg.setOperateCode(SourceOperateCodeEnum.DEV_INIT_GATHER_01.getValue());
|
||||||
socketMsg.setData(jsonString);
|
socketMsg.setData(jsonString);
|
||||||
String json = JSON.toJSONString(socketMsg);
|
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;
|
break;
|
||||||
case UNPROCESSED_BUSINESS:
|
case UNPROCESSED_BUSINESS:
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ public enum SourceResponseCodeEnum {
|
|||||||
NO_INIT_DEV(10556,"不存在上线的设备"),
|
NO_INIT_DEV(10556,"不存在上线的设备"),
|
||||||
|
|
||||||
//自定义前端展示消息
|
//自定义前端展示消息
|
||||||
SOCKET_ERROR(20000,"连接服务端失败")
|
SOCKET_ERROR(20000,"服务端连接失败")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,10 +2,10 @@ package com.njcn.gather.detection.pojo.vo;
|
|||||||
|
|
||||||
import com.njcn.gather.detection.pojo.enums.SourceResponseCodeEnum;
|
import com.njcn.gather.detection.pojo.enums.SourceResponseCodeEnum;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.apache.poi.ss.formula.functions.T;
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class WebSocketVO {
|
public class WebSocketVO<T> {
|
||||||
|
|
||||||
private Integer code;
|
private Integer code;
|
||||||
|
|
||||||
|
|||||||
@@ -101,14 +101,14 @@ public class PreDetectionServiceImpl implements PreDetectionService {
|
|||||||
Channel channel = SocketManager.getChannelByUserId(s);
|
Channel channel = SocketManager.getChannelByUserId(s);
|
||||||
socketDevResponseService.xuClear();
|
socketDevResponseService.xuClear();
|
||||||
socketDevResponseService.initList(param);
|
socketDevResponseService.initList(param);
|
||||||
if(channel==null || !channel.isActive()){
|
|
||||||
NettyClient.socketClient(ip, port, param.getUserPageId(), new NettySourceClientHandler(param, sourceResponseService));
|
|
||||||
}
|
|
||||||
msg= new SocketMsg();
|
msg= new SocketMsg();
|
||||||
msg.setRequestId(SourceOperateCodeEnum.YJC_YTXJY.getValue());
|
msg.setRequestId(SourceOperateCodeEnum.YJC_YTXJY.getValue());
|
||||||
msg.setOperateCode(SourceOperateCodeEnum.INIT_GATHER.getValue());
|
msg.setOperateCode(SourceOperateCodeEnum.INIT_GATHER.getValue());
|
||||||
msg.setData(JSON.toJSONString(sourceParam));
|
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 {
|
} else {
|
||||||
throw new BusinessException(DetectionResponseEnum.SOURCE_INFO_NOT);
|
throw new BusinessException(DetectionResponseEnum.SOURCE_INFO_NOT);
|
||||||
}
|
}
|
||||||
@@ -129,9 +129,9 @@ public class PreDetectionServiceImpl implements PreDetectionService {
|
|||||||
String ddId = param.getUserPageId() + "_Source";
|
String ddId = param.getUserPageId() + "_Source";
|
||||||
Channel channel = SocketManager.getChannelByUserId(ddId);
|
Channel channel = SocketManager.getChannelByUserId(ddId);
|
||||||
if (channel == null || !channel.isActive()) {
|
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();
|
runnable.run();
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ import java.util.concurrent.TimeUnit;
|
|||||||
@Getter
|
@Getter
|
||||||
public class NettyClient {
|
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();
|
NioEventLoopGroup group = new NioEventLoopGroup();
|
||||||
Bootstrap bootstrap = new Bootstrap();
|
Bootstrap bootstrap = new Bootstrap();
|
||||||
try {
|
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()) {
|
if (!ch.isSuccess()) {
|
||||||
System.out.println("链接服务端失败...");
|
System.out.println("链接服务端失败...");
|
||||||
} else {
|
} else {
|
||||||
System.out.println("链接服务端成功...");
|
System.out.println("链接服务端成功...");
|
||||||
|
channelFuture.channel().writeAndFlush(msg);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.njcn.gather.detection.util.socket.cilent;
|
|||||||
import com.njcn.gather.detection.handler.SocketDevResponseService;
|
import com.njcn.gather.detection.handler.SocketDevResponseService;
|
||||||
import com.njcn.gather.detection.pojo.param.PreDetectionParam;
|
import com.njcn.gather.detection.pojo.param.PreDetectionParam;
|
||||||
import com.njcn.gather.detection.util.socket.SocketManager;
|
import com.njcn.gather.detection.util.socket.SocketManager;
|
||||||
|
import io.netty.channel.Channel;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
import io.netty.channel.SimpleChannelInboundHandler;
|
import io.netty.channel.SimpleChannelInboundHandler;
|
||||||
import io.netty.handler.timeout.TimeoutException;
|
import io.netty.handler.timeout.TimeoutException;
|
||||||
@@ -12,6 +13,7 @@ import org.springframework.beans.factory.annotation.Value;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.ConnectException;
|
import java.net.ConnectException;
|
||||||
import java.net.ProtocolException;
|
import java.net.ProtocolException;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -40,7 +42,11 @@ public class NettyDevClientHandler extends SimpleChannelInboundHandler<String> {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
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());
|
SocketManager.addUser(param.getUserPageId()+"_Dev",ctx.channel());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.param.PreDetectionParam;
|
||||||
import com.njcn.gather.detection.pojo.vo.SocketMsg;
|
import com.njcn.gather.detection.pojo.vo.SocketMsg;
|
||||||
import com.njcn.gather.detection.util.socket.SocketManager;
|
import com.njcn.gather.detection.util.socket.SocketManager;
|
||||||
|
import io.netty.channel.Channel;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
import io.netty.channel.SimpleChannelInboundHandler;
|
import io.netty.channel.SimpleChannelInboundHandler;
|
||||||
import io.netty.handler.timeout.IdleState;
|
import io.netty.handler.timeout.IdleState;
|
||||||
@@ -16,6 +17,7 @@ import lombok.RequiredArgsConstructor;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.ConnectException;
|
import java.net.ConnectException;
|
||||||
import java.net.ProtocolException;
|
import java.net.ProtocolException;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -36,9 +38,13 @@ public class NettySourceClientHandler extends SimpleChannelInboundHandler<Strin
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
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());
|
SocketManager.addUser(webUser.getUserPageId()+"_Source",ctx.channel());
|
||||||
|
System.out.println("一存储");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -91,7 +97,7 @@ public class NettySourceClientHandler extends SimpleChannelInboundHandler<Strin
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handlerAdded(ChannelHandlerContext ctx) {
|
public void handlerAdded(ChannelHandlerContext ctx) {
|
||||||
System.out.println("有通道接入" + ctx.channel());
|
System.out.println("有通道接入" + ctx.channel().id());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ public class NettyServerChannelInitializer extends ChannelInitializer<NioSocketC
|
|||||||
// .addLast(new IdleStateHandler(10, 0, 0, TimeUnit.SECONDS))
|
// .addLast(new IdleStateHandler(10, 0, 0, TimeUnit.SECONDS))
|
||||||
.addLast(new StringDecoder(CharsetUtil.UTF_8))
|
.addLast(new StringDecoder(CharsetUtil.UTF_8))
|
||||||
.addLast(new StringEncoder(CharsetUtil.UTF_8))
|
.addLast(new StringEncoder(CharsetUtil.UTF_8))
|
||||||
.addLast(NettyServerHandler.INSTANCE);
|
.addLast(new NettyServerHandler());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user