/** * Start transport provider * * @param \Thruway\Peer\AbstractPeer $peer * @param \React\EventLoop\LoopInterface $loop */ public function startTransportProvider(AbstractPeer $peer, LoopInterface $loop) { Logger::info($this, "Starting Transport"); $this->peer = $peer; $this->loop = $loop; $this->connector = new \Ratchet\Client\Factory($this->loop); $this->connector->__invoke($this->URL, ['wamp.2.json'])->then(function (WebSocket $conn) { Logger::info($this, "Pawl has connected"); $transport = new PawlTransport($conn, $this->loop); $transport->setSerializer(new JsonSerializer()); $transport->setTrusted($this->trusted); $this->peer->onOpen($transport); $conn->on('message', function ($msg) use($transport) { Logger::debug($this, "Received: {$msg}"); try { $this->peer->onMessage($transport, $transport->getSerializer()->deserialize($msg)); } catch (DeserializationException $e) { Logger::warning($this, "Deserialization exception occurred."); } catch (\Exception $e) { Logger::warning($this, "Exception occurred during onMessage: " . $e->getMessage()); } }); $conn->on('close', function ($conn) { Logger::info($this, "Pawl has closed"); $this->peer->onClose('close'); }); $conn->on('pong', function ($frame, $ws) use($transport) { $transport->onPong($frame, $ws); }); }, function ($e) { $this->peer->onClose('unreachable'); Logger::info($this, "Could not connect: {$e->getMessage()}"); // $this->loop->stop(); }); }
/** * */ public function startTransportProvider(AbstractPeer $peer, LoopInterface $loop) { $this->manager->info("Starting Transport\n"); $this->peer = $peer; $this->loop = $loop; $this->connector = new \Ratchet\Client\Factory($this->loop); $this->connector->__invoke($this->URL, ['wamp.2.json'])->then(function (WebSocket $conn) { $this->manager->info("Pawl has connected\n"); $transport = new PawlTransport($conn, $this->loop); $transport->setSerializer(new JsonSerializer()); $this->peer->onOpen($transport); $conn->on('message', function ($msg) use($transport) { $this->manager->info("Received: {$msg}\n"); $this->peer->onMessage($transport, $transport->getSerializer()->deserialize($msg)); }); $conn->on('close', function ($conn) { $this->manager->info("Pawl has closed\n"); $this->peer->onClose('close'); }); $conn->on('pong', function ($frame, $ws) use($transport) { $transport->onPong($frame, $ws); }); }, function ($e) { $this->peer->onClose('unreachable'); $this->manager->info("Could not connect: {$e->getMessage()}\n"); // $this->loop->stop(); }); }
/** * Handle process on close transport * * @param \React\Socket\Connection $conn */ public function handleClose(Connection $conn) { Logger::debug($this, "Raw socket closed " . $conn->getRemoteAddress()); $transport = $this->transports[$conn]; $this->transports->detach($conn); $this->peer->onClose($transport); }
/** * This is called before or after a socket is closed (depends on how it's closed). * SendMessage to $conn will not result in an error if it has already been closed. * * @param \Ratchet\ConnectionInterface $conn The socket/connection that is closing/closed * @throws \Exception */ public function onClose(ConnectionInterface $conn) { /* @var $transport RatchetTransport */ $transport = $this->transports[$conn]; $this->transports->detach($conn); $this->peer->onClose($transport); Logger::info($this, "Ratchet has closed"); }
/** * This is called before or after a socket is closed (depends on how it's closed). SendMessage to $conn will not result in an error if it has already been closed. * @param ConnectionInterface $conn The socket/connection that is closing/closed * @throws \Exception */ function onClose(ConnectionInterface $conn) { /* @var $transport RatchetTransport */ $transport = $this->transports[$conn]; $this->transports->detach($conn); $this->peer->onClose($transport); $this->manager->debug("onClose..."); }
/** * Handle process on close connection * * @param \React\Stream\Stream $conn */ public function handleClose(Stream $conn) { //$this->getManager()->debug("Raw socket closed " . $conn->getRemoteAddress()); $this->peer->onClose($this->transport); }