优化代码

main
gzydong 2021-07-09 19:40:43 +08:00
parent d40d06091e
commit b63c33e2ae
6 changed files with 49 additions and 88 deletions

View File

@ -9,14 +9,10 @@ namespace App\Constants;
*/ */
class TalkMode class TalkMode
{ {
/** // 私信
* 私聊
*/
const PRIVATE_CHAT = 1; const PRIVATE_CHAT = 1;
/** // 群聊
* 群聊
*/
const GROUP_CHAT = 2; const GROUP_CHAT = 2;
public static function getTypes() public static function getTypes()

View File

@ -307,26 +307,4 @@ class TalkController extends CController
'limit' => $limit '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();
}
} }

View File

@ -244,9 +244,7 @@ class TalkMessageController extends CController
return $this->response->fail('暂不属于好友关系或群聊成员,无法发送聊天消息!'); return $this->response->fail('暂不属于好友关系或群聊成员,无法发送聊天消息!');
} }
$emoticon = EmoticonItem::where('id', $params['emoticon_id'])->where('user_id', $user_id)->first([ $emoticon = EmoticonItem::where('id', $params['emoticon_id'])->where('user_id', $user_id)->first();
'url', 'file_suffix', 'file_size'
]);
if (!$emoticon) return $this->response->fail('表情不存在!'); if (!$emoticon) return $this->response->fail('表情不存在!');
@ -368,7 +366,7 @@ class TalkMessageController extends CController
{ {
$params = $this->request->inputs(['record_id']); $params = $this->request->inputs(['record_id']);
$this->validate($params, [ $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']); [$isTrue, $message,] = $this->talkService->revokeRecord($this->uid(), $params['record_id']);

View File

@ -37,21 +37,13 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos
* @inject * @inject
* @var SocketClientService * @var SocketClientService
*/ */
private $socketClientService; private $client;
/** /**
* @inject * @inject
* @var ReceiveHandleService * @var ReceiveHandleService
*/ */
private $receiveHandleService; private $receiveHandle;
/**
* 消息事件绑定
*/
const EVENTS = [
TalkMessageEvent::EVENT_TALK => 'onTalk',
TalkMessageEvent::EVENT_KEYBOARD => 'onKeyboard',
];
/** /**
* 连接创建成功回调事件 * 连接创建成功回调事件
@ -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')); 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) { if ($isOnline) {
@ -75,7 +67,7 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos
} }
// 绑定fd与用户关系 // 绑定fd与用户关系
$this->socketClientService->bindRelation($request->fd, $user_id); $this->client->bind($request->fd, $user_id);
// 加入群聊 // 加入群聊
$groupIds = GroupMember::getUserGroupIds($user_id); $groupIds = GroupMember::getUserGroupIds($user_id);
@ -105,13 +97,12 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos
if ($frame->data == 'PING') return; if ($frame->data == 'PING') return;
$result = json_decode($frame->data, true); $result = json_decode($frame->data, true);
if (!isset(self::EVENTS[$result['event']])) return;
// 回调事件处理函数 if (isset(ReceiveHandleService::EVENTS[$result['event']])) {
call_user_func_array([ call_user_func_array([$this->receiveHandle, ReceiveHandleService::EVENTS[$result['event']]], [
$this->receiveHandleService, $server, $frame, $result['data']
self::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 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')); stdout_log()->notice("客户端FD:{$fd} 已关闭连接 用户ID为【{$user_id}】,关闭时间:" . date('Y-m-d H:i:s'));
// 删除 fd 绑定关系 // 删除 fd 绑定关系
$this->socketClientService->removeRelation($fd); $this->client->unbind($fd);
// 判断是否存在异地登录 // 判断是否存在异地登录
$isOnline = $this->socketClientService->isOnlineAll($user_id); $isOnline = $this->client->isOnlineAll($user_id);
if (!$isOnline) { if (!$isOnline) {
MessageProducer::publish( MessageProducer::publish(
MessageProducer::create(TalkMessageEvent::EVENT_ONLINE_STATUS, [ MessageProducer::create(TalkMessageEvent::EVENT_ONLINE_STATUS, [

View File

@ -21,15 +21,26 @@ class ReceiveHandleService
/** /**
* @var SocketClientService * @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 Response|Server $server
* @param Frame $frame * @param Frame $frame
@ -38,24 +49,18 @@ class ReceiveHandleService
*/ */
public function onTalk($server, Frame $frame, $data) public function onTalk($server, Frame $frame, $data)
{ {
$user_id = $this->socketClientService->findFdUserId($frame->fd); $user_id = $this->client->findFdUserId($frame->fd);
if ($user_id != $data['sender_id']) { if ($user_id != $data['sender_id']) return;
return;
}
// 验证消息类型 私聊|群聊 // 验证消息类型
if (!in_array($data['talk_type'], TalkMode::getTypes())) { if (!in_array($data['talk_type'], TalkMode::getTypes())) return;
return;
}
// 验证发送消息用户与接受消息用户之间是否存在好友或群聊关系(后期走缓存) // 验证发送消息用户与接受消息用户之间是否存在好友或群聊关系
if ($data['talk_type'] == TalkMode::PRIVATE_CHAT) { if ($data['talk_type'] == TalkMode::PRIVATE_CHAT) {
// 判断发送者和接受者是否是好友关系
if (!UsersFriend::isFriend((int)$data['sender_id'], (int)$data['receiver_id'], true)) { if (!UsersFriend::isFriend((int)$data['sender_id'], (int)$data['receiver_id'], true)) {
return; return;
} }
} else if ($data['talk_type'] == TalkMode::GROUP_CHAT) { } else if ($data['talk_type'] == TalkMode::GROUP_CHAT) {
// 判断是否属于群成员
if (!Group::isMember((int)$data['receiver_id'], (int)$data['sender_id'])) { if (!Group::isMember((int)$data['receiver_id'], (int)$data['sender_id'])) {
return; return;
} }
@ -71,11 +76,8 @@ class ReceiveHandleService
'updated_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s'),
]); ]);
if (!$result) return; // 判断是否私信
// 判断是否私聊
if ($result->talk_type == TalkMode::PRIVATE_CHAT) { if ($result->talk_type == TalkMode::PRIVATE_CHAT) {
// 设置好友消息未读数
UnreadTalk::getInstance()->increment($result->user_id, $result->receiver_id); UnreadTalk::getInstance()->increment($result->user_id, $result->receiver_id);
} }
@ -85,14 +87,12 @@ class ReceiveHandleService
'created_at' => $result->created_at 'created_at' => $result->created_at
]); ]);
MessageProducer::publish( MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_TALK, [
MessageProducer::create(TalkMessageEvent::EVENT_TALK, [
'sender_id' => $result->user_id, 'sender_id' => $result->user_id,
'receiver_id' => $result->receiver_id, 'receiver_id' => $result->receiver_id,
'talk_type' => $result->talk_type, 'talk_type' => $result->talk_type,
'record_id' => $result->id 'record_id' => $result->id
]) ]));
);
} }
/** /**
@ -105,11 +105,9 @@ class ReceiveHandleService
*/ */
public function onKeyboard($server, Frame $frame, $data) public function onKeyboard($server, Frame $frame, $data)
{ {
MessageProducer::publish( MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_KEYBOARD, [
MessageProducer::create(TalkMessageEvent::EVENT_KEYBOARD, [ 'sender_id' => (int)$data['sender_id'],
'sender_id' => intval($data['sender_id']), 'receiver_id' => (int)$data['receiver_id'],
'receiver_id' => intval($data['receiver_id']), ]));
])
);
} }
} }

View File

@ -19,7 +19,7 @@ class SocketClientService
* @param int $user_id 用户ID * @param int $user_id 用户ID
* @param string $run_id 服务运行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); SocketFdBindUser::getInstance()->bind($fd, $user_id, $run_id);
SocketUserBindFds::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 int $fd 客户端ID
* @param string $run_id 服务运行ID默认当前服务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); $user_id = $this->findFdUserId($fd);