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 = [
'userId' => $userId,
'userName' => $name,
'avatarColor' => $avatarColor,
'content' => $text,
'timestamp' => time(), // ISO 8601格式时间
'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);
}
/**
* 广播消息给所有用户(可排除指定用户)
*/
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] : [];
Gateway::sendToAll($message, null, $excludeList);

View File

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