优化代码

main
gzydong 2021-05-22 20:54:30 +08:00
parent 08618ee3f3
commit 67c0e0adee
10 changed files with 27 additions and 16 deletions

View File

@ -14,7 +14,6 @@ namespace App\Amqp\Producer;
use App\Constants\SocketConstants;
use Hyperf\Amqp\Message\ProducerMessage;
use Hyperf\Amqp\Message\Type;
use Hyperf\Utils\Str;
/**
* 消息生产者

View File

@ -33,7 +33,7 @@ class FriendRemark extends HashRedis
* @param int $friend_id 好友ID
* @return string
*/
public function read(int $user_id, int $friend_id)
public function read(int $user_id, int $friend_id): string
{
return $this->get($this->_flag($user_id, $friend_id));
}
@ -45,7 +45,7 @@ class FriendRemark extends HashRedis
* @param int $friend_id 好友ID
* @return string
*/
private function _flag(int $user_id, int $friend_id)
private function _flag(int $user_id, int $friend_id): string
{
return "{$user_id}_{$friend_id}";
}

View File

@ -34,7 +34,7 @@ class LastMessage extends HashRedis
* @param int $receive 接收者ID
* @return array
*/
public function read(int $type, int $sender, int $receive)
public function read(int $type, int $sender, int $receive): array
{
$message = $this->get($this->flag($type, $sender, $receive));

View File

@ -4,6 +4,11 @@ namespace App\Cache;
use App\Cache\Repository\HashRedis;
/**
* 服务运行ID - 缓存助手
*
* @package App\Cache
*/
class ServerRunID extends HashRedis
{
protected $prefix = 'SERVER_RUN_ID';
@ -21,7 +26,7 @@ class ServerRunID extends HashRedis
* @param int $type 获取类型[1:正在运行;2:已超时;3:所有]
* @return array
*/
public function getServerRunIdAll(int $type = 1)
public function getServerRunIdAll(int $type = 1): array
{
$arr = $this->all();

View File

@ -44,7 +44,7 @@ class SocketFdBindUser extends HashGroupRedis
* @param string $run_id 服务运行ID默认当前服务ID
* @return int
*/
public function findUserId(int $fd, $run_id = SERVER_RUN_ID)
public function findUserId(int $fd, $run_id = SERVER_RUN_ID): int
{
return (int)$this->get($run_id, strval($fd)) ?: 0;
}

View File

@ -4,6 +4,11 @@ namespace App\Cache;
use App\Cache\Repository\SetGroupRedis;
/**
* 聊天室 - 缓存助手
*
* @package App\Cache
*/
class SocketRoom extends SetGroupRedis
{
protected $name = 'ws:room';

View File

@ -71,7 +71,7 @@ class SocketUserBindFds extends SetGroupRedis
* @param string $run_id 服务运行ID默认当前服务ID
* @return array
*/
public function findFds(int $user_id, $run_id = SERVER_RUN_ID)
public function findFds(int $user_id, $run_id = SERVER_RUN_ID): array
{
$arr = $this->all($this->filter([$run_id, $user_id]));
foreach ($arr as $k => $value) {
@ -81,7 +81,7 @@ class SocketUserBindFds extends SetGroupRedis
return $arr;
}
public function getCachePrefix(string $run_id)
public function getCachePrefix(string $run_id): string
{
return $this->getCacheKey($run_id);
}

View File

@ -31,7 +31,7 @@ class UnreadTalk extends HashRedis
* @param int $receive 接收者ID
* @return int
*/
public function read(int $sender, int $receive)
public function read(int $sender, int $receive): int
{
return (int)$this->get($this->flag($sender, $receive));
}
@ -63,7 +63,7 @@ class UnreadTalk extends HashRedis
* @param int $user_id 用户ID
* @return array
*/
public function reads(int $user_id)
public function reads(int $user_id): array
{
$iterator = null;
$arr = [];

View File

@ -70,10 +70,12 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos
{
$token = $request->get['token'] ?? '';
$userInfo = $this->jwt->getParserData($token);
$userInfo['user_id'] = intval($userInfo['user_id']);
stdout_log()->notice("用户连接信息 : user_id:{$userInfo['user_id']} | fd:{$request->fd} 时间:" . date('Y-m-d H:i:s'));
// 判断是否存在异地登录
$isOnline = $this->socketClientService->isOnlineAll(intval($userInfo['user_id']));
$isOnline = $this->socketClientService->isOnlineAll($userInfo['user_id']);
// 若开启单点登录,则主动关闭之前登录的连接
if ($isOnline) {
@ -129,7 +131,7 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos
*/
public function onClose($server, int $fd, int $reactorId): void
{
$user_id = (int)$this->socketClientService->findFdUserId($fd);
$user_id = $this->socketClientService->findFdUserId($fd);
stdout_log()->notice("客户端FD:{$fd} 已关闭连接 用户ID为【{$user_id}】,关闭时间:" . date('Y-m-d H:i:s'));

View File

@ -58,7 +58,7 @@ class SocketClientService
* @param array $run_ids 服务运行ID
* @return bool
*/
public function isOnlineAll(int $user_id, array $run_ids = [])
public function isOnlineAll(int $user_id, array $run_ids = []): bool
{
return SocketUserBindFds::getInstance()->isOnlineAll($user_id, $run_ids);
}
@ -70,7 +70,7 @@ class SocketClientService
* @param string $run_id 服务运行ID默认当前服务ID
* @return int
*/
public function findFdUserId(int $fd, $run_id = SERVER_RUN_ID)
public function findFdUserId(int $fd, $run_id = SERVER_RUN_ID): int
{
return SocketFdBindUser::getInstance()->findUserId($fd, $run_id);
}