Browse Source

no message

kuaifan 5 years ago
parent
commit
847280271d
2 changed files with 19 additions and 4 deletions
  1. 1 1
      app/Module/Chat.php
  2. 18 3
      app/Services/WebSocketService.php

+ 1 - 1
app/Module/Chat.php

@@ -173,7 +173,7 @@ class Chat
         if (!is_array($array)) {
             $array = [];
         }
-        //messageType来自客户端(前端->后端):refresh/unread/read/roger/user/info/team/docs
+        //messageType来自客户端(前端->后端):refresh/unread/read/roger/user/info/team/docs/appActivity/appDeactive
         //messageType来自服务端(后端->前端):error/open/kick/user/back/unread/docs
         if (!isset($array['messageType'])) $array['messageType'] = '';  //消息类型
         if (!isset($array['messageId'])) $array['messageId'] = '';      //消息ID(用于back给客户端)

+ 18 - 3
app/Services/WebSocketService.php

@@ -84,7 +84,7 @@ class WebSocketService implements WebSocketHandlerInterface
             return;
         }
         //踢下线
-        /*if ($channel != 'chromeExtend') {
+        if (in_array($channel, ['ios', 'android'])) {
             $userLists = $this->getUser('', $channel, $username);
             foreach ($userLists AS $user) {
                 $server->push($user['fd'], Chat::formatMsgSend([
@@ -98,8 +98,9 @@ class WebSocketService implements WebSocketHandlerInterface
                 ]));
                 $this->deleteUser($user['fd']);
             }
-        }*/
+        }
         //保存用户、发送open事件
+        Cache::forever("ws::appActivity-" . $username, "yes");
         $this->saveUser($fd, $channel, $username);
         $server->push($fd, Chat::formatMsgSend([
             'messageType' => 'open',
@@ -165,6 +166,20 @@ class WebSocketService implements WebSocketHandlerInterface
                 break;
 
             /**
+             * APP进入前台
+             */
+            case 'appActivity':
+                Cache::forever("ws::appActivity-" . $data['username'], "yes");
+                break;
+
+            /**
+             * APP进入后台
+             */
+            case 'appDeactive':
+                Cache::forever("ws::appActivity-" . $data['username'], "no");
+                break;
+
+            /**
              * 总未读消息数
              */
             case 'unread':
@@ -259,7 +274,7 @@ class WebSocketService implements WebSocketHandlerInterface
                     Task::deliver($pushTask);
                     //
                     $notificationTask = new NotificationTask($resData['id']);
-                    $notificationTask->delay(8);
+                    $notificationTask->delay(Cache::get("ws::appActivity-" . $data['target']) == "no" ? 2 : 8);
                     Task::deliver($notificationTask);
                 }
                 break;