소스 검색

websocket解析代码提交

jingyuanchao 1 년 전
부모
커밋
8c2411b5d8
1개의 변경된 파일19개의 추가작업 그리고 29개의 파일을 삭제
  1. 19 29
      soc-modules/soc-modules-mediator/src/main/java/com/xunmei/mediator/websocket/dto/WebsocketPayloadResolve.java

+ 19 - 29
soc-modules/soc-modules-mediator/src/main/java/com/xunmei/mediator/websocket/dto/WebsocketPayloadResolve.java

@@ -22,34 +22,24 @@ public class WebsocketPayloadResolve {
 
     public WebsocketPayloadResolve(WebsocketResult result) {
         JSONObject parentPayload = (JSONObject) result.getPayload();
-        String serviceName = (String) parentPayload.get(WebSocketConstants.SERVICE);
-        if (WebSocketConstants.UP_LINK_SERVICE_PASS_THROUGH.equals(serviceName) ||
-                WebSocketConstants.DOWN_LINK_SERVICE_PASS_THROUGH.equals(serviceName)) {
-            // 说明是透穿的
-            JSONObject payloadObj = (JSONObject) parentPayload.get(WebSocketConstants.ARGS);
-            WebsocketResult childResult = payloadObj.toJavaObject(WebsocketResult.class);
-            JSONObject childValue = (JSONObject) childResult.getPayload();
-            Object object = childValue.get(WebSocketConstants.ARGS);
-            if (object instanceof JSONObject) {
-                JSONObject object1 = (JSONObject) object;
-                String event = (String) childValue.get(WebSocketConstants.EVENT);
-                String service = (String) childValue.get(WebSocketConstants.SERVICE);
-                JSONObject header = (JSONObject) object1.get(WebSocketConstants.HEADER);
-                String routingKey = ObjectUtil.isNotEmpty(event) ? event : service;
-                this.routingKey = routingKey;
-                this.header = header;
-                this.data = object1;
-                this.topic=childResult.getTopic();
-            }
-        } else {
-            String event = (String) parentPayload.get(WebSocketConstants.EVENT);
-            String service = (String) parentPayload.get(WebSocketConstants.SERVICE);
-            JSONObject header = (JSONObject) parentPayload.get(WebSocketConstants.HEADER);
-            String routingKey = ObjectUtil.isNotEmpty(event) ? event : service;
-            this.routingKey = routingKey;
-            this.header = header;
-            this.data = parentPayload.get(WebSocketConstants.ARGS);
-            this.topic=result.getTopic();
-        }
+        String serviceName = parentPayload.getString(WebSocketConstants.SERVICE);
+        //是否透穿的数据,是的话,取里面那层payload的数据,不是的话就去第一层的数据
+        boolean isPassThrough = WebSocketConstants.UP_LINK_SERVICE_PASS_THROUGH.equals(serviceName) ||
+                WebSocketConstants.DOWN_LINK_SERVICE_PASS_THROUGH.equals(serviceName);
+
+        JSONObject payloadToExtract = isPassThrough ?
+                ((JSONObject) ((JSONObject) parentPayload.get(WebSocketConstants.ARGS)).toJavaObject(WebsocketResult.class).getPayload()) :
+                parentPayload;
+
+        this.routingKey = getRoutingKey(payloadToExtract);
+        this.header = payloadToExtract.getJSONObject(WebSocketConstants.HEADER);
+        this.data = payloadToExtract.get(WebSocketConstants.ARGS) != null ?payloadToExtract.get(WebSocketConstants.ARGS) :payloadToExtract.get(WebSocketConstants.DATA);
+        this.topic = isPassThrough ?((JSONObject) parentPayload.get(WebSocketConstants.ARGS)).toJavaObject(WebsocketResult.class).getTopic() :result.getTopic();
+    }
+
+    private String getRoutingKey(JSONObject payload) {
+        String event = payload.getString(WebSocketConstants.EVENT);
+        String service = payload.getString(WebSocketConstants.SERVICE);
+        return ObjectUtil.isNotEmpty(event) ? event : service;
     }
 }