/** * Start this live reload server * * @return */ public function start() { $config = $this->config; $loop = \React\EventLoop\Factory::create(); $app = new \Ratchet\App($config['host'], $config['port'], $config['host'], $loop); foreach ($config['routes'] as $route) { call_user_func_array(array($app, 'route'), $route); } $loop->addTimer($config['timeout'], array($this, 'watchTempFile')); $app->run(); }
/** * Handle pin level changes from the client. * The pin is automatically set to output if it isn't already. */ $controller->on('pin.level', function ($data) use($board) { $pin = $board->getPin($data['pin']); //Just to help out! if ($pin->getFunction() !== PinFunction::OUTPUT) { $pin->setFunction(PinFunction::OUTPUT); } $data['level'] ? $pin->high() : $pin->low(); }); //Set up the update events server -> client //This is a bit of a duplicate of above but it's for clarity as an example. $headers = $board->getPhysicalPins(); foreach ($headers as $header) { foreach ($header as $pin_number => $physical_pin) { if ($physical_pin->gpio_number !== null) { $pin = $board->getPin($physical_pin->gpio_number); //Need to add a callback on these pins to the client too for level and function changes. $pin->on(\Calcinai\PHPi\Pin::EVENT_FUNCTION_CHANGE, function ($new_function) use($pin, $controller) { $controller->broadcast('pin.function', ['pin' => $pin->getPinNumber(), 'func' => $new_function]); }); $pin->on(\Calcinai\PHPi\Pin::EVENT_LEVEL_CHANGE, function () use($pin, $controller) { $controller->broadcast('pin.level', ['pin' => $pin->getPinNumber(), 'level' => $pin->getLevel()]); }); } } } $app->run();
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();