소스 검색

服务站大屏

suntianwu 3 년 전
부모
커밋
6bfafe2c08
1개의 변경된 파일35개의 추가작업 그리고 29개의 파일을 삭제
  1. 35 29
      src/main/java/com/care/bigscreen/websocket/BigScreenWebSocketEndpoint.java

+ 35 - 29
src/main/java/com/care/bigscreen/websocket/BigScreenWebSocketEndpoint.java

@@ -1,11 +1,16 @@
 package com.care.bigscreen.websocket;
 package com.care.bigscreen.websocket;
 
 
 
 
+import cn.hutool.json.JSONUtil;
+import com.care.common.vo.UserLogindConvertVO;
 import com.care.util.ExUtil;
 import com.care.util.ExUtil;
 import com.care.util.JsonUtil;
 import com.care.util.JsonUtil;
+import com.care.util.JwtUtils;
 import com.care.util.Result;
 import com.care.util.Result;
+import io.jsonwebtoken.Claims;
 import org.slf4j.Logger;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 import org.springframework.stereotype.Component;
 import org.springframework.util.StringUtils;
 import org.springframework.util.StringUtils;
 
 
@@ -24,6 +29,10 @@ import java.util.concurrent.CopyOnWriteArraySet;
 @Component
 @Component
 public class BigScreenWebSocketEndpoint {
 public class BigScreenWebSocketEndpoint {
     private static final Logger logger = LoggerFactory.getLogger(BigScreenWebSocketEndpoint.class);
     private static final Logger logger = LoggerFactory.getLogger(BigScreenWebSocketEndpoint.class);
+
+    @Autowired
+    JwtUtils jwtUtils;
+
     //用来存放每个客户端对应的BigScreenWebSocketEndpoint对象
     //用来存放每个客户端对应的BigScreenWebSocketEndpoint对象
     private static CopyOnWriteArraySet<BigScreenWebSocketEndpoint> currentWebSocketSession = new CopyOnWriteArraySet<>();
     private static CopyOnWriteArraySet<BigScreenWebSocketEndpoint> currentWebSocketSession = new CopyOnWriteArraySet<>();
 
 
@@ -31,8 +40,8 @@ public class BigScreenWebSocketEndpoint {
     private Session session;
     private Session session;
     //用于关联当前客户端和websocket会话的关系
     //用于关联当前客户端和websocket会话的关系
 
 
-    //登录
-    private String username;
+    //电话作为登陆用户
+    private String phone;
 
 
     //服务站ID
     //服务站ID
     private String stationId;
     private String stationId;
@@ -52,8 +61,8 @@ public class BigScreenWebSocketEndpoint {
                 closeSession(session, "-1", "连接失败,token为空");
                 closeSession(session, "-1", "连接失败,token为空");
                 return;
                 return;
             }
             }
-            //建立websocket和自动外呼坐席的关联关系
-            //从token中解析出坐席的信息
+            //建立websocket和登录用户、站点ID的关联关系
+            //从token中解析出登录用户的信息
             String[] keyValues = requestParamString.split("&");
             String[] keyValues = requestParamString.split("&");
             //取出token 字符串
             //取出token 字符串
             String ts = null;
             String ts = null;
@@ -65,25 +74,22 @@ public class BigScreenWebSocketEndpoint {
                     stationId = t.replaceAll("stationId=", "");
                     stationId = t.replaceAll("stationId=", "");
                 }
                 }
             }
             }
-//            if (StringUtils.isEmpty(ts) || StringUtils.isEmpty(stationId)) {
-//                closeSession(session, "-1", "连接失败,token或stationId为空");
-//                return;
-//            }
-//
-//            //连接对象初始化
-//            Token token = null;
-//            try {
-//                token = TokenUtil.parseTokenStr(ts);
-//            } catch (Exception e) {
-//                logger.error("websocket会话建立异常:"+ ExUtil.exToDetail(e));
-//            }
-//            if(token == null){
-//                closeSession(session, "-1", "连接失败,token为空");
-//                return;
-//            }
-//
-//            logger.info("websocket会话建立:登录用户={},服务站ID={}登录!", token.getPayload().getUsername(), this.stationId);
-//            this.username = token.getPayload().getUsername();
+            if (StringUtils.isEmpty(ts) || StringUtils.isEmpty(stationId)) {
+                closeSession(session, "-1", "连接失败,token或stationId为空");
+                return;
+            }
+
+            Claims claims = jwtUtils.tokenParse(ts);
+            if (claims != null) {
+                UserLogindConvertVO vo = JSONUtil.toBean(claims.getSubject(), UserLogindConvertVO.class);
+                if (vo != null) {
+                    this.phone = vo.getPhone();
+                } else{
+                    closeSession(session, "-1", "连接失败,token解析失败");
+                    return;
+                }
+            }
+            logger.info("websocket会话建立:服务站ID={},登录用户={}登录!", this.stationId,this.phone);
             this.session = session;
             this.session = session;
             currentWebSocketSession.add(this);
             currentWebSocketSession.add(this);
         } catch (Exception e) {
         } catch (Exception e) {
@@ -96,12 +102,12 @@ public class BigScreenWebSocketEndpoint {
     public void onClose(Session session) {
     public void onClose(Session session) {
         //移除连接
         //移除连接
         currentWebSocketSession.remove(this);
         currentWebSocketSession.remove(this);
-        logger.info("客户端断开连接,客户端id={},坐席={},退出自动外呼", session.getId(), this.username);
+        logger.info("客户端断开连接,客户端id={},服务站ID={},登录用户={},退出websocket连接", session.getId(), this.stationId, this.phone);
     }
     }
 
 
     @OnError
     @OnError
     public void onError(Session session, Throwable error) {
     public void onError(Session session, Throwable error) {
-        logger.error("客户端id={},服务站ID={},登录用户={},连接异常信息={}.", session.getId(), this.stationId, this.username, error.getCause());
+        logger.error("客户端id={},服务站ID={},登录用户={},连接异常信息={}.", session.getId(), this.stationId, this.phone, error.getCause());
     }
     }
 
 
     @OnMessage
     @OnMessage
@@ -121,7 +127,7 @@ public class BigScreenWebSocketEndpoint {
                 try {
                 try {
                     session.getBasicRemote().sendText(message);
                     session.getBasicRemote().sendText(message);
                 } catch (IOException e) {
                 } catch (IOException e) {
-                    logger.info("给服务站ID={},登录用户={} 发送数据失败,客户端连接已经断开.", endpoint.getStationId(), endpoint.getUsername());
+                    logger.info("给服务站ID={},登录用户={} 发送数据失败,客户端连接已经断开.", endpoint.getStationId(), endpoint.getPhone());
                 }
                 }
             }
             }
         }
         }
@@ -142,7 +148,7 @@ public class BigScreenWebSocketEndpoint {
                 try {
                 try {
                     session.getBasicRemote().sendText(msg);
                     session.getBasicRemote().sendText(msg);
                 } catch (IOException e) {
                 } catch (IOException e) {
-                    logger.info("给服务站ID={},登录用户={} 发送数据失败,客户端连接已经断开.", endpoint.getStationId(), endpoint.getUsername());
+                    logger.info("给服务站ID={},登录用户={} 发送数据失败,客户端连接已经断开.", endpoint.getStationId(), endpoint.getPhone());
                 }
                 }
             }
             }
         }
         }
@@ -178,8 +184,8 @@ public class BigScreenWebSocketEndpoint {
         return session;
         return session;
     }
     }
 
 
-    public String getUsername() {
-        return username;
+    public String getPhone() {
+        return phone;
     }
     }
 
 
     public String getStationId() {
     public String getStationId() {