2020-11-07 22:57:10 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Service;
|
|
|
|
|
|
2021-05-22 14:47:46 +08:00
|
|
|
|
use App\Cache\SocketFdBindUser;
|
|
|
|
|
use App\Cache\SocketUserBindFds;
|
2020-11-07 22:57:10 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Socket客户端ID服务
|
|
|
|
|
*
|
|
|
|
|
* @package App\Service
|
|
|
|
|
*/
|
2020-11-29 14:44:11 +08:00
|
|
|
|
class SocketClientService
|
2020-11-07 22:57:10 +08:00
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* 客户端fd与用户ID绑定关系
|
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
|
* @param int $fd 客户端fd
|
|
|
|
|
* @param int $user_id 用户ID
|
|
|
|
|
* @param string $run_id 服务运行ID(默认当前服务ID)
|
2020-11-07 22:57:10 +08:00
|
|
|
|
*/
|
|
|
|
|
public function bindRelation(int $fd, int $user_id, $run_id = SERVER_RUN_ID)
|
|
|
|
|
{
|
2021-05-22 14:47:46 +08:00
|
|
|
|
SocketFdBindUser::getInstance()->bind($fd, $user_id, $run_id);
|
|
|
|
|
SocketUserBindFds::getInstance()->bind($fd, $user_id, $run_id);
|
2020-11-07 22:57:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 解除指定的客户端fd与用户绑定关系
|
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
|
* @param int $fd 客户端ID
|
2020-11-07 22:57:10 +08:00
|
|
|
|
* @param string $run_id 服务运行ID(默认当前服务ID)
|
|
|
|
|
*/
|
|
|
|
|
public function removeRelation(int $fd, $run_id = SERVER_RUN_ID)
|
|
|
|
|
{
|
2021-05-22 14:47:46 +08:00
|
|
|
|
$user_id = $this->findFdUserId($fd);
|
2020-11-07 22:57:10 +08:00
|
|
|
|
|
2021-05-22 14:47:46 +08:00
|
|
|
|
SocketFdBindUser::getInstance()->unBind($fd, $run_id);
|
|
|
|
|
SocketUserBindFds::getInstance()->unBind($fd, $user_id, $run_id);
|
2020-11-07 22:57:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-11-08 17:10:05 +08:00
|
|
|
|
* 检测用户当前是否在线(指定运行服务器)
|
2020-11-07 22:57:10 +08:00
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
|
* @param int $user_id 用户ID
|
|
|
|
|
* @param string $run_id 服务运行ID(默认当前服务ID)
|
2020-11-07 22:57:10 +08:00
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isOnline(int $user_id, $run_id = SERVER_RUN_ID): bool
|
|
|
|
|
{
|
2021-05-22 14:47:46 +08:00
|
|
|
|
return SocketUserBindFds::getInstance()->isOnline($user_id, $run_id);
|
2020-11-07 22:57:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-08 17:10:05 +08:00
|
|
|
|
/**
|
|
|
|
|
* 检测用户当前是否在线(查询所有在线服务器)
|
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
|
* @param int $user_id 用户ID
|
2020-11-08 17:10:05 +08:00
|
|
|
|
* @param array $run_ids 服务运行ID
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isOnlineAll(int $user_id, array $run_ids = [])
|
|
|
|
|
{
|
2021-05-22 14:47:46 +08:00
|
|
|
|
return SocketUserBindFds::getInstance()->isOnlineAll($user_id, $run_ids);
|
2020-11-08 17:10:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-07 22:57:10 +08:00
|
|
|
|
/**
|
|
|
|
|
* 查询客户端fd对应的用户ID
|
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
|
* @param int $fd 客户端ID
|
2020-11-07 22:57:10 +08:00
|
|
|
|
* @param string $run_id 服务运行ID(默认当前服务ID)
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function findFdUserId(int $fd, $run_id = SERVER_RUN_ID)
|
|
|
|
|
{
|
2021-05-22 14:47:46 +08:00
|
|
|
|
return SocketFdBindUser::getInstance()->findUserId($fd, $run_id);
|
2020-11-07 22:57:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 查询用户的客户端fd集合(用户可能存在多端登录)
|
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
|
* @param int $user_id 用户ID
|
|
|
|
|
* @param string $run_id 服务运行ID(默认当前服务ID)
|
2020-11-08 22:58:17 +08:00
|
|
|
|
* @return array
|
2020-11-07 22:57:10 +08:00
|
|
|
|
*/
|
|
|
|
|
public function findUserFds(int $user_id, $run_id = SERVER_RUN_ID)
|
|
|
|
|
{
|
2021-05-22 14:47:46 +08:00
|
|
|
|
return SocketUserBindFds::getInstance()->findFds($user_id, $run_id);
|
2020-11-07 22:57:10 +08:00
|
|
|
|
}
|
2020-11-08 17:10:05 +08:00
|
|
|
|
}
|