优化代码
parent
08618ee3f3
commit
67c0e0adee
|
@ -14,7 +14,6 @@ namespace App\Amqp\Producer;
|
||||||
use App\Constants\SocketConstants;
|
use App\Constants\SocketConstants;
|
||||||
use Hyperf\Amqp\Message\ProducerMessage;
|
use Hyperf\Amqp\Message\ProducerMessage;
|
||||||
use Hyperf\Amqp\Message\Type;
|
use Hyperf\Amqp\Message\Type;
|
||||||
use Hyperf\Utils\Str;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 消息生产者
|
* 消息生产者
|
||||||
|
|
|
@ -33,7 +33,7 @@ class FriendRemark extends HashRedis
|
||||||
* @param int $friend_id 好友ID
|
* @param int $friend_id 好友ID
|
||||||
* @return string
|
* @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));
|
return $this->get($this->_flag($user_id, $friend_id));
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,7 @@ class FriendRemark extends HashRedis
|
||||||
* @param int $friend_id 好友ID
|
* @param int $friend_id 好友ID
|
||||||
* @return string
|
* @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}";
|
return "{$user_id}_{$friend_id}";
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,7 @@ class LastMessage extends HashRedis
|
||||||
* @param int $receive 接收者ID
|
* @param int $receive 接收者ID
|
||||||
* @return array
|
* @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));
|
$message = $this->get($this->flag($type, $sender, $receive));
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,11 @@ namespace App\Cache;
|
||||||
|
|
||||||
use App\Cache\Repository\HashRedis;
|
use App\Cache\Repository\HashRedis;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 服务运行ID - 缓存助手
|
||||||
|
*
|
||||||
|
* @package App\Cache
|
||||||
|
*/
|
||||||
class ServerRunID extends HashRedis
|
class ServerRunID extends HashRedis
|
||||||
{
|
{
|
||||||
protected $prefix = 'SERVER_RUN_ID';
|
protected $prefix = 'SERVER_RUN_ID';
|
||||||
|
@ -21,7 +26,7 @@ class ServerRunID extends HashRedis
|
||||||
* @param int $type 获取类型[1:正在运行;2:已超时;3:所有]
|
* @param int $type 获取类型[1:正在运行;2:已超时;3:所有]
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getServerRunIdAll(int $type = 1)
|
public function getServerRunIdAll(int $type = 1): array
|
||||||
{
|
{
|
||||||
$arr = $this->all();
|
$arr = $this->all();
|
||||||
|
|
||||||
|
|
|
@ -44,7 +44,7 @@ class SocketFdBindUser extends HashGroupRedis
|
||||||
* @param string $run_id 服务运行ID(默认当前服务ID)
|
* @param string $run_id 服务运行ID(默认当前服务ID)
|
||||||
* @return int
|
* @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;
|
return (int)$this->get($run_id, strval($fd)) ?: 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,11 @@ namespace App\Cache;
|
||||||
|
|
||||||
use App\Cache\Repository\SetGroupRedis;
|
use App\Cache\Repository\SetGroupRedis;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 聊天室 - 缓存助手
|
||||||
|
*
|
||||||
|
* @package App\Cache
|
||||||
|
*/
|
||||||
class SocketRoom extends SetGroupRedis
|
class SocketRoom extends SetGroupRedis
|
||||||
{
|
{
|
||||||
protected $name = 'ws:room';
|
protected $name = 'ws:room';
|
||||||
|
|
|
@ -71,7 +71,7 @@ class SocketUserBindFds extends SetGroupRedis
|
||||||
* @param string $run_id 服务运行ID(默认当前服务ID)
|
* @param string $run_id 服务运行ID(默认当前服务ID)
|
||||||
* @return array
|
* @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]));
|
$arr = $this->all($this->filter([$run_id, $user_id]));
|
||||||
foreach ($arr as $k => $value) {
|
foreach ($arr as $k => $value) {
|
||||||
|
@ -81,7 +81,7 @@ class SocketUserBindFds extends SetGroupRedis
|
||||||
return $arr;
|
return $arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCachePrefix(string $run_id)
|
public function getCachePrefix(string $run_id): string
|
||||||
{
|
{
|
||||||
return $this->getCacheKey($run_id);
|
return $this->getCacheKey($run_id);
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,7 @@ class UnreadTalk extends HashRedis
|
||||||
* @param int $receive 接收者ID
|
* @param int $receive 接收者ID
|
||||||
* @return int
|
* @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));
|
return (int)$this->get($this->flag($sender, $receive));
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ class UnreadTalk extends HashRedis
|
||||||
* @param int $user_id 用户ID
|
* @param int $user_id 用户ID
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function reads(int $user_id)
|
public function reads(int $user_id): array
|
||||||
{
|
{
|
||||||
$iterator = null;
|
$iterator = null;
|
||||||
$arr = [];
|
$arr = [];
|
||||||
|
|
|
@ -70,10 +70,12 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos
|
||||||
{
|
{
|
||||||
$token = $request->get['token'] ?? '';
|
$token = $request->get['token'] ?? '';
|
||||||
$userInfo = $this->jwt->getParserData($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'));
|
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) {
|
if ($isOnline) {
|
||||||
|
@ -129,7 +131,7 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos
|
||||||
*/
|
*/
|
||||||
public function onClose($server, int $fd, int $reactorId): void
|
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'));
|
stdout_log()->notice("客户端FD:{$fd} 已关闭连接 ,用户ID为【{$user_id}】,关闭时间:" . date('Y-m-d H:i:s'));
|
||||||
|
|
||||||
|
|
|
@ -58,7 +58,7 @@ class SocketClientService
|
||||||
* @param array $run_ids 服务运行ID
|
* @param array $run_ids 服务运行ID
|
||||||
* @return bool
|
* @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);
|
return SocketUserBindFds::getInstance()->isOnlineAll($user_id, $run_ids);
|
||||||
}
|
}
|
||||||
|
@ -70,7 +70,7 @@ class SocketClientService
|
||||||
* @param string $run_id 服务运行ID(默认当前服务ID)
|
* @param string $run_id 服务运行ID(默认当前服务ID)
|
||||||
* @return int
|
* @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);
|
return SocketFdBindUser::getInstance()->findUserId($fd, $run_id);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue