clients = $storage; } protected $dispatcher; public function setDispatcher(Dispatcher $dispatcher) { $this->dispatcher = $dispatcher; return $this; } protected $request_builder; public function setRequestBuilder(RequestBuilder $builder) { $this->request_builder = $builder; return $this; } public function onOpen(Connection $conn) { $this->clients->attach($conn); } public function onMessage(Connection $from, $msg) { foreach ($this->clients as $client) { if ($client != $from) { continue; } $request = $this->request_builder->build($msg); $response = $this->dispatcher->dispatch($request); $from->send($response); } } public function onClose(Connection $conn) { $this->clients->detach($conn); } public function onError(Connection $conn, \Exception $e) { $conn->send(json_encode($e)); $conn->close(); } }