测试
This commit is contained in:
125
app/Services/GatewayWorker/GatewayClientService.php
Normal file
125
app/Services/GatewayWorker/GatewayClientService.php
Normal file
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\GatewayWorker;
|
||||
|
||||
use GatewayClient\Gateway;
|
||||
|
||||
class GatewayClientService
|
||||
{
|
||||
|
||||
// 单例
|
||||
private static $instance;
|
||||
public static function getInstance(): self
|
||||
{
|
||||
if (!self::$instance) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function init()
|
||||
{
|
||||
// ds(Gateway::$registerAddress = config('gateway.register_address'));
|
||||
Gateway::$registerAddress = config('gateway.register_address');
|
||||
ds(Gateway::getAllUidCount());
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定用户ID和客户端ID
|
||||
*/
|
||||
public function bindUser(string $clientId, int $userId): void
|
||||
{
|
||||
Gateway::bindUid($clientId, $userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户ID获取客户端ID列表
|
||||
*/
|
||||
public function getClientIdsByUserId(int $userId): array
|
||||
{
|
||||
return Gateway::getClientIdByUid($userId) ?: [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 加入聊天(绑定用户并可选加入分组)
|
||||
*/
|
||||
public function joinChat(string $clientId, int $userId, ?string $group = null): void
|
||||
{
|
||||
$this->bindUser($clientId, $userId);
|
||||
|
||||
if ($group) {
|
||||
Gateway::joinGroup($clientId, $group);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建标准化消息
|
||||
*/
|
||||
protected function buildMessage(int $userId, string $name, string $text, string $type = 'received'): string
|
||||
{
|
||||
$message = [
|
||||
'userId' => $userId,
|
||||
'name' => $name,
|
||||
'text' => $text,
|
||||
'timestamp' => date('Y-m-d\TH:i:sP'), // ISO 8601格式时间
|
||||
'type' => $type
|
||||
];
|
||||
|
||||
return json_encode($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送消息给指定用户
|
||||
*/
|
||||
public function sendToUser(int $toUserId, int $fromUserId, string $fromUserName, string $text): void
|
||||
{
|
||||
$message = $this->buildMessage($fromUserId, $fromUserName, $text);
|
||||
Gateway::sendToUid($toUserId, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 广播消息给所有用户(可排除指定用户)
|
||||
*/
|
||||
public function broadcastMessage(int $fromUserId, string $fromUserName, string $text, ?int $excludeUserId = null): void
|
||||
{
|
||||
$message = $this->buildMessage($fromUserId, $fromUserName, $text);
|
||||
|
||||
$excludeList = $excludeUserId ? [$excludeUserId] : [];
|
||||
Gateway::sendToAll($message, null, $excludeList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送消息到指定分组
|
||||
*/
|
||||
public function sendToGroup(string $group, int $fromUserId, string $fromUserName, string $text, ?int $excludeUserId = null): void
|
||||
{
|
||||
$message = $this->buildMessage($fromUserId, $fromUserName, $text);
|
||||
|
||||
$excludeClientIds = [];
|
||||
if ($excludeUserId) {
|
||||
$excludeClientIds = $this->getClientIdsByUserId($excludeUserId);
|
||||
}
|
||||
|
||||
Gateway::sendToGroup($group, $message, $excludeClientIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送系统消息
|
||||
*/
|
||||
public function sendSystemMessage(string $text, $target = null): void
|
||||
{
|
||||
$message = $this->buildMessage(0, 'System', $text, 'system');
|
||||
|
||||
if (is_int($target)) {
|
||||
// 发送给特定用户
|
||||
Gateway::sendToUid($target, $message);
|
||||
} elseif (is_string($target)) {
|
||||
// 发送到分组
|
||||
Gateway::sendToGroup($target, $message);
|
||||
} else {
|
||||
// 全局广播
|
||||
Gateway::sendToAll($message);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user