37 lines
877 B
PHP
37 lines
877 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
namespace App\Blog\Support;
|
|
|
|
use Workerman\Connection\TcpConnection;
|
|
|
|
/**
|
|
* Workerman WebSocket 广播器:任务执行中向后台推送进度。
|
|
*/
|
|
class WorkermanBroadcaster
|
|
{
|
|
/** @var array<int, TcpConnection> */
|
|
public static array $connections = [];
|
|
|
|
public static function add(TcpConnection $connection): void
|
|
{
|
|
self::$connections[spl_object_id($connection)] = $connection;
|
|
}
|
|
|
|
public static function remove(TcpConnection $connection): void
|
|
{
|
|
unset(self::$connections[spl_object_id($connection)]);
|
|
}
|
|
|
|
public static function send(string $event, array $data = []): void
|
|
{
|
|
$message = json_encode(['event' => $event, 'data' => $data], JSON_UNESCAPED_UNICODE);
|
|
|
|
foreach (self::$connections as $connection) {
|
|
$connection->send($message);
|
|
}
|
|
}
|
|
}
|