ソースを参照

修正升压站报警异常

shilin 1 年間 前
コミット
285b97b00c

+ 19 - 1
alarm-service/pom.xml

@@ -30,11 +30,29 @@
         <pgsql.version>42.2.5</pgsql.version>
         <groovy.version>3.0.8</groovy.version>
         <springframework.boot.version>2.2.2.RELEASE</springframework.boot.version>
-
+        <tomcat.version>9.0.83</tomcat.version>
     </properties>
 
 
     <dependencies>
+
+
+        <dependency>
+            <groupId>org.apache.tomcat.embed</groupId>
+            <artifactId>tomcat-embed-core</artifactId>
+            <version>${tomcat.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.tomcat.embed</groupId>
+            <artifactId>tomcat-embed-websocket</artifactId>
+            <version>${tomcat.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.tomcat.embed</groupId>
+            <artifactId>tomcat-embed-el</artifactId>
+            <version>${tomcat.version}</version>
+        </dependency>
         <dependency>
             <groupId>com.opencsv</groupId>
             <artifactId>opencsv</artifactId>

+ 49 - 20
alarm-service/src/main/java/com/gyee/alarm/websocket/WebSocket.java

@@ -7,6 +7,7 @@ import org.springframework.stereotype.Component;
 import javax.websocket.*;
 import javax.websocket.server.PathParam;
 import javax.websocket.server.ServerEndpoint;
+
 import java.io.IOException;
 import java.util.concurrent.ConcurrentHashMap;
 
@@ -136,7 +137,7 @@ public class WebSocket {
             synchronized (session){
                 if(session.isOpen())
                 {
-                    session.getBasicRemote().sendObject(message);
+                    session.getBasicRemote().sendText(message);
                 }
             }
 
@@ -147,50 +148,78 @@ public class WebSocket {
     /**
      * 向客户端发送消息
      */
-    public synchronized void sendMessage(Object message) throws IOException, EncodeException {
+    public  void sendMessage(Object message) throws IOException, EncodeException {
 
         try {
-            if(StringUtils.notEmp(session))
+            if(StringUtils.notEmp(session) && session.isOpen())
             {
 
                 synchronized (session){
 
-                    if( session.isOpen())
-                    {
-                        session.getBasicRemote().sendObject(message);
-                    }
+                    session.getBasicRemote().sendObject(message);
                 }
 
             }
         }catch (Exception e)
         {
-            logger.error("用户错误:" + this.userId + ",原因:" + e.getStackTrace());
-//            if(StringUtils.notEmp(userId) && webSocketMap.containsKey(userId))
-//            {
-//                webSocketMap.remove(userId);
-//                subOnlineCount();           //在线数减1
-//                logger.info("用户{}关闭连接!当前在线人数为{}", userId, getOnlineCount());
-//            }
-//
+            logger.error("原因:" +getStackMsg(e));
 //            try {
-//                if(StringUtils.notEmp(session))
+//                if(StringUtils.notEmp(session) )
 //                {
-//                    session.close();
-//                    logger.info("用户错误:" + this.userId + ",关闭session:" );
+//                    if( session.isOpen())
+//                    {
+//                        session.close();
+//                        logger.info( "关闭session:" );
+//                    }else
+//                    {
+//                        logger.info( "session:已经关闭" );
+//                    }
 //                }else
 //                {
-//                    logger.info( ",session:null" );
+//                    logger.info( "session:null" );
 //                }
 //            }catch (IOException io)
 //            {
-//                logger.error("用户错误:" + this.userId + ",原因:" + io.getMessage());
+//                logger.error("原因:" +getStackMsg(io));
 //            }
+//            logger.error("原因:" + getStackMsg(e));
+//
+//            if(StringUtils.notEmp(userId) && webSocketMap.containsKey(userId))
+//            {
+//                webSocketMap.remove(userId);
+//                subOnlineCount();           //在线数减1
+//                logger.info("用户{}关闭连接!当前在线人数为{}", userId, getOnlineCount());
+//            }
+
+
         }
 
 
         //this.session.getAsyncRemote().sendText(message);
     }
 
+    private static String getStackMsg(Exception e) {
+
+        StringBuffer sb = new StringBuffer();
+        StackTraceElement[] stackArray = e.getStackTrace();
+        for (int i = 0; i < stackArray.length; i++) {
+            StackTraceElement element = stackArray[i];
+            sb.append(element.toString() + "\n");
+        }
+        return sb.toString();
+    }
+
+    private static String getStackMsg(Throwable e) {
+
+        StringBuffer sb = new StringBuffer();
+        StackTraceElement[] stackArray = e.getStackTrace();
+        for (int i = 0; i < stackArray.length; i++) {
+            StackTraceElement element = stackArray[i];
+            sb.append(element.toString() + "\n");
+        }
+        return sb.toString();
+    }
+
     /**
      * 通过userId向客户端发送消息
      */