BF broadcasting

This commit is contained in:
a6a2f5842 2025-09-13 19:48:59 +02:00
parent 5c7d6a1c1b
commit 6031cdf9c5
2 changed files with 27 additions and 42 deletions

View File

@ -136,37 +136,6 @@ class Controller
]));
}
final public function broadcast(
mixed $payload,
?string $event = null,
?string $channel = null,
bool $including_self = false
) : void {
if (! $this->channel) {
$this->error('Channel not found');
return;
}
foreach ($this->channel->getConnections() as $channel_conection) {
if ($channel_conection !== $this->connection) {
$channel_conection->send(json_encode([
'event' => ($event ?? $this->event),
'data' => $payload,
'channel' => $channel ?? $this->channel->getName(),
]));
}
if ($including_self) {
$this->connection->send(json_encode([
'event' => ($event ?? $this->event),
'data' => $payload,
'channel' => $channel ?? $this->channel->getName(),
]));
}
}
}
final public function progress(
mixed $payload = null,
?string $event = null,
@ -268,7 +237,7 @@ class Controller
return true;
}
final public function broadcasting(
final public function broadcast(
array|string|null $payload = null,
?string $event = null,
?string $channel = null,
@ -280,16 +249,30 @@ class Controller
];
}
$channel ??= ($this->channel ? $this->channel->getName() : null);
$p = [
'event' => ($event ?? $this->event),
'data' => $payload,
'channel' => $channel ?? ($this->channel ? $this->channel->getName() : null),
'channel' => $channel,
];
if (get_class($this->connection) !== MockConnection::class) {
throw new \Exception('This method is only available in async mode');
if (! $channel) {
$this->error('Channel not found');
return;
}
foreach ($this->channel->getConnections() as $channel_conection) {
if ($channel_conection !== $this->connection) {
$channel_conection->send(json_encode($p));
}
if ($including_self) {
$this->connection->send(json_encode($p));
}
}
}else{
$connection = clone $this->connection;
$connection->broadcast(
$p,
@ -298,6 +281,8 @@ class Controller
);
}
}
protected static function get_uniquifyer($event)
{
preg_match('/[\[].*[\]]/', $event, $matches);

View File

@ -562,7 +562,7 @@ class Handler implements MessageComponentInterface
$channel_conection->send(json_encode([
'event' => ($event ?? $event),
'data' => $payload,
'channel' => $channel ?? $channel->getName(),
'channel' => $channel->getName(),
]));
}
@ -570,7 +570,7 @@ class Handler implements MessageComponentInterface
$connection->send(json_encode([
'event' => ($event ?? $event),
'data' => $payload,
'channel' => $channel ?? $channel->getName(),
'channel' => $channel->getName(),
]));
}
}