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

54 lines
1.4 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;
2025-06-26 10:30:49 +08:00
use GatewayClient\Gateway;
2025-06-25 16:12:51 +08:00
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:51:10 +08:00
// $uids = [1,2,3];
// $uidsC = [];
// foreach ($uids as $v) {
// $uidsC[$v] = Gateway::getClientIdByUid($v);
// }
// ds($uidsC);
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:51:10 +08:00
$data['avatarColor'],
2025-06-26 10:51:48 +08:00
$this->userInfo['nick_name'],
2025-06-26 11:00:50 +08:00
'单聊'. $data['content'],
2025-06-26 09:56:14 +08:00
);
2025-06-26 11:02:57 +08:00
// $this->gatewayClient->broadcastMessage(
// $this->userId,
// $data['avatarColor'],
// $this->userInfo['nick_name'],
// '群发'. $data['content'],
// );
2025-06-26 09:56:14 +08:00
return ['发送成功'];
}
2025-06-25 14:09:34 +08:00
}