# update 权限相关
This commit is contained in:
@@ -10,6 +10,7 @@ import com.maibu.netty.NettyClient;
|
||||
import com.maibu.utils.CommandUtils;
|
||||
import com.maibu.utils.ControlUtils;
|
||||
import com.maibu.utils.json.JsonUtils;
|
||||
import com.maibu.utils.spring.SpringUtils;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -41,7 +42,7 @@ public class WebsocketHandler extends WebSocketServer {
|
||||
// 空闲超时时间(10秒):超过10秒无消息则判定为断开
|
||||
private static final long IDLE_TIMEOUT = 10 * 1000;
|
||||
// 定时检测线程池(每隔10秒检测一次空闲连接)
|
||||
private final ScheduledExecutorService idleCheckExecutor = Executors.newSingleThreadScheduledExecutor();
|
||||
private ScheduledExecutorService executor = SpringUtils.getBean("scheduledExecutorService");
|
||||
|
||||
|
||||
public WebsocketHandler(int port) {
|
||||
@@ -81,24 +82,29 @@ public class WebsocketHandler extends WebSocketServer {
|
||||
return;
|
||||
}
|
||||
//todo 先关闭原先的
|
||||
WebSocket oldWebsocket = MiddleGlobalMemory.onlineSockets.get(key);
|
||||
NettyClient oldNettyClient = MiddleGlobalMemory.nettyClientMap.get(key);
|
||||
if (oldWebsocket != null && oldWebsocket.isOpen()) {
|
||||
oldWebsocket.close();
|
||||
}
|
||||
if (oldNettyClient != null && oldNettyClient.isConnected()) {
|
||||
oldNettyClient.disconnect();
|
||||
}
|
||||
// WebSocket oldWebsocket = MiddleGlobalMemory.onlineSockets.get(key);
|
||||
NettyClient client = MiddleGlobalMemory.nettyClientMap.get(key);
|
||||
|
||||
// if (oldWebsocket != null && oldWebsocket.isOpen()) {
|
||||
// oldWebsocket.close();
|
||||
// }
|
||||
// if (oldNettyClient != null && oldNettyClient.isConnected()) {
|
||||
// oldNettyClient.disconnect();
|
||||
// }
|
||||
MiddleGlobalMemory.onlineSockets.put(key, conn);
|
||||
//这里可以根据需要初始化Netty客户端
|
||||
NettyClient client = new NettyClient();
|
||||
if (client == null) {
|
||||
client = new NettyClient();
|
||||
}
|
||||
client.key = key;
|
||||
client.requestDeviceId = deviceId;
|
||||
client.token = token;
|
||||
client.userName = userName;
|
||||
client.initClient();
|
||||
client.initConnect(MiddleConstant.vertxIp, 9001);
|
||||
MiddleGlobalMemory.nettyClientMap.put(key, client);
|
||||
if (!client.isConnected()) {
|
||||
client.initClient();
|
||||
client.initConnect(MiddleConstant.vertxIp, 9001);
|
||||
}
|
||||
} else if (MesType.path.equals(type)) {
|
||||
//todo下发路径 预留
|
||||
pathDistribute();
|
||||
@@ -126,7 +132,7 @@ public class WebsocketHandler extends WebSocketServer {
|
||||
// else if(MesType.heartbeat.equals(type)){
|
||||
// clientActiveTimeMap.put(conn,System.currentTimeMillis());
|
||||
// }
|
||||
else if(MesType.switchPermission.equals(type)){
|
||||
else if (MesType.switchPermission.equals(type)) {
|
||||
DeviceRequestDTO requestDTO = new DeviceRequestDTO();
|
||||
requestDTO.setRequest(CommandRequestType.switch_control);
|
||||
requestDTO.setToken(token);
|
||||
@@ -136,12 +142,12 @@ public class WebsocketHandler extends WebSocketServer {
|
||||
logger.debug("Netty客户端:{}发送操控请求数据: {}", userName, JsonUtils.toJsonString(requestDTO));
|
||||
NettyClient nettyClient = MiddleGlobalMemory.nettyClientMap.get(key);
|
||||
ByteBuf buf = Unpooled.buffer();
|
||||
CommandUtils.buildCommand(buf,requestDTO, CommandConstant.interaction);
|
||||
CommandUtils.buildCommand(buf, requestDTO, CommandConstant.interaction);
|
||||
if (nettyClient != null && nettyClient.isConnected()) {
|
||||
// if (!nettyClient.isConnected()) nettyClient.connect();
|
||||
nettyClient.sendData(buf.array());
|
||||
}
|
||||
} else if(MesType.switchResult.equals(type)){
|
||||
} else if (MesType.switchResult.equals(type)) {
|
||||
DeviceRequestDTO requestDTO = new DeviceRequestDTO();
|
||||
requestDTO.setToken(token);
|
||||
requestDTO.setPlatform("web");
|
||||
@@ -154,7 +160,7 @@ public class WebsocketHandler extends WebSocketServer {
|
||||
logger.debug("Netty客户端:{}发送切换操控权请求反馈: {}", userName, JsonUtils.toJsonString(requestDTO));
|
||||
NettyClient nettyClient = MiddleGlobalMemory.nettyClientMap.get(key);
|
||||
ByteBuf buf = Unpooled.buffer();
|
||||
CommandUtils.buildCommand(buf,requestDTO, CommandConstant.interaction);
|
||||
CommandUtils.buildCommand(buf, requestDTO, CommandConstant.interaction);
|
||||
if (nettyClient != null && nettyClient.isConnected()) {
|
||||
// if (!nettyClient.isConnected()) nettyClient.connect();
|
||||
nettyClient.sendData(buf.array());
|
||||
@@ -225,7 +231,7 @@ public class WebsocketHandler extends WebSocketServer {
|
||||
|
||||
// 核心:定时检测空闲连接(每隔10秒执行一次)
|
||||
private void startIdleCheckTask() {
|
||||
idleCheckExecutor.scheduleAtFixedRate(() -> {
|
||||
executor.scheduleAtFixedRate(() -> {
|
||||
long now = System.currentTimeMillis();
|
||||
// 遍历所有连接,检测是否超时
|
||||
for (Map.Entry<WebSocket, Long> entry : clientActiveTimeMap.entrySet()) {
|
||||
|
||||
Reference in New Issue
Block a user