feat:优化代码

main
gzydong 2022-04-12 19:34:20 +08:00
parent 71bbfca2dc
commit 7cea815888
8 changed files with 24 additions and 27 deletions

View File

@ -18,22 +18,22 @@ class TalkEventConstant
/**
* 键盘输入事件通知 - 事件名
*/
const EVENT_KEYBOARD = 'event_keyboard';
const EVENT_TALK_KEYBOARD = 'event_talk_keyboard';
/**
* 用户在线状态通知 - 事件名
*/
const EVENT_ONLINE_STATUS = 'event_online_status';
const EVENT_LOGIN = 'event_login';
/**
* 聊天消息撤销通知 - 事件名
*/
const EVENT_REVOKE_TALK = 'event_revoke_talk';
const EVENT_TALK_REVOKE = 'event_talk_revoke';
/**
* 好友申请消息通知 - 事件名
*/
const EVENT_FRIEND_APPLY = 'event_friend_apply';
const EVENT_CONTACT_APPLY = 'event_contact_apply';
/**
* @return array
@ -42,10 +42,10 @@ class TalkEventConstant
{
return [
self::EVENT_TALK => '对话消息通知',
self::EVENT_KEYBOARD => '键盘输入事件通知',
self::EVENT_ONLINE_STATUS => '用户在线状态通知',
self::EVENT_REVOKE_TALK => '聊天消息撤销通知',
self::EVENT_FRIEND_APPLY => '好友申请消息通知'
self::EVENT_TALK_KEYBOARD => '键盘输入事件通知',
self::EVENT_LOGIN => '用户在线状态通知',
self::EVENT_TALK_REVOKE => '聊天消息撤销通知',
self::EVENT_CONTACT_APPLY => '好友申请消息通知'
];
}
}

View File

@ -80,7 +80,7 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos
}
if (!$isOnline) {
event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_ONLINE_STATUS, [
event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_LOGIN, [
'user_id' => $user_id,
'status' => 1,
]));
@ -133,7 +133,7 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos
$isOnline = $this->client->isOnlineAll($user_id);
if ($isOnline) return;
event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_ONLINE_STATUS, [
event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_LOGIN, [
'user_id' => $user_id,
'status' => 0,
]));

View File

@ -50,8 +50,6 @@ class RedisWebsocketSubscribe extends AbstractProcess
//echo PHP_EOL . "chan : $chan , msg : $message";
$data = json_decode($message, true);
var_dump("subscribe ====> {$message}");
$this->handleService->handle($data);
}
}

View File

@ -52,7 +52,7 @@ class ContactApplyService
// 判断对方是否在线。如果在线发送消息通知
$isOnline = di()->get(SocketClientService::class)->isOnlineAll($friend_id);
if ($isOnline) {
event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_FRIEND_APPLY, [
event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_CONTACT_APPLY, [
'apply_id' => $result->id,
'type' => 1,
]));
@ -103,7 +103,7 @@ class ContactApplyService
// 判断对方是否在线。如果在线发送消息通知
$isOnline = di()->get(SocketClientService::class)->isOnlineAll($info->user_id);
if ($isOnline) {
event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_FRIEND_APPLY, [
event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_CONTACT_APPLY, [
'apply_id' => $apply_id,
'type' => 2,
]));

View File

@ -282,7 +282,6 @@ class GroupService extends BaseService
Db::commit();
} catch (Exception $e) {
Db::rollBack();
var_dump($e->getMessage());
return false;
}

View File

@ -22,8 +22,8 @@ class ReceiveHandleService
// 消息事件绑定
const EVENTS = [
TalkEventConstant::EVENT_TALK => 'onTalk',
TalkEventConstant::EVENT_KEYBOARD => 'onKeyboard',
TalkEventConstant::EVENT_TALK => 'onTalk',
TalkEventConstant::EVENT_TALK_KEYBOARD => 'onKeyboard',
];
/**
@ -79,7 +79,7 @@ class ReceiveHandleService
*/
public function onKeyboard($server, Frame $frame, $data)
{
event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_KEYBOARD, [
event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_TALK_KEYBOARD, [
'sender_id' => (int)$data['sender_id'],
'receiver_id' => (int)$data['receiver_id'],
]));

View File

@ -25,16 +25,16 @@ class SubscribeHandleService
TalkEventConstant::EVENT_TALK => 'onConsumeTalk',
// 键盘输入事件
TalkEventConstant::EVENT_KEYBOARD => 'onConsumeKeyboard',
TalkEventConstant::EVENT_TALK_KEYBOARD => 'onConsumeKeyboard',
// 用户在线状态事件
TalkEventConstant::EVENT_ONLINE_STATUS => 'onConsumeOnlineStatus',
TalkEventConstant::EVENT_LOGIN => 'onConsumeOnlineStatus',
// 聊天消息推送事件
TalkEventConstant::EVENT_REVOKE_TALK => 'onConsumeRevokeTalk',
TalkEventConstant::EVENT_TALK_REVOKE => 'onConsumeRevokeTalk',
// 好友申请相关事件
TalkEventConstant::EVENT_FRIEND_APPLY => 'onConsumeFriendApply'
TalkEventConstant::EVENT_CONTACT_APPLY => 'onConsumeFriendApply'
];
/**
@ -133,7 +133,7 @@ class SubscribeHandleService
{
$fds = $this->clientService->findUserFds($data['data']['receiver_id']);
$this->push($fds, $this->toJson(TalkEventConstant::EVENT_KEYBOARD, $data['data']));
$this->push($fds, $this->toJson(TalkEventConstant::EVENT_TALK_KEYBOARD, $data['data']));
}
/**
@ -157,7 +157,7 @@ class SubscribeHandleService
$fds = array_unique(array_merge(...$fds));
$this->push($fds, $this->toJson(TalkEventConstant::EVENT_ONLINE_STATUS, [
$this->push($fds, $this->toJson(TalkEventConstant::EVENT_LOGIN, [
'user_id' => $user_id,
'status' => $status
]));
@ -188,7 +188,7 @@ class SubscribeHandleService
if (!$fds) return;
$this->push($fds, $this->toJson(TalkEventConstant::EVENT_REVOKE_TALK, [
$this->push($fds, $this->toJson(TalkEventConstant::EVENT_TALK_REVOKE, [
'talk_type' => $record->talk_type,
'sender_id' => $record->user_id,
'receiver_id' => $record->receiver_id,
@ -234,7 +234,7 @@ class SubscribeHandleService
'mobile' => $friendInfo->mobile,
];
$this->push(array_unique($fds), $this->toJson(TalkEventConstant::EVENT_FRIEND_APPLY, $msg));
$this->push(array_unique($fds), $this->toJson(TalkEventConstant::EVENT_CONTACT_APPLY, $msg));
}
private function toJson(string $event, array $data): string

View File

@ -213,7 +213,7 @@ class TalkService extends BaseService
$result->is_revoke = 1;
$result->save();
event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_REVOKE_TALK, [
event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_TALK_REVOKE, [
'record_id' => $result->id
]));