*/ protected $events = []; public function __construct(Response $connection, Request $request) { $this->connection = $connection; $this->connection->upgrade(); } public function on(string $event, callable $callback): void { $this->events[$event] = $callback; } public function start(): void { while (true) { $frame = $this->connection->recv(); if ($frame === false || $frame instanceof CloseFrame || $frame === '') { $callback = $this->events[static::ON_CLOSE]; $callback($this->connection, $this->connection->fd); break; } $callback = $this->events[static::ON_MESSAGE]; $callback($this->connection, $frame); } $this->connection = null; $this->events = []; } }