kuaifan 5 tahun lalu
induk
melakukan
d65d3a53dc
2 mengubah file dengan 24 tambahan dan 10 penghapusan
  1. 2 2
      app/Services/WebSocketService.php
  2. 22 8
      resources/assets/js/main/main.js

+ 2 - 2
app/Services/WebSocketService.php

@@ -106,7 +106,7 @@ class WebSocketService implements WebSocketHandlerInterface
                 break;
 
             case 'all':
-                foreach (self::getUsers() as $user) {
+                foreach (self::getUsersAll() as $user) {
                     $data['target'] = $user['username'];
                     $server->push($user['wsid'], Base::array2json($data));
                 }
@@ -153,7 +153,7 @@ class WebSocketService implements WebSocketHandlerInterface
      * 获取当前用户
      * @return array|string
      */
-    public static function getUsers()
+    public static function getUsersAll()
     {
         return Base::DBC2A(DB::table('users')->select(['wsid', 'username'])->where('wsid', '>', 0)->get());
     }

+ 22 - 8
resources/assets/js/main/main.js

@@ -290,6 +290,17 @@ import '../../sass/main.scss';
         WS: {
             __instance: null,
             __connected: false,
+            __autoLine() {
+                let tempId = $A.randomString(16);
+                this.__autoId = tempId;
+                // console.log("[WS] Connection auto 30s ...");
+                setTimeout(() => {
+                    if (this.__autoId === tempId) {
+                        // console.log("[WS] Connection auto ...");
+                        this.connection();
+                    }
+                }, 30 * 1000);
+            },
 
             /**
              * 连接
@@ -310,11 +321,12 @@ import '../../sass/main.scss';
 
                 // 连接建立时触发
                 this.__instance.onopen = (event) => {
-                    // console.log("Connection open ...");
+                    // console.log("[WS] Connection open ...");
                 }
 
                 // 接收到服务端推送时执行
                 this.__instance.onmessage = (event) => {
+                    // console.log("[WS] Connection message ...");
                     let msgDetail = $A.jsonParse(event.data);
                     if (msgDetail.messageType === 'open') {
                         this.__connected = true;
@@ -324,16 +336,18 @@ import '../../sass/main.scss';
 
                 // 连接关闭时触发
                 this.__instance.onclose = (event) => {
-                    // console.log("Connection closed ...");
+                    // console.log("[WS] Connection closed ...");
                     this.__connected = false;
                     this.__instance = null;
+                    this.__autoLine();
                 }
 
                 // 连接出错
                 this.__instance.onerror = (event) => {
-                    // console.log("Connection error ...");
+                    // console.log("[WS] Connection error ...");
                     this.__connected = false;
                     this.__instance = null;
+                    this.__autoLine();
                 }
 
                 return this;
@@ -375,15 +389,15 @@ import '../../sass/main.scss';
              */
             sendTo(type, target, content) {
                 if (this.__instance === null) {
-                    console.log("ws:未初始化连接");
+                    console.log("[WS] 未初始化连接");
                     return;
                 }
                 if (this.__connected === false) {
-                    console.log("ws:未连接成功");
+                    console.log("[WS] 未连接成功");
                     return;
                 }
                 if (['user', 'all'].indexOf(type) === -1) {
-                    console.log("ws:错误的消息类型-" + type);
+                    console.log("[WS] 错误的消息类型-" + type);
                     return;
                 }
                 this.__instance.send(JSON.stringify({
@@ -402,11 +416,11 @@ import '../../sass/main.scss';
              */
             close() {
                 if (this.__instance === null) {
-                    console.log("ws:未初始化连接");
+                    console.log("[WS] 未初始化连接");
                     return;
                 }
                 if (this.__connected === false) {
-                    console.log("ws:未连接成功");
+                    console.log("[WS] 未连接成功");
                     return;
                 }
                 this.__instance.close();