From b63c33e2ae868986685007443640982aeafca4d0 Mon Sep 17 00:00:00 2001 From: gzydong <837215079@qq.com> Date: Fri, 9 Jul 2021 19:40:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Constants/TalkMode.php | 8 +-- app/Controller/Api/V1/TalkController.php | 22 ------- .../Api/V1/TalkMessageController.php | 6 +- app/Controller/WebSocketController.php | 33 ++++------ app/Service/Message/ReceiveHandleService.php | 64 +++++++++---------- app/Service/SocketClientService.php | 4 +- 6 files changed, 49 insertions(+), 88 deletions(-) diff --git a/app/Constants/TalkMode.php b/app/Constants/TalkMode.php index c6fd677..027d293 100644 --- a/app/Constants/TalkMode.php +++ b/app/Constants/TalkMode.php @@ -9,14 +9,10 @@ namespace App\Constants; */ class TalkMode { - /** - * 私聊 - */ + // 私信 const PRIVATE_CHAT = 1; - /** - * 群聊 - */ + // 群聊 const GROUP_CHAT = 2; public static function getTypes() diff --git a/app/Controller/Api/V1/TalkController.php b/app/Controller/Api/V1/TalkController.php index 4e807af..d5a8ed1 100644 --- a/app/Controller/Api/V1/TalkController.php +++ b/app/Controller/Api/V1/TalkController.php @@ -307,26 +307,4 @@ class TalkController extends CController 'limit' => $limit ]); } - - /** - * 搜索聊天记录(待开发) - * @RequestMapping(path="records-search", methods="get") - * - * @return ResponseInterface - */ - public function searchChatRecords() - { - return $this->response->success(); - } - - /** - * 获取聊天记录上下文数据(待开发) - * @RequestMapping(path="records-context", methods="get") - * - * @return ResponseInterface - */ - public function getRecordsContext() - { - return $this->response->success(); - } } diff --git a/app/Controller/Api/V1/TalkMessageController.php b/app/Controller/Api/V1/TalkMessageController.php index 542456a..3a14b07 100644 --- a/app/Controller/Api/V1/TalkMessageController.php +++ b/app/Controller/Api/V1/TalkMessageController.php @@ -244,9 +244,7 @@ class TalkMessageController extends CController return $this->response->fail('暂不属于好友关系或群聊成员,无法发送聊天消息!'); } - $emoticon = EmoticonItem::where('id', $params['emoticon_id'])->where('user_id', $user_id)->first([ - 'url', 'file_suffix', 'file_size' - ]); + $emoticon = EmoticonItem::where('id', $params['emoticon_id'])->where('user_id', $user_id)->first(); if (!$emoticon) return $this->response->fail('表情不存在!'); @@ -368,7 +366,7 @@ class TalkMessageController extends CController { $params = $this->request->inputs(['record_id']); $this->validate($params, [ - 'record_id' => 'required|integer|min:0' + 'record_id' => 'required|integer|min:1' ]); [$isTrue, $message,] = $this->talkService->revokeRecord($this->uid(), $params['record_id']); diff --git a/app/Controller/WebSocketController.php b/app/Controller/WebSocketController.php index b0daab4..9c6882e 100644 --- a/app/Controller/WebSocketController.php +++ b/app/Controller/WebSocketController.php @@ -37,21 +37,13 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos * @inject * @var SocketClientService */ - private $socketClientService; + private $client; /** * @inject * @var ReceiveHandleService */ - private $receiveHandleService; - - /** - * 消息事件绑定 - */ - const EVENTS = [ - TalkMessageEvent::EVENT_TALK => 'onTalk', - TalkMessageEvent::EVENT_KEYBOARD => 'onKeyboard', - ]; + private $receiveHandle; /** * 连接创建成功回调事件 @@ -67,7 +59,7 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos stdout_log()->notice("用户连接信息 : user_id:{$user_id} | fd:{$request->fd} 时间:" . date('Y-m-d H:i:s')); // 判断是否存在异地登录 - $isOnline = $this->socketClientService->isOnlineAll($user_id); + $isOnline = $this->client->isOnlineAll($user_id); // 若开启单点登录,则主动关闭之前登录的连接 if ($isOnline) { @@ -75,7 +67,7 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos } // 绑定fd与用户关系 - $this->socketClientService->bindRelation($request->fd, $user_id); + $this->client->bind($request->fd, $user_id); // 加入群聊 $groupIds = GroupMember::getUserGroupIds($user_id); @@ -105,13 +97,12 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos if ($frame->data == 'PING') return; $result = json_decode($frame->data, true); - if (!isset(self::EVENTS[$result['event']])) return; - // 回调事件处理函数 - call_user_func_array([ - $this->receiveHandleService, - self::EVENTS[$result['event']] - ], [$server, $frame, $result['data']]); + if (isset(ReceiveHandleService::EVENTS[$result['event']])) { + call_user_func_array([$this->receiveHandle, ReceiveHandleService::EVENTS[$result['event']]], [ + $server, $frame, $result['data'] + ]); + } } /** @@ -123,15 +114,15 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos */ public function onClose($server, int $fd, int $reactorId): void { - $user_id = $this->socketClientService->findFdUserId($fd); + $user_id = $this->client->findFdUserId($fd); stdout_log()->notice("客户端FD:{$fd} 已关闭连接 ,用户ID为【{$user_id}】,关闭时间:" . date('Y-m-d H:i:s')); // 删除 fd 绑定关系 - $this->socketClientService->removeRelation($fd); + $this->client->unbind($fd); // 判断是否存在异地登录 - $isOnline = $this->socketClientService->isOnlineAll($user_id); + $isOnline = $this->client->isOnlineAll($user_id); if (!$isOnline) { MessageProducer::publish( MessageProducer::create(TalkMessageEvent::EVENT_ONLINE_STATUS, [ diff --git a/app/Service/Message/ReceiveHandleService.php b/app/Service/Message/ReceiveHandleService.php index ee564dc..48b102b 100644 --- a/app/Service/Message/ReceiveHandleService.php +++ b/app/Service/Message/ReceiveHandleService.php @@ -21,15 +21,26 @@ class ReceiveHandleService /** * @var SocketClientService */ - private $socketClientService; + private $client; - public function __construct(SocketClientService $clientService) + // 消息事件绑定 + const EVENTS = [ + TalkMessageEvent::EVENT_TALK => 'onTalk', + TalkMessageEvent::EVENT_KEYBOARD => 'onKeyboard', + ]; + + /** + * ReceiveHandleService constructor. + * + * @param SocketClientService $client + */ + public function __construct(SocketClientService $client) { - $this->socketClientService = $clientService; + $this->client = $client; } /** - * 对话消息 + * 对话文本消息 * * @param Response|Server $server * @param Frame $frame @@ -38,24 +49,18 @@ class ReceiveHandleService */ public function onTalk($server, Frame $frame, $data) { - $user_id = $this->socketClientService->findFdUserId($frame->fd); - if ($user_id != $data['sender_id']) { - return; - } + $user_id = $this->client->findFdUserId($frame->fd); + if ($user_id != $data['sender_id']) return; - // 验证消息类型 私聊|群聊 - if (!in_array($data['talk_type'], TalkMode::getTypes())) { - return; - } + // 验证消息类型 + if (!in_array($data['talk_type'], TalkMode::getTypes())) return; - // 验证发送消息用户与接受消息用户之间是否存在好友或群聊关系(后期走缓存) + // 验证发送消息用户与接受消息用户之间是否存在好友或群聊关系 if ($data['talk_type'] == TalkMode::PRIVATE_CHAT) { - // 判断发送者和接受者是否是好友关系 if (!UsersFriend::isFriend((int)$data['sender_id'], (int)$data['receiver_id'], true)) { return; } } else if ($data['talk_type'] == TalkMode::GROUP_CHAT) { - // 判断是否属于群成员 if (!Group::isMember((int)$data['receiver_id'], (int)$data['sender_id'])) { return; } @@ -71,11 +76,8 @@ class ReceiveHandleService 'updated_at' => date('Y-m-d H:i:s'), ]); - if (!$result) return; - - // 判断是否私聊 + // 判断是否私信 if ($result->talk_type == TalkMode::PRIVATE_CHAT) { - // 设置好友消息未读数 UnreadTalk::getInstance()->increment($result->user_id, $result->receiver_id); } @@ -85,14 +87,12 @@ class ReceiveHandleService 'created_at' => $result->created_at ]); - MessageProducer::publish( - MessageProducer::create(TalkMessageEvent::EVENT_TALK, [ - 'sender_id' => $result->user_id, - 'receiver_id' => $result->receiver_id, - 'talk_type' => $result->talk_type, - 'record_id' => $result->id - ]) - ); + MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_TALK, [ + 'sender_id' => $result->user_id, + 'receiver_id' => $result->receiver_id, + 'talk_type' => $result->talk_type, + 'record_id' => $result->id + ])); } /** @@ -105,11 +105,9 @@ class ReceiveHandleService */ public function onKeyboard($server, Frame $frame, $data) { - MessageProducer::publish( - MessageProducer::create(TalkMessageEvent::EVENT_KEYBOARD, [ - 'sender_id' => intval($data['sender_id']), - 'receiver_id' => intval($data['receiver_id']), - ]) - ); + MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_KEYBOARD, [ + 'sender_id' => (int)$data['sender_id'], + 'receiver_id' => (int)$data['receiver_id'], + ])); } } diff --git a/app/Service/SocketClientService.php b/app/Service/SocketClientService.php index 6fb1c8b..9489ea7 100644 --- a/app/Service/SocketClientService.php +++ b/app/Service/SocketClientService.php @@ -19,7 +19,7 @@ class SocketClientService * @param int $user_id 用户ID * @param string $run_id 服务运行ID(默认当前服务ID) */ - public function bindRelation(int $fd, int $user_id, $run_id = SERVER_RUN_ID) + public function bind(int $fd, int $user_id, $run_id = SERVER_RUN_ID) { SocketFdBindUser::getInstance()->bind($fd, $user_id, $run_id); SocketUserBindFds::getInstance()->bind($fd, $user_id, $run_id); @@ -31,7 +31,7 @@ class SocketClientService * @param int $fd 客户端ID * @param string $run_id 服务运行ID(默认当前服务ID) */ - public function removeRelation(int $fd, $run_id = SERVER_RUN_ID) + public function unbind(int $fd, $run_id = SERVER_RUN_ID) { $user_id = $this->findFdUserId($fd);