Beispiel #1
0
         */
        $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()}"));
        });
    }
}
$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);
$server = new WebSocketServer("tcp://0.0.0.0:12345", $loop, $logger);
$server->bind();
$server->on("handshake", function (\Devristo\Phpws\Protocol\WebSocketTransportInterface $transport, \Devristo\Phpws\Protocol\Handshake $handshake) {
    $handshake->getResponse()->getHeaders()->addHeaderLine("X-WebSocket-Server", "phpws");
});
$router = new \Devristo\Phpws\Server\UriHandler\ClientRouter($server, $logger);
$router->addRoute('#^/stack#i', new StackHandler($loop, $logger));
// Start the event loop
$loop->run();
 public function __construct($wsport)
 {
     $this->say('Creating server');
     // Logger (required for handlers)
     $this->logger = new \Zend\Log\Logger();
     // Log PHP errors
     Zend\Log\Logger::registerErrorHandler($this->logger);
     // Writes to stdout
     $writer = new \Zend\Log\Writer\Stream("php://output");
     $this->logger->addWriter($writer);
     // Also log to a file
     //$writer2 = new Zend\Log\Writer\Stream('/path/to/logfile');
     //$this->logger->addWriter($writer2);
     // Filter log messages not showing debug
     //$filter = new Zend\Log\Filter\Priority(\Zend\Log\Logger::WARN);
     $filter = new Zend\Log\Filter\Priority(\Zend\Log\Logger::CRIT);
     $writer->addFilter($filter);
     //$writer2->addFilter($filter);
     // WebSocket server
     $this->loop = \React\EventLoop\Factory::create();
     $this->server = new \Devristo\Phpws\Server\WebSocketServer("tcp://0.0.0.0:{$wsport}", $this->loop, $this->logger);
     // Handlers
     $this->index = new IndexHandler($this->logger, $this, 'index');
     $this->tournament = new TournamentIndexHandler($this->logger, $this, 'tournament');
     $this->draft = new DraftHandler($this->logger, $this, 'draft');
     $this->build = new BuildHandler($this->logger, $this, 'build');
     $this->game = new GameHandler($this->logger, $this, 'game');
     $this->admin = new AdminHandler($this->logger, $this, 'admin');
     $this->handlers = array('index', 'tournament', 'draft', 'build', 'game', 'admin');
     // Routes
     $router = new \Devristo\Phpws\Server\UriHandler\ClientRouter($this->server, $this->logger);
     $router->addRoute('#^/index#i', $this->index);
     $router->addRoute('#^/game#i', $this->game);
     $router->addRoute('#^/tournament#i', $this->tournament);
     $router->addRoute('#^/draft#i', $this->draft);
     $router->addRoute('#^/build#i', $this->build);
     $router->addRoute('#^/admin#i', $this->admin);
     // Params
     global $ts3;
     $this->ts3 = $ts3;
     $this->say('Server created');
 }
Beispiel #3
0
    protected function getStream(WebSocketTransportInterface $user, $id)
    {
        $userStreams = $this->getStreamsByUser($user);
        return array_key_exists($id, $userStreams) ? $userStreams[$id] : null;
    }
    /**
     * @param WebSocketTransportInterface $user
     * @return \React\Stream\Stream[]
     */
    protected function getStreamsByUser(WebSocketTransportInterface $user)
    {
        return array_key_exists($user->getId(), $this->streams) ? $this->streams[$user->getId()] : array();
    }
    protected function removeStream(WebSocketTransportInterface $user, $id)
    {
        unset($this->streams[$user->getId()][$id]);
    }
    protected function addStream(WebSocketTransportInterface $user, $id, \React\Stream\Stream $stream)
    {
        $this->streams[$user->getId()][$id] = $stream;
    }
}
$loop = \React\EventLoop\Factory::create();
$logger = new \Zend\Log\Logger();
$writer = new Zend\Log\Writer\Stream("php://output");
$logger->addWriter($writer);
$server = new WebSocketServer("tcp://0.0.0.0:12345", $loop, $logger);
$router = new \Devristo\Phpws\Server\UriHandler\ClientRouter($server, $logger);
$router->addRoute("#^/proxy\$#i", new ProxyHandler($loop, $logger));
$server->bind();
$loop->run();
Beispiel #4
0
    /**
     * This class deals with users who are not routed
     */
    public function onConnect(WebSocketTransportInterface $user)
    {
        //do nothing
        $this->logger->notice("User {$user->getId()} did not join any room");
    }
    public function onMessage(WebSocketTransportInterface $user, WebSocketMessageInterface $msg)
    {
        //do nothing
        $this->logger->notice("User {$user->getId()} is not in a room but tried to say: {$msg->getData()}");
    }
}
$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
$server = new WebSocketServer("tcp://0.0.0.0:12345", $loop, $logger);
// Create a router which transfers all /chat connections to the ChatHandler class
$router = new \Devristo\Phpws\Server\UriHandler\ClientRouter($server, $logger);
// route /chat url
$router->addRoute('#^/chat$#i', new ChatHandler($logger));
// route unmatched urls durring this demo to avoid errors
$router->addRoute('#^(.*)$#i', new ChatHandlerForUnroutedUrls($logger));
// Bind the server
$server->bind();
// Start the event loop
$loop->run();