Prechádzať zdrojové kódy

优化IM出现参数错误的问题

kuaifan 5 rokov pred
rodič
commit
1386967243

+ 28 - 0
app/Events/ServerStartEvent.php

@@ -0,0 +1,28 @@
+<?php
+
+
+namespace App\Events;
+
+
+use Hhxsv5\LaravelS\Swoole\Events\ServerStartInterface;
+use Swoole\Http\Server;
+
+class ServerStartEvent implements ServerStartInterface
+{
+
+    public function __construct()
+    {
+    }
+
+    public function handle(Server $server)
+    {
+        $server->startMsecTime = $this->msecTime();
+    }
+
+    private function msecTime()
+    {
+        list($msec, $sec) = explode(' ', microtime());
+        $time = explode(".", $sec . ($msec * 1000));
+        return $time[0];
+    }
+}

+ 6 - 2
app/Events/WorkerStartEvent.php

@@ -1,6 +1,8 @@
 <?php
+
 namespace App\Events;
 
+use Cache;
 use DB;
 use Hhxsv5\LaravelS\Swoole\Events\WorkerStartInterface;
 use Swoole\Http\Server;
@@ -14,7 +16,9 @@ class WorkerStartEvent implements WorkerStartInterface
 
     public function handle(Server $server, $workerId)
     {
-        // TODO: Implement handle() method.
-        DB::table('ws')->delete();
+        if (isset($server->startMsecTime) && Cache::get("swooleServerStartMsecTime") != $server->startMsecTime) {
+            Cache::forever("swooleServerStartMsecTime", $server->startMsecTime);
+            DB::table('ws')->delete();
+        }
     }
 }

+ 2 - 1
app/Http/Controllers/Api/ChatController.php

@@ -174,7 +174,8 @@ class ChatController extends Controller
         //
         $username = trim(Request::input('username'));
         if (Base::leftExists($username, "group::")) {
-            $res = Chat::groupOpen($username);
+            //$res = Chat::groupOpen($username);
+            return Base::retError('参数错误');
         } else {
             $res = Chat::openDialog($user['username'], $username);
         }

+ 8 - 16
app/Services/WebSocketService.php

@@ -181,10 +181,7 @@ class WebSocketService implements WebSocketHandlerInterface
              * 总未读消息数
              */
             case 'unread':
-                $username = DB::table('ws')->where([
-                    'fd' => $frame->fd,
-                    'channel' => $data['channel'],
-                ])->value('username');
+                $username = $this->getUsername($frame->fd, $data['channel']);
                 if ($username) {
                     $num = intval(DB::table('chat_dialog')->where('user1', $username)->sum('unread1'));
                     $num+= intval(DB::table('chat_dialog')->where('user2', $username)->sum('unread2'));
@@ -198,10 +195,7 @@ class WebSocketService implements WebSocketHandlerInterface
              * 已读会员消息
              */
             case 'read':
-                $username = DB::table('ws')->where([
-                    'fd' => $frame->fd,
-                    'channel' => $data['channel'],
-                ])->value('username');
+                $username = $this->getUsername($frame->fd, $data['channel']);
                 $dialog = Chat::openDialog($username, $data['target']);
                 if (!Base::isError($dialog)) {
                     $dialog = $dialog['data'];
@@ -224,10 +218,7 @@ class WebSocketService implements WebSocketHandlerInterface
             case 'roger':
                 $contentIds = Base::explodeInt(',', $data['contentId']);
                 if ($contentIds) {
-                    $username = DB::table('ws')->where([
-                        'fd' => $frame->fd,
-                        'channel' => $data['channel'],
-                    ])->value('username');
+                    $username = $this->getUsername($frame->fd, $data['channel']);
                     if ($username) {
                         DB::table('chat_msg')->where('receive', $username)->whereIn('id', $contentIds)->update([
                             'roger' => 1,
@@ -240,10 +231,7 @@ class WebSocketService implements WebSocketHandlerInterface
              * 发给用户
              */
             case 'user':
-                $username = DB::table('ws')->where([
-                    'fd' => $frame->fd,
-                    'channel' => $data['channel'],
-                ])->value('username');
+                $username = $this->getUsername($frame->fd, $data['channel']);
                 $res = Chat::saveMessage($username, $data['target'], $data['body']);
                 if (Base::isError($res)) {
                     $back = [
@@ -463,6 +451,10 @@ class WebSocketService implements WebSocketHandlerInterface
         return $this->getUser('', $channel, $username);
     }
 
+    private function getUsername($fd, $channel) {
+        return DB::table('ws')->where(['fd' => $fd, 'channel' => $channel ])->value('username');
+    }
+
     /**
      * 获取团队所有在线用户
      * @return array|string

+ 1 - 0
config/laravels.php

@@ -19,6 +19,7 @@ return [
         'log'           => true,
     ],
     'event_handlers'           => [
+        'ServerStart' => \App\Events\ServerStartEvent::class,
         'WorkerStart' => \App\Events\WorkerStartEvent::class,
     ],
     'websocket'                => [