hyperf-chat/app/Cache/SocketUserBindFds.php

90 lines
2.4 KiB
PHP
Raw Permalink Normal View History

2021-05-22 14:47:46 +08:00
<?php
namespace App\Cache;
use App\Cache\Repository\SetGroupRedis;
/**
* :用户ID与客户端ID绑定(一对多关系)
*
* @package App\Cache
*/
class SocketUserBindFds extends SetGroupRedis
{
protected $name = 'ws:user-fds';
/**
* @param int $fd 客户端ID
* @param int $user_id 用户ID
* @param string $run_id 服务运行ID默认当前服务ID
* @return bool|int
*/
public function bind(int $fd, int $user_id, $run_id = SERVER_RUN_ID)
{
return $this->add($this->filter([$run_id, $user_id]), $fd);
}
/**
* @param int $fd 客户端ID
2021-08-20 22:38:46 +08:00
* @param int $user_id
2021-05-22 14:47:46 +08:00
* @param string $run_id 服务运行ID默认当前服务ID
* @return int
*/
2021-08-20 22:38:46 +08:00
public function unBind(int $fd, int $user_id, $run_id = SERVER_RUN_ID): int
2021-05-22 14:47:46 +08:00
{
return $this->rem($this->filter([$run_id, $user_id]), $fd);
}
/**
* 检测用户当前是否在线(指定运行服务器)
*
* @param int $user_id 用户ID
* @param string $run_id 服务运行ID默认当前服务ID
* @return bool
*/
public function isOnline(int $user_id, $run_id = SERVER_RUN_ID): bool
{
return (bool)$this->count($this->filter([$run_id, $user_id]));
}
/**
* 检测用户当前是否在线(查询所有在线服务器)
*
* @param int $user_id 用户ID
* @param array $run_ids 服务运行ID默认当前服务ID
* @return bool
*/
public function isOnlineAll(int $user_id, array $run_ids = []): bool
{
$run_ids = $run_ids ?: ServerRunID::getInstance()->getServerRunIdAll();
foreach ($run_ids as $run_id => $time) {
if ($this->isOnline($user_id, $run_id)) return true;
}
return false;
}
/**
* 查询用户的客户端fd集合(用户可能存在多端登录)
*
* @param int $user_id 用户ID
* @param string $run_id 服务运行ID默认当前服务ID
* @return array
*/
2021-05-22 20:54:30 +08:00
public function findFds(int $user_id, $run_id = SERVER_RUN_ID): array
2021-05-22 14:47:46 +08:00
{
$arr = $this->all($this->filter([$run_id, $user_id]));
foreach ($arr as $k => $value) {
$arr[$k] = intval($value);
}
return $arr;
}
2021-05-22 20:54:30 +08:00
public function getCachePrefix(string $run_id): string
2021-05-22 14:47:46 +08:00
{
return $this->getCacheKey($run_id);
}
}