Files
im-demo-server/app/Services/GatewayWorker/ImService.php

45 lines
1.1 KiB
PHP
Raw Normal View History

2025-06-25 14:09:34 +08:00
<?php
namespace App\Services\GatewayWorker;
2025-06-26 09:56:14 +08:00
use App\Enum\MessageTypeEnum;
2025-06-25 16:12:51 +08:00
use App\Services\Base\BaseService;
class ImService extends BaseService
2025-06-25 14:09:34 +08:00
{
2025-06-26 10:21:45 +08:00
public function __construct()
{
parent::__construct();
$this->gatewayClient = GatewayClientService::getInstance()->init();
}
2025-06-25 16:12:51 +08:00
public function bindClientIdByUserId($clientId)
{
if (empty($clientId)) {
throw new \Exception('clientId不能为空');
}
GatewayClientService::getInstance()->init()->bindUser($clientId, $this->userId);
return ['绑定成功'];
}
2025-06-26 09:56:14 +08:00
public function sendMessage($data, $type = MessageTypeEnum::Text->value)
{
2025-06-26 10:05:04 +08:00
$data['message_type'] = MessageTypeEnum::from($type)->description();
2025-06-26 10:21:45 +08:00
$this->gatewayClient->sendToUser(
2025-06-26 09:56:14 +08:00
$this->userId === 1? 2: 1,
2025-06-26 10:13:19 +08:00
$this->userId,
2025-06-26 10:10:02 +08:00
$this->userInfo['nick_name'],
2025-06-26 10:17:21 +08:00
$data['content'],
2025-06-26 09:56:14 +08:00
);
2025-06-26 10:21:45 +08:00
$this->gatewayClient->broadcastMessage(
$this->userId,
$this->userInfo['nick_name'],
$data['content'],
);
2025-06-26 09:56:14 +08:00
return ['发送成功'];
}
2025-06-25 14:09:34 +08:00
}