Beispiel #1
0
 /**
  * @param StackTransport $transport
  */
 public function listenTo(StackTransport $transport)
 {
     $self = $this;
     $transport->on("message", function (RemoteEventMessage $message) use($transport, $self) {
         $room = $message->getRoom();
         if (!$room) {
             return;
         }
         $event = $message->getEvent();
         if ($message->getEvent() == 'subscribe') {
             $self->room($room)->subscribe($transport);
             // If the transport is disconnected, make sure we 'fake' the unsubscribe
             $transport->getWebSocketTransport()->on("close", function () use($self, $transport, $room) {
                 $self->room($room)->unsubscribe($transport);
                 // Fake unsubscribe message
                 $message = new RemoteEventMessage();
                 $message->setEvent("unsubscribe");
                 $message->setRoom($room);
                 $self->emit("unsubscribe", [$transport, $message]);
                 $self->room($room)->emit("unsubscribe", [$transport, $message]);
             });
         } elseif ($message->getEvent() == 'unsubscribe') {
             $self->room($room)->unsubscribe($transport);
         }
         $self->room($room)->emit($message->getEvent(), [$transport, $message]);
         $self->emit($event, [$transport, $message]);
     });
 }
 public function remoteEmit($event, $data)
 {
     foreach ($this->getMembers() as $member) {
         $message = RemoteEventMessage::create($this->name, $event, $data);
         $member->getTopTransport()->send($message);
     }
 }
Beispiel #3
0
 /**
  * Notify everyone when a user has joined the chat
  *
  * @param StackTransport $stackTransport
  */
 public function onConnect(WebSocketTransportInterface $transport)
 {
     /**
      * @var $stackTransport StackTransport
      * @var $jsonTransport RemoteEventTransport
      */
     $logger = $this->logger;
     $loop = $this->loop;
     $stackTransport = StackTransport::create($transport, array(function (TransportInterface $carrier) use($loop, $logger) {
         return new RemoteEventTransport($carrier, $loop, $logger);
     }));
     $jsonTransport = $stackTransport->getTopTransport();
     $server = $transport->getHandshakeResponse()->getHeaders()->get('X-WebSocket-Server')->getFieldValue();
     $greetingMessage = RemoteEventMessage::create(null, "greeting", "hello world from {$server}!");
     $jsonTransport->whenResponseTo($greetingMessage, 0.1)->then(function (RemoteEventMessage $result) use($logger, $server) {
         $logger->notice(sprintf("Got '%s' in response to 'hello world from {$server}!'", $result->getData()));
     });
     $jsonTransport->remoteEvent()->on("greeting", function (RemoteEventMessage $message) use($transport, $logger) {
         $logger->notice(sprintf("We got a greeting event from {$transport->getId()}"));
     });
 }
 public function send(RemoteEventMessage $message)
 {
     $this->carrierProtocol->sendString($message->toJson());
 }