修复Socket客户端的部分问题
This commit is contained in:
parent
1b48f9f9b2
commit
54016a0b2d
|
|
@ -115,9 +115,7 @@ public class SocketClient {
|
|||
*/
|
||||
private void connect(List<SocketDataItem> socketDataItemList) {
|
||||
for (SocketDataItem socketDataItem : socketDataItemList) {
|
||||
Thread thread = new Thread(() -> {
|
||||
connect(socketDataItem);
|
||||
});
|
||||
Thread thread = new Thread(() -> connect(socketDataItem));
|
||||
thread.start();
|
||||
}
|
||||
}
|
||||
|
|
@ -150,7 +148,7 @@ public class SocketClient {
|
|||
while (true) {
|
||||
try {
|
||||
InputStream inputStream = socket.getInputStream();
|
||||
byte[] bytes = new byte[1024];
|
||||
byte[] bytes = new byte[socketDataItem.getReceiveBytesLength()];
|
||||
int read = inputStream.read(bytes);
|
||||
socketDataItem.setLastReceiveMessageTime(LocalDateTime.now());
|
||||
if(socketClientEvent != null) {
|
||||
|
|
@ -160,9 +158,8 @@ public class SocketClient {
|
|||
}
|
||||
} catch (Exception e) {
|
||||
// 断开的结果
|
||||
socket = null;
|
||||
socketDataItem.setSocketStatus(SocketStatusEnum.DISCONNECTED);
|
||||
socketDataItem.setSocket(socket);
|
||||
socketDataItem.setSocket(null);
|
||||
if(socketClientEvent != null) {
|
||||
socketClientEvent.onDisconnect(socketDataItem); // 触发断开连接事件
|
||||
}
|
||||
|
|
@ -171,9 +168,8 @@ public class SocketClient {
|
|||
}
|
||||
} catch (IOException e) {
|
||||
// 连接失败的结果
|
||||
socket = null;
|
||||
socketDataItem.setSocketStatus(SocketStatusEnum.ERROR);
|
||||
socketDataItem.setSocket(socket);
|
||||
socketDataItem.setSocket(null);
|
||||
if(socketClientEvent != null) {
|
||||
socketClientEvent.onError(socketDataItem, e); // 触发连接失败事件
|
||||
}
|
||||
|
|
@ -254,23 +250,20 @@ public class SocketClient {
|
|||
socket.shutdownInput();
|
||||
socket.close();
|
||||
} catch (IOException ignored) {}
|
||||
socket = null;
|
||||
socketDataItem.setSocketStatus(SocketStatusEnum.DISCONNECTED);
|
||||
socketDataItem.setSocket(socket);
|
||||
socketDataItem.setSocket(null);
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
if(socket.isClosed() || !socket.isConnected()) {
|
||||
socket = null;
|
||||
socketDataItem.setSocketStatus(SocketStatusEnum.DISCONNECTED);
|
||||
socketDataItem.setSocket(socket);
|
||||
socketDataItem.setSocket(null);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} catch (Exception ignored) {
|
||||
socket = null;
|
||||
socketDataItem.setSocketStatus(SocketStatusEnum.DISCONNECTED);
|
||||
socketDataItem.setSocket(socket);
|
||||
socketDataItem.setSocket(null);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package org.wcs.plugin.tcp.model;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.*;
|
||||
import org.wcs.plugin.tcp.enums.SocketStatusEnum;
|
||||
|
||||
import java.net.Socket;
|
||||
|
|
@ -12,6 +11,8 @@ import java.time.LocalDateTime;
|
|||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
@Builder
|
||||
public class SocketDataItem {
|
||||
|
||||
/**
|
||||
|
|
@ -47,7 +48,14 @@ public class SocketDataItem {
|
|||
/**
|
||||
* 自动重连
|
||||
*/
|
||||
private boolean autoReconnect;
|
||||
@Builder.Default
|
||||
private boolean autoReconnect = true;
|
||||
|
||||
/**
|
||||
* 接收字节长度
|
||||
*/
|
||||
@Builder.Default
|
||||
private int receiveBytesLength = 1024;
|
||||
|
||||
/**
|
||||
* Socket 状态
|
||||
|
|
@ -64,101 +72,5 @@ public class SocketDataItem {
|
|||
*/
|
||||
private boolean userDisConnected;
|
||||
|
||||
/**
|
||||
* 构建开始
|
||||
* @return SocketDataItem
|
||||
*/
|
||||
public static SocketDataItem buildStart() {
|
||||
SocketDataItem socketDataItem = new SocketDataItem();
|
||||
socketDataItem.setSocketStatus(SocketStatusEnum.CREATE);
|
||||
socketDataItem.setAutoReconnect(true);
|
||||
socketDataItem.setUserDisConnected(false);
|
||||
return socketDataItem;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置SocketId
|
||||
* @param socketId SocketId
|
||||
* @return SocketDataItem
|
||||
*/
|
||||
public SocketDataItem setSocketId(String socketId) {
|
||||
this.socketId = socketId;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置Socket IP
|
||||
* @param ip Socket IP
|
||||
* @return SocketDataItem
|
||||
*/
|
||||
public SocketDataItem setIp(String ip) {
|
||||
this.socketIp = ip;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 端口
|
||||
* @param port 端口
|
||||
* @return SocketDataItem
|
||||
*/
|
||||
public SocketDataItem setPort(int port) {
|
||||
this.socketPort = port;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 标记
|
||||
* @param tag 标记
|
||||
* @return SocketDataItem
|
||||
*/
|
||||
public SocketDataItem setTag(String tag) {
|
||||
this.socketTag = tag;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 心跳包内容
|
||||
* @param heartBeatString 心跳包内容
|
||||
* @return SocketDataItem
|
||||
*/
|
||||
public SocketDataItem setHeartBeatString(String heartBeatString) {
|
||||
this.heartBeatString = heartBeatString;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动重连
|
||||
* @param autoReconnect 自动重连
|
||||
* @return SocketDataItem
|
||||
*/
|
||||
public SocketDataItem setAutoReconnect(boolean autoReconnect) {
|
||||
this.autoReconnect = autoReconnect;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 构建结束
|
||||
* @return SocketDataItem
|
||||
*/
|
||||
public SocketDataItem buildEnd() {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SocketDataItem{" +
|
||||
"socketId='" + socketId + '\'' +
|
||||
", socketTag='" + socketTag + '\'' +
|
||||
", socketIp='" + socketIp + '\'' +
|
||||
", socketPort=" + socketPort +
|
||||
", heartBeatString='" + heartBeatString + '\'' +
|
||||
", autoReconnect=" + autoReconnect +
|
||||
", socketStatus=" + socketStatus +
|
||||
'}';
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -127,13 +127,14 @@ public class EtagJob implements Job {
|
|||
});
|
||||
for (AppEtagControllerInfo etagControllerInfo : etagControllerInfoList) {
|
||||
|
||||
EtagCommon.socketClient.addSocketDataItem(SocketDataItem.buildStart()
|
||||
.setSocketId(etagControllerInfo.getControllerId().toString())
|
||||
.setIp(etagControllerInfo.getIp())
|
||||
.setPort(etagControllerInfo.getPort())
|
||||
.setTag(etagControllerInfo.getControllerName())
|
||||
.setHeartBeatString("")
|
||||
.buildEnd());
|
||||
EtagCommon.socketClient.addSocketDataItem(SocketDataItem.builder()
|
||||
.socketId(etagControllerInfo.getControllerId().toString())
|
||||
.socketTag(etagControllerInfo.getControllerName())
|
||||
.socketIp(etagControllerInfo.getIp())
|
||||
.socketPort(etagControllerInfo.getPort())
|
||||
.heartBeatString("")
|
||||
.build());
|
||||
|
||||
}
|
||||
EtagCommon.socketClient.connectAll();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user