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( final public function progress(
mixed $payload = null, mixed $payload = null,
?string $event = null, ?string $event = null,
@ -268,7 +237,7 @@ class Controller
return true; return true;
} }
final public function broadcasting( final public function broadcast(
array|string|null $payload = null, array|string|null $payload = null,
?string $event = null, ?string $event = null,
?string $channel = null, ?string $channel = null,
@ -280,22 +249,38 @@ class Controller
]; ];
} }
$channel ??= ($this->channel ? $this->channel->getName() : null);
$p = [ $p = [
'event' => ($event ?? $this->event), 'event' => ($event ?? $this->event),
'data' => $payload, 'data' => $payload,
'channel' => $channel ?? ($this->channel ? $this->channel->getName() : null), 'channel' => $channel,
]; ];
if (get_class($this->connection) !== MockConnection::class) { 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,
$channel,
$including_self
);
} }
$connection = clone $this->connection;
$connection->broadcast(
$p,
$channel,
$including_self
);
} }
protected static function get_uniquifyer($event) protected static function get_uniquifyer($event)

View File

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