优化代码

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
{
/**
* 私聊
*/
// 私信
const PRIVATE_CHAT = 1;
/**
* 群聊
*/
// 群聊
const GROUP_CHAT = 2;
public static function getTypes()

View File

@ -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();
}
}

View File

@ -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']);

View File

@ -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, [

View File

@ -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'],
]));
}
}

View File

@ -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);