Browse Source

考生端websocket

wangliang 4 năm trước cách đây
mục cha
commit
c5d4df992e

+ 38 - 0
themis-business/src/main/java/com/qmth/themis/business/util/WebsocketUtil.java

@@ -0,0 +1,38 @@
+package com.qmth.themis.business.util;
+
+import java.lang.reflect.Field;
+
+/**
+ * @Description: websocket util
+ * @Param:
+ * @return:
+ * @Author: wangliang
+ * @Date: 2020/7/27
+ */
+public class WebsocketUtil {
+
+    public static Object getFieldInstance(Object obj, String fieldPath) {
+        String fields[] = fieldPath.split("#");
+        for (String field : fields) {
+            obj = getField(obj, obj.getClass(), field);
+            if (obj == null) {
+                return null;
+            }
+        }
+
+        return obj;
+    }
+
+    public static Object getField(Object obj, Class<?> clazz, String fieldName) {
+        for (; clazz != Object.class; clazz = clazz.getSuperclass()) {
+            try {
+                Field field;
+                field = clazz.getDeclaredField(fieldName);
+                field.setAccessible(true);
+                return field.get(obj);
+            } catch (Exception e) {
+            }
+        }
+        return null;
+    }
+}

+ 12 - 10
themis-exam/src/main/java/com/qmth/themis/exam/websocket/WebSocketServer.java

@@ -8,6 +8,7 @@ import com.qmth.themis.business.enums.MqEnum;
 import com.qmth.themis.business.enums.SystemOperationEnum;
 import com.qmth.themis.business.util.JacksonUtil;
 import com.qmth.themis.business.util.RedisUtil;
+import com.qmth.themis.business.util.WebsocketUtil;
 import com.qmth.themis.common.enums.ExceptionResultEnum;
 import com.qmth.themis.common.exception.BusinessException;
 import com.qmth.themis.common.signature.SignatureInfo;
@@ -27,6 +28,7 @@ import javax.websocket.server.PathParam;
 import javax.websocket.server.ServerEndpoint;
 import java.io.IOException;
 import java.lang.reflect.Method;
+import java.net.InetSocketAddress;
 import java.time.LocalDateTime;
 import java.time.ZoneOffset;
 import java.util.*;
@@ -49,16 +51,14 @@ public class WebSocketServer
     /**
      * 与某个客户端的连接会话,需要通过它来给客户端发送数据
      */
-    private Session session;
-    private String sessionId;
-    private Long recordId;
-    private String platform = null;
-    private String deviceId = null;
-    private String Authorization = null;
+    private Session session = null;
+    private String sessionId = null, ip = null;
+    private Long recordId = null;
+    private String platform = null, deviceId = null, Authorization = null;
     private Long time = null;
     private RedisUtil redisUtil;
     private Long updateTime = null;
-    private Map<String, Object> tranMap;
+    private Map<String, Object> tranMap = null;
 
     /**
      * 连接建立成功调用的方法
@@ -111,12 +111,13 @@ public class WebSocketServer
                     //发送恢复网络mq消息
                     log.info("用户连接:" + this.sessionId + ",当前在线人数为:" + getOnlineCount());
                     try {
-                        this.sendMessage("连接成功");
+                        InetSocketAddress addr = (InetSocketAddress) WebsocketUtil.getFieldInstance(this.session.getAsyncRemote(), "base#socketWrapper#socket#sc#remoteAddress");
+                        this.ip = addr.toString().replace("/", "").split(":")[0];
+                        this.sendMessage("ip[" + this.ip + "]连接成功");
                         tranMap = new HashMap<>();
                         tranMap.put("recordId", this.recordId);
                         tranMap.put("deviceId", this.deviceId);
-                        //todo 这里获取真实ip
-                        tranMap.put("ip", "");
+                        tranMap.put("ip", this.ip);
                         tranMap.put("updateTime", this.updateTime);
                     } catch (IOException e) {
                         e.printStackTrace();
@@ -136,6 +137,7 @@ public class WebSocketServer
      */
     @OnClose
     public void onClose() {
+        log.info("onClose is come in");
         if (webSocketMap.containsKey(this.sessionId)) {
             webSocketMap.remove(this.sessionId);
             //从set中删除