测试
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
8
app/Services/GatewayWorker/ImService.php
Normal file
8
app/Services/GatewayWorker/ImService.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\GatewayWorker;
|
||||
|
||||
class ImService
|
||||
{
|
||||
|
||||
}
|
||||
@@ -8,7 +8,10 @@
|
||||
"require": {
|
||||
"php": "^8.2",
|
||||
"laravel/framework": "^12.0",
|
||||
"laravel/tinker": "^2.10.1"
|
||||
"laravel/tinker": "^2.10.1",
|
||||
"workerman/gateway-worker": "^4.0",
|
||||
"workerman/gatewayclient": "^3.1",
|
||||
"workerman/workerman": "^5.1"
|
||||
},
|
||||
"require-dev": {
|
||||
"fakerphp/faker": "^1.23",
|
||||
|
||||
8303
composer.lock
generated
Normal file
8303
composer.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
11
config/gateway.php
Normal file
11
config/gateway.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
/*
|
||||
* -----------服务地址-----------
|
||||
*/
|
||||
// 'register_address' => 'http://t-ws.nailaoyun.cn/register/'
|
||||
// 'register_address' => 't-ws.nailaoyun.cn/register/',
|
||||
'register_address' => '101.43.12.11:11238',
|
||||
// 'register_address' => '172.22.240.1:11238',
|
||||
];
|
||||
112
config/response.php
Executable file
112
config/response.php
Executable file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
|
||||
# 返回封装
|
||||
|
||||
/**
|
||||
* 请求成功返回
|
||||
* @param $data
|
||||
* @param $msg
|
||||
* @param $code
|
||||
* @return JsonResponse
|
||||
*/
|
||||
if ( !function_exists('jok') ) {
|
||||
function jok($data = [], $msg = '请求成功'): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'code' => 0,
|
||||
'message' => $msg,
|
||||
'result' => $data,
|
||||
'type' => 'success'
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求失败返回
|
||||
* @param $data
|
||||
* @param $msg
|
||||
* @param $code
|
||||
* @return JsonResponse
|
||||
*/
|
||||
if ( !function_exists('jerr') ) {
|
||||
#[NoReturn]
|
||||
function jerr($msg = '', $data = [], $code = 500): JsonResponse
|
||||
{
|
||||
exit(json_encode([
|
||||
'code' => $code,
|
||||
'message' => $msg,
|
||||
'result' => $data,
|
||||
'type' => 'error'
|
||||
], JSON_UNESCAPED_UNICODE, JSON_PARTIAL_OUTPUT_ON_ERROR));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 异常返回(他会阻止后面所有代码的运行)
|
||||
* @param $data
|
||||
* @param $msg
|
||||
* @param $code
|
||||
* @return JsonResponse
|
||||
*/
|
||||
if ( !function_exists('jInstanceof') ) {
|
||||
function jInstanceof($code = 500, $msg = '错误返回', $data = []): JsonResponse
|
||||
{
|
||||
|
||||
// 转换成秒
|
||||
$timeDifference = microtime(true) - LARAVEL_START;
|
||||
|
||||
return response()->json([
|
||||
'code' => $code,
|
||||
'message' => $msg,
|
||||
'result' => $data,
|
||||
'type' => 'error',
|
||||
'interface_info' => [
|
||||
'result_time' => $timeDifference < 1? round($timeDifference * 1000) . ' ms': number_format($timeDifference, 1) . ' s',
|
||||
'ecs' => env('ECS')
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试 (他会阻止后面所有代码的运行)
|
||||
* @param $data
|
||||
* @param $msg
|
||||
* @param $code
|
||||
* @return void
|
||||
*/
|
||||
if ( !function_exists('ds') ) {
|
||||
#[NoReturn]
|
||||
function ds($data = [], $msg = '测试返回'): void
|
||||
{
|
||||
exit(json_encode([
|
||||
'code' => 19522,
|
||||
'message' => $msg,
|
||||
'result' => $data,
|
||||
'type' => 'success'
|
||||
],JSON_UNESCAPED_SLASHES,JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 身份过期返回(他会阻止后面所有代码的运行)
|
||||
* @param $data
|
||||
* @param $msg
|
||||
* @param $code
|
||||
* @return void
|
||||
*/
|
||||
if ( !function_exists('jexpire') ) {
|
||||
#[NoReturn]
|
||||
function jexpire($msg = '身份过期', $data = []): void
|
||||
{
|
||||
exit(json_encode([
|
||||
'code' => 401,
|
||||
'message' => $msg,
|
||||
'result' => $data,
|
||||
'type' => 'jexpire'
|
||||
],JSON_UNESCAPED_SLASHES,JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
@@ -5,3 +5,9 @@ use Illuminate\Support\Facades\Route;
|
||||
Route::get('/', function () {
|
||||
return view('welcome');
|
||||
});
|
||||
Route::post('/b', function () {
|
||||
$userId = request()->post('user_id');
|
||||
$clientId = request()->post('client_id');
|
||||
\App\Services\GatewayWorker\GatewayClientService::getInstance()->init()->bindUser($clientId, $userId);
|
||||
return jok('绑定成功');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user