Пример #1
0
 /**
  * Start transport provider
  *
  * @param \Thruway\Peer\ClientInterface $client
  * @param \React\EventLoop\LoopInterface $loop
  */
 public function startTransportProvider(ClientInterface $client, LoopInterface $loop)
 {
     Logger::info($this, "Starting Transport");
     $this->client = $client;
     $this->loop = $loop;
     $this->connector = new 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());
         $this->client->onOpen($transport);
         $conn->on('message', function ($msg) use($transport) {
             Logger::debug($this, "Received: {$msg}");
             try {
                 $this->client->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->client->onClose('close');
         });
         $conn->on('pong', function ($frame, $ws) use($transport) {
             $transport->onPong($frame, $ws);
         });
     }, function ($e) {
         $this->client->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();
     });
 }