コード例 #1
0
 public function unsubscribe(StackTransport $transport)
 {
     if (array_key_exists($transport->getId(), $this->members)) {
         unset($this->members[$transport->getId()]);
         $this->emit("unsubscribe", array($transport));
     }
 }
コード例 #2
0
ファイル: RemoteEvents.php プロジェクト: valinurovam/phpws
 /**
  * @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]);
     });
 }
コード例 #3
0
ファイル: remoteEvents.php プロジェクト: rb-cohen/phpws
 /**
  * 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()}"));
     });
 }
コード例 #4
0
ファイル: rooms.php プロジェクト: rb-cohen/phpws
// Run from command prompt > php demo.php
use Devristo\Phpws\RemoteEvent\RemoteEventTransport;
use Devristo\Phpws\Protocol\StackTransport;
use Devristo\Phpws\Protocol\TransportInterface;
use Devristo\Phpws\Server\WebSocketServer;
$loop = \React\EventLoop\Factory::create();
// Create a logger which writes everything to the STDOUT
$logger = new \Zend\Log\Logger();
$writer = new Zend\Log\Writer\Stream("php://output");
$logger->addWriter($writer);
// Create a WebSocket server and create a router which sends all user requesting /echo to the DemoEchoHandler above
$server = new WebSocketServer("tcp://0.0.0.0:12345", $loop, $logger);
$handler = new \Devristo\Phpws\RemoteEvent\RemoteEvents($logger);
$server->on("connect", function (TransportInterface $transport) use($loop, $logger, $handler) {
    $stack = StackTransport::create($transport, array(function (TransportInterface $transport) use($loop, $logger) {
        return new RemoteEventTransport($transport, $loop, $logger);
    }));
    $handler->listenTo($stack);
});
$handler->room("time")->on("subscribe", function (StackTransport $transport) use($logger, $handler) {
    $logger->notice("Someone joined our room full of time enthousiasts!!");
});
// Each 0.5 seconds sent the time to all connected clients
$loop->addPeriodicTimer(0.5, function () use($server, $handler, $logger) {
    $time = new DateTime();
    $string = $time->format("Y-m-d H:i:s");
    if (count($handler->room("time")->getMembers())) {
        $logger->notice("Broadcasting time to time room: {$string}");
    }
    $handler->room("time")->remoteEmit("time", $string);
});