From 99c2f1d881c371f772d0a90a86058be835ee5b90 Mon Sep 17 00:00:00 2001 From: tangfudong <280620913@qq.com> Date: Mon, 20 Mar 2023 13:41:39 +0800 Subject: [PATCH] =?UTF-8?q?webocket=E9=80=9A=E8=AE=AF=E5=8D=8F=E8=AE=AE?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E7=AB=AF=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/WebSocketServerVerticle.java | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/iot-components/iot-websocket-component/src/main/java/cc/iotkit/comp/websocket/server/WebSocketServerVerticle.java b/iot-components/iot-websocket-component/src/main/java/cc/iotkit/comp/websocket/server/WebSocketServerVerticle.java index 830bb69e..ab463f89 100644 --- a/iot-components/iot-websocket-component/src/main/java/cc/iotkit/comp/websocket/server/WebSocketServerVerticle.java +++ b/iot-components/iot-websocket-component/src/main/java/cc/iotkit/comp/websocket/server/WebSocketServerVerticle.java @@ -12,6 +12,7 @@ import io.vertx.core.http.HttpServerOptions; import io.vertx.core.http.ServerWebSocket; import io.vertx.core.net.PemKeyCertOptions; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import java.util.HashMap; import java.util.Map; @@ -44,29 +45,34 @@ public class WebSocketServerVerticle extends AbstractDeviceVerticle { } httpServer = vertx.createHttpServer(options).webSocketHandler(wsClient -> { log.info("webSocket client connect sessionId:{},path={}", wsClient.textHandlerID(), wsClient.path()); - String deviceKey = wsClient.headers().get("deviceKey"); + String deviceKey = wsClient.path().replace("/",""); + if(StringUtils.isBlank(deviceKey)||deviceKey.split("_").length<2){ + wsClient.reject(); + log.warn("陌生连接,拒绝"); + return; + } + Map deviceKeyObj=new HashMap<>(); + deviceKeyObj.put("deviceKey",deviceKey); + executor.onReceive(new HashMap<>(), "auth", JsonUtil.toJsonString(deviceKeyObj), (r) -> { + if (r == null) { + //认证失败 + log.warn("认证失败,拒绝"); + wsClient.reject(); + return; + } + //保存设备与连接关系 + wsClients.put(getDeviceKey(r), wsClient); + }); wsClient.textMessageHandler(message -> { - executor.onReceive(new HashMap<>(), "auth", deviceKey, (r) -> { - if (r == null) { - //认证失败 - wsClient.reject(); - return; - } - //保存设备与连接关系 - wsClients.put(getDeviceKey(r), wsClient); - executor.onReceive(new HashMap<>(), "", message); - }); + executor.onReceive(new HashMap<>(), "", message); }); wsClient.closeHandler(c -> { log.warn("client connection closed,deviceKey:{}", deviceKey); - executor.onReceive(new HashMap<>(), "disconnect", deviceKey, (r) -> { + executor.onReceive(new HashMap<>(), "disconnect", JsonUtil.toJsonString(deviceKeyObj), (r) -> { //删除设备与连接关系 wsClients.remove(getDeviceKey(r)); }); }); - wsClient.endHandler(e -> { - log.warn("webSocket client connection end,deviceKey:{}", deviceKey); - }); wsClient.exceptionHandler(ex -> { log.warn("webSocket client connection exception,deviceKey:{}", deviceKey); }); @@ -82,7 +88,9 @@ public class WebSocketServerVerticle extends AbstractDeviceVerticle { @Override public void stop() throws Exception { for (String deviceKey : wsClients.keySet()) { - executor.onReceive(null, "disconnect", deviceKey); + Map deviceKeyObj=new HashMap<>(); + deviceKeyObj.put("deviceKey",deviceKey); + executor.onReceive(null, "disconnect", JsonUtil.toJsonString(deviceKeyObj)); } httpServer.close(voidAsyncResult -> log.info("close webocket server...")); }