if ($obj["command"] == "message") { foreach ($this->clients as $client) { if ($this->clients[$client] == $obj["to"]) { $client->send(json_encode(array("from" => $this->clients[$from], "content" => $obj["content"]))); } } } else { if ($obj["command"] == "message_all") { foreach ($this->clients as $client) { if ($from != $client) { $client->send(json_encode(array("from" => $this->clients[$from], "content" => $obj["content"]))); } } } } } } public function onClose(ConnectionInterface $conn) { $this->clients->detach($conn); } public function onError(ConnectionInterface $conn, \Exception $e) { echo "用户 " . $this->clients[$conn] . " 断开连接了"; $conn->close(); } } // Run the server application through the WebSocket protocol on port 8080 $app = new Ratchet\App('211.87.226.143', 5000, '0.0.0.0'); $app->route('/', new MyChat()); $app->run();
// Error logger. ElfChat\Server\WebSocketServer\ErrorLogger::register($app['logger']); $host = array_key_exists('host', $opt) ? $opt['host'] : 'localhost'; $port = array_key_exists('port', $opt) ? $opt['port'] : '8080'; $path = array_key_exists('path', $opt) ? $opt['path'] : '/'; $address = '0.0.0.0'; // 0.0.0.0 means receive connections from any // Init Ratchet server $loop = LoopFactory::create(); $ratchet = new Ratchet\App($host, $port, $address, $loop); // Session Integration $sessionWrapper = function ($component) use($app) { return new SessionProvider($component, $app['session.storage.handler'], array('name' => 'ELFCHAT')); }; // WebSocket Server $chat = new ElfChat\Server\WebSocketServer($app); $wsServer = new WsServer($sessionWrapper($chat)); $ratchet->route($path, $wsServer, array('*')); // WebSocket Controller $controller = new ElfChat\Server\WebSocketServer\Controller($chat); $factory = new \ElfChat\Server\WebSocketServer\Controller\ActionFactory($controller, $app['session.storage.handler'], $app['security.provider']); // Http services $ratchet->route("{$path}/kill", $factory->create('kill', 'ROLE_MODERATOR'), array('*')); $ratchet->route("{$path}/log", $factory->create('log', 'ROLE_MODERATOR'), array('*')); $ratchet->route("{$path}/update_user", $factory->create('updateUser', 'ROLE_GUEST'), array('*')); $ratchet->route("{$path}/memory_usage", $factory->create('memoryUsage', 'ROLE_ADMIN'), array('*')); // Loops $loop->addPeriodicTimer(1, function () use($controller) { $controller->gatherMemoryUsage(); }); $ratchet->run();
* @author Michael Calcinai <*****@*****.**> */ //TODO make this work from vendor dir include __DIR__ . '/../vendor/autoload.php'; use Calcinai\PHPi\Board; use Calcinai\PHPi\Pin\PinFunction; use Ratchet\ConnectionInterface; //The actual WS construction is a bit messy, but it shows the general idea. $loop = \React\EventLoop\Factory::create(); $board = \Calcinai\PHPi\Factory::create($loop); $http_server = new ForExampleOnlyHTTPServer(__DIR__ . '/client.html'); $controller = new RatchetEventBridge(); //This is like a vhost, if it donesn't match the host header you'll get a 404 $app = new Ratchet\App('raspberrypi.local', 9999, '0.0.0.0', $loop); //Route the root to serve the html file $app->route('/', $http_server, ['*']); //Route to ws server $app->route('/phpi', $controller, ['*']); //Some sort of visible heartbeat. No functional use - display only. $loop->addPeriodicTimer(1, function () use($controller) { $controller->broadcast('time', date('r')); }); //The following are the events triggered by the client: /** * Send the pins and their functions on connect * Also send board meta */ $controller->on('client.connect', function (ConnectionInterface $connection) use($controller, $board) { //Prepare the pins in a useful format for the client $headers = $board->getPhysicalPins(); foreach ($headers as &$header) {
<?php //@see https://github.com/reactphp/event-loop $runloop = null; use Ratchet\MessageComponentInterface; use Ratchet\ConnectionInterface; require __DIR__ . '/Ratchet/vendor/autoload.php'; require __DIR__ . '/../index.php'; require __DIR__ . '/../application/controllers/pubsub.php'; $app = new Ratchet\App('localhost', 9090); $app->route('/pubsub', PubSub::instance('WebSocket'), array('*')); $app->run();
<?php use Ratchet\MessageComponentInterface; use Ratchet\ConnectionInterface; use Evenement\EventEmitter; use Apes\RoundHelper; use Apes\RoundProcessor; use Apes\EngineHelper; // Make sure composer dependencies have been installed require 'vendor/autoload.php'; if (isset($argv[1])) { $time = $argv[1]; } else { $time = 600; } if (isset($argv[2])) { $debug = true; } else { $debug = false; } $loop = React\EventLoop\Factory::create(); $emit = new EventEmitter(); $helper = new EngineHelper(); $clients = new \SplObjectStorage(); $processor = new RoundProcessor(); $app = new Ratchet\App('localhost', 8080, '127.0.0.1', $loop); $app->route('/xxxxxx', new Apes\GameEngine($loop, $emit, $clients, $processor, $helper, $time, $debug)); $app->run();
<?php error_reporting(E_ERROR); require __DIR__ . "/vendor/autoload.php"; require __DIR__ . "/event_handler.php"; $loop = React\EventLoop\Factory::create(); $evHndler = new event_handler(); $zctx = new React\ZMQ\Context($loop); $rcvr = $zctx->getSocket(ZMQ::SOCKET_PULL); $rcvr->bind("tcp://127.0.0.1:5555"); $rcvr->on('message', array($evHndler, 'onPush')); $app = new Ratchet\App('gamecon-shirofuji.c9.io', 8081, '0.0.0.0', $loop); $app->route('/game', $evHndler); $app->run();
<?php /** * Created by PhpStorm. * User: vaishakh * Date: 27/09/15 * Time: 9:03 AM */ use Ratchet\Server\IoServer; use Ratchet\Http\HttpServer; use Ratchet\WebSocket\WsServer; require './socket.php'; require './vendor/autoload.php'; $app = new Ratchet\App('dev.chat.com', 8080); $app->route('/chat', new Chat()); $app->route('/echo', new Ratchet\Server\EchoServer(), array('*')); $app->run(); // $server = IoServer::factory( // new Chat(), // 8080 // ); // $server->run();
echo "New connection! ({$conn->resourceId})\n"; } public function onMessage(ConnectionInterface $from, $msg) { foreach ($this->clients as $client) { if ($from != $client) { $client->send($msg); } } } public function onClose(ConnectionInterface $conn) { $this->clients->detach($conn); echo "Close connection! ({$conn->resourceId})\n"; } public function onError(ConnectionInterface $conn, \Exception $e) { $conn->close(); } } // Run the server application through the WebSocket protocol on port 8080 $app = new Ratchet\App($httpHost = 'localhost', $port = 8080, $address = '0.0.0.0'); // Create a route for every MessageComponent $app->route('/chat', new MyChat()); $app->route('/simplesketch', new SimpleSketch()); $app->route('/mouse', new Mouse()); $app->route('/movingball', new MovingBall()); $app->route('/counter', new Counter()); $app->route('/live_editor', new LiveEditor()); $app->route('/echo', new Ratchet\Server\EchoServer(), array('*')); $app->run();
<?php /** * Created by PhpStorm. * User: johan * Date: 15-08-23 * Time: 05:01 */ require __DIR__ . '/../vendor/autoload.php'; $channels = new \app\ChannelCollection(); // create a "General" channel by default. $channels->add(new \app\Channel('General')); // initialize command handlers so we can respond to requests over the socket $commands = [new \app\JoinChannelCommand($channels), new \app\LeaveChannelCommand($channels), new \app\SendMessageCommand($channels), new \app\ListChannelsCommand($channels)]; $chat = new \app\Chat($commands); $ratchet = new \Ratchet\App('localhost', 8080); $ratchet->route('/chat', $chat, ['*']); echo "Starting server\n"; $ratchet->run();
date_default_timezone_set('Europe/Oslo'); $loop = LoopFactory::create(); $app = new \Ratchet\App('localhost', 8080, '127.0.0.1', $loop); $handler = new \Monolog\Handler\StreamHandler('php://stdout', \Psr\Log\LogLevel::INFO); $processor = new \Monolog\Processor\MemoryUsageProcessor(); $logger = new Monolog\Logger('socketty', array($handler), array($processor)); $authenticator = new BasicAuthenticator('username'); $authorizer = new BasicAuthorizer(); $enabledCommands = ['ssh', 'top', 'ping']; $spawner = new CommandSpawner($enabledCommands); $terminalFactory = new TerminalFactory($loop, $spawner); $clientFactory = new ClientFactory($loop, $logger, $terminalFactory, $authorizer); $socketty = new Socketty($logger, $loop, $clientFactory, $authenticator); $memcached = new Memcached(); $memcached->addServer('localhost', 11211); $handler = new MemcachedSessionHandler($memcached); $sessionProvider = new SessionProvider($socketty, $handler, array('name' => 'CUSTOM_NAME')); declare (ticks=1); $sig_handler = function ($signal) use($socketty, $logger) { try { $logger->critical('Server was killed. Stopping new connections and closing existing connections now...'); $socketty->allowNewConnections(false); $socketty->closeAll(); $logger->critical('All connections now closed. Exiting...'); } finally { exit; } }; pcntl_signal(SIGTERM, $sig_handler); $app->route('', $sessionProvider); $app->run();