/**
  * 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();
     });
 }
 /**
  * Send message
  *
  * @param \Thruway\Message\Message $msg
  * @throws \Exception
  */
 public function sendMessage(Message $msg)
 {
     if ($this->getFarPeerTransport() === null) {
         throw new \Exception("You must set the farPeerTransport on internal client transports");
     }
     $this->farPeer->onMessage($this->getFarPeerTransport(), $msg);
 }
 /**
  * Triggered when a client sends data through the socket
  * @param  \Ratchet\ConnectionInterface $from The socket/connection that sent the message to your application
  * @param  string $msg The message received
  * @throws \Exception
  */
 function onMessage(ConnectionInterface $from, $msg)
 {
     $this->manager->debug("onMessage...({$msg})");
     /** @var TransportInterface $transport */
     $transport = $this->transports[$from];
     $this->peer->onMessage($transport, $transport->getSerializer()->deserialize($msg));
 }
 /**
  * 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);
 }
 public function startTransportProvider(AbstractPeer $peer, LoopInterface $loop)
 {
     // the peer that is passed into here is the server that our internal client connects to
     $this->peer = $peer;
     // create a new transport for the router side to use
     $transport = new InternalClientTransport($this->internalClient, $loop);
     // create a new transport for the client side to use
     $clientTransport = new InternalClientTransport($this->peer, $loop);
     // give the transports each other because they are going to call directly into the
     // other side
     $transport->setFarPeerTransport($clientTransport);
     $clientTransport->setFarPeerTransport($transport);
     // connect the transport to the Router/Peer
     $this->peer->onOpen($transport);
     // open the client side
     $this->internalClient->onOpen($clientTransport);
     // tell the internal client to start up
     $this->internalClient->start();
 }
 /**
  * Triggered when a client sends data through the socket
  *
  * @param  \Ratchet\ConnectionInterface $from The socket/connection that sent the message to your application
  * @param  string $msg The message received
  * @throws \Exception
  */
 public function onMessage(ConnectionInterface $from, $msg)
 {
     Logger::debug($this, "onMessage: ({$msg})");
     /** @var TransportInterface $transport */
     $transport = $this->transports[$from];
     try {
         $this->peer->onMessage($transport, $transport->getSerializer()->deserialize($msg));
     } catch (DeserializationException $e) {
         Logger::alert($this, "Deserialization exception occurred.");
     } catch (\Exception $e) {
         Logger::alert($this, "Exception occurred during onMessage: " . $e->getMessage());
     }
 }
Example #8
0
 /**
  * Handle process reveived data
  *
  * @param mixed $data
  * @return void
  */
 public function handleData($data)
 {
     //        if ($this->handshakeByte == 0) {
     //            $this->handshakeByte = $data[0];
     //            $data = substr($data, 1);
     //        }
     $this->buffer = $this->buffer . $data;
     $bufferLen = strlen($this->buffer);
     while ($bufferLen > 0 && $bufferLen >= $this->msgLen) {
         if ($this->msgLen == 0) {
             // the next 4 bytes are going to be the msglen
             if ($bufferLen >= 4) {
                 $this->msgLen = array_values(unpack("N", $this->buffer))[0];
                 if ($this->msgLen <= 0) {
                     Logger::error("Invalid message size sent");
                     $this->close();
                 }
                 // shift off the first 4 bytes
                 $bufferLen = $bufferLen - 4;
                 $this->buffer = substr($this->buffer, 4, $bufferLen);
             } else {
                 // we don't have enough to get the message length
                 return;
             }
         }
         if ($bufferLen >= $this->msgLen) {
             $msg = $this->getSerializer()->deserialize(substr($this->buffer, 0, $this->msgLen));
             $this->peer->onMessage($this, $msg);
             if ($bufferLen == $this->msgLen) {
                 $this->buffer = "";
                 $this->msgLen = 0;
                 $bufferLen = 0;
             } else {
                 $bufferLen = $bufferLen - $this->msgLen;
                 $this->buffer = substr($this->buffer, $this->msgLen, $bufferLen);
                 $this->msgLen = 0;
             }
         }
     }
 }
 /**
  * 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);
 }