From 5c04064df1e1f453fa888581f9ee1fe4b1a5dd31 Mon Sep 17 00:00:00 2001 From: lq Date: Thu, 26 Jun 2025 10:21:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Services/Base/BaseService.php | 1 + app/Services/GatewayWorker/ImService.php | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/Services/Base/BaseService.php b/app/Services/Base/BaseService.php index 3cbae3b9..460097cf 100644 --- a/app/Services/Base/BaseService.php +++ b/app/Services/Base/BaseService.php @@ -8,6 +8,7 @@ class BaseService { protected $userId; protected $userInfo; + protected $gatewayClient; /** * @var mixed 单例实例,确保该类只有一个全局实例 */ diff --git a/app/Services/GatewayWorker/ImService.php b/app/Services/GatewayWorker/ImService.php index 8cbbc735..0a3d90f6 100644 --- a/app/Services/GatewayWorker/ImService.php +++ b/app/Services/GatewayWorker/ImService.php @@ -8,6 +8,12 @@ use App\Services\Base\BaseService; class ImService extends BaseService { + public function __construct() + { + parent::__construct(); + $this->gatewayClient = GatewayClientService::getInstance()->init(); + } + public function bindClientIdByUserId($clientId) { if (empty($clientId)) { @@ -22,12 +28,17 @@ class ImService extends BaseService public function sendMessage($data, $type = MessageTypeEnum::Text->value) { $data['message_type'] = MessageTypeEnum::from($type)->description(); - GatewayClientService::getInstance()->init()->sendToUser( + $this->gatewayClient->sendToUser( $this->userId === 1? 2: 1, $this->userId, $this->userInfo['nick_name'], $data['content'], ); + $this->gatewayClient->broadcastMessage( + $this->userId, + $this->userInfo['nick_name'], + $data['content'], + ); return ['发送成功']; } }