From 3bdba2c99375e4e2a8a33d469050afd526deed18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 12 Apr 2021 15:37:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HttpServer/Emit.php | 43 ++++++++----------------------------------- System/Snowflake.php | 2 +- 2 files changed, 9 insertions(+), 36 deletions(-) diff --git a/HttpServer/Emit.php b/HttpServer/Emit.php index 7f782e45..07f1fcb7 100644 --- a/HttpServer/Emit.php +++ b/HttpServer/Emit.php @@ -20,47 +20,20 @@ class Emit extends Component /** - * @param string $name + * @param int[] $users * @param string $message * @throws Exception */ - public function emit(string $name, string $message) + public function emit(array $users, string $message) { - $redis = Snowflake::app()->getRedis(); - if (!$redis->exists($name) || $redis->sCard($name) < 1) { - return; + $table = Snowflake::app()->getTable('SYSTEM:ONLINE:PEOPLES'); + + foreach ($users as $user) { + $fd = $table->get((string)$user, ['clientId']); + + Snowflake::push($fd, $message); } - $socket = Snowflake::app()->getSwoole(); - foreach ($redis->sMembers($name) as $value) { - $socket->push($value, $message); - } - } - - - /** - * @param string $name - * @param int $value - * @throws Exception - */ - public function register(string $name, int $value) - { - redis()->sAdd($name, $value); - } - - - /** - * @param string $name - * @param int|null $value - * @throws Exception - */ - public function clear(string $name, ?int $value = null) - { - if (!empty($value)) { - redis()->sRem($name, $value); - } else { - redis()->del($name); - } } diff --git a/System/Snowflake.php b/System/Snowflake.php index b890e99b..574bb73b 100644 --- a/System/Snowflake.php +++ b/System/Snowflake.php @@ -360,7 +360,7 @@ class Snowflake public static function push(int $fd, $data): mixed { $server = static::getWebSocket(); - if (empty($server)) { + if (empty($server) || !$server->isEstablished($fd)) { return false; } if (!is_string($data)) {