This commit is contained in:
2025-06-26 10:51:10 +08:00
parent a41967fac4
commit cfc77d29af
2 changed files with 14 additions and 11 deletions

View File

@@ -55,11 +55,12 @@ class GatewayClientService
/** /**
* 构建标准化消息 * 构建标准化消息
*/ */
protected function buildMessage(int $userId, string $name, string $text, string $type = 'received'): string protected function buildMessage(int $userId, string $name, string $avatarColor, string $text, string $type = 'received'): string
{ {
$message = [ $message = [
'userId' => $userId, 'userId' => $userId,
'userName' => $name, 'userName' => $name,
'avatarColor' => $avatarColor,
'content' => $text, 'content' => $text,
'timestamp' => time(), // ISO 8601格式时间 'timestamp' => time(), // ISO 8601格式时间
'type' => $type 'type' => $type
@@ -71,18 +72,18 @@ class GatewayClientService
/** /**
* 发送消息给指定用户 * 发送消息给指定用户
*/ */
public function sendToUser(int $toUserId, int $fromUserId, string $fromUserName, string $msgData, string $type = 'chat'): void public function sendToUser(int $toUserId, int $fromUserId, string $formUserAvatarColor, string $fromUserName, string $msgData, string $type = 'chat'): void
{ {
$message = $this->buildMessage($fromUserId, $fromUserName, $msgData, $type); $message = $this->buildMessage($fromUserId, $fromUserName, $formUserAvatarColor, $msgData, $type);
Gateway::sendToUid($toUserId, $message); Gateway::sendToUid($toUserId, $message);
} }
/** /**
* 广播消息给所有用户(可排除指定用户) * 广播消息给所有用户(可排除指定用户)
*/ */
public function broadcastMessage(int $fromUserId, string $fromUserName, string $text, ?int $excludeUserId = null, string $type = 'chat'): void public function broadcastMessage(int $fromUserId, string $formUserAvatarColor, string $fromUserName, string $text, ?int $excludeUserId = null, string $type = 'chat'): void
{ {
$message = $this->buildMessage($fromUserId, $fromUserName, $text, $type); $message = $this->buildMessage($fromUserId, $fromUserName, $formUserAvatarColor, $text, $type);
$excludeList = $excludeUserId ? [$excludeUserId] : []; $excludeList = $excludeUserId ? [$excludeUserId] : [];
Gateway::sendToAll($message, null, $excludeList); Gateway::sendToAll($message, null, $excludeList);

View File

@@ -28,22 +28,24 @@ class ImService extends BaseService
public function sendMessage($data, $type = MessageTypeEnum::Text->value) public function sendMessage($data, $type = MessageTypeEnum::Text->value)
{ {
$uids = [1,2,3]; // $uids = [1,2,3];
$uidsC = []; // $uidsC = [];
foreach ($uids as $v) { // foreach ($uids as $v) {
$uidsC[$v] = Gateway::getClientIdByUid($v); // $uidsC[$v] = Gateway::getClientIdByUid($v);
} // }
ds($uidsC); // ds($uidsC);
$data['message_type'] = MessageTypeEnum::from($type)->description(); $data['message_type'] = MessageTypeEnum::from($type)->description();
$this->gatewayClient->sendToUser( $this->gatewayClient->sendToUser(
$this->userId === 1? 2: 1, $this->userId === 1? 2: 1,
$this->userId, $this->userId,
$this->userInfo['nick_name'], $this->userInfo['nick_name'],
$data['avatarColor'],
$data['content'], $data['content'],
); );
$this->gatewayClient->broadcastMessage( $this->gatewayClient->broadcastMessage(
$this->userId, $this->userId,
$this->userInfo['nick_name'], $this->userInfo['nick_name'],
$data['avatarColor'],
$data['content'], $data['content'],
); );
return ['发送成功']; return ['发送成功'];