/**
  * @param ServerEvent $event
  */
 public function bindPnctlEvent(ServerEvent $event)
 {
     if (!extension_loaded('pcntl')) {
         return;
     }
     $loop = $event->getEventLoop();
     $server = $event->getServer();
     $pnctlEmitter = new PnctlEmitter($loop);
     $pnctlEmitter->on(SIGTERM, function () use($server, $loop) {
         $server->emit('end');
         $server->shutdown();
         $loop->stop();
         $this->logger->notice('Server stopped !');
     });
     $pnctlEmitter->on(SIGINT, function () use($server, $loop) {
         $this->logger->notice('Press CTLR+C again to stop the server');
         if (SIGINT === pcntl_sigtimedwait([SIGINT], $siginfo, 5)) {
             $this->logger->notice('Stopping server ...');
             $server->emit('end');
             $server->shutdown();
             foreach ($this->periodicRegistry->getPeriodics() as $periodic) {
                 if ($periodic instanceof TimerInterface && $loop->isTimerActive($periodic)) {
                     $loop->cancelTimer($periodic);
                 }
             }
             $loop->stop();
             $this->logger->notice('Server stopped !');
         } else {
             $this->logger->notice('CTLR+C not pressed, continue to run normally');
         }
     });
 }
 public function launch($profile)
 {
     $this->logger->info('Starting web socket');
     $stack = new Builder();
     /* @var $loop LoopInterface */
     $loop = Factory::create();
     $server = new Server($loop);
     $server->listen($this->port, $this->host);
     if (true === $profile) {
         $memoryUsagePeriodicTimer = new PeriodicMemoryUsage($this->logger);
         $this->periodicRegistry->addPeriodic($memoryUsagePeriodicTimer);
     }
     /** @var PeriodicInterface $periodic */
     foreach ($this->periodicRegistry->getPeriodics() as $periodic) {
         $loop->addPeriodicTimer($periodic->getTimeout(), [$periodic, 'tick']);
         $this->logger->info(sprintf('Register periodic callback %s, executed each %s seconds', $periodic instanceof ProxyInterface ? get_parent_class($periodic) : get_class($periodic), $periodic->getTimeout()));
     }
     $allowedOrigins = array_merge(array('localhost', '127.0.0.1'), $this->originRegistry->getOrigins());
     $stack->push('Ratchet\\Server\\IoServer', $server, $loop)->push('Ratchet\\Http\\HttpServer');
     if ($this->originCheck) {
         $stack->push('Gos\\Bundle\\WebSocketBundle\\Server\\App\\Stack\\OriginCheck', $allowedOrigins, $this->eventDispatcher);
     }
     $stack->push('Ratchet\\WebSocket\\WsServer')->push('Ratchet\\Session\\SessionProvider', $this->sessionHandler)->push('Ratchet\\Wamp\\WampServer');
     $app = $stack->resolve($this->wampApplication);
     /* Server Event Loop to add other services in the same loop. */
     $event = new ServerEvent($loop, $server);
     $this->eventDispatcher->dispatch(Events::SERVER_LAUNCHED, $event);
     $this->logger->info(sprintf('Launching %s on %s PID: %s', $this->getName(), $this->getAddress(), getmypid()));
     $app->run();
 }
 /**
  * @param Server        $server
  * @param LoopInterface $loop
  */
 protected function closure(Server $server, LoopInterface $loop)
 {
     $this->logger->notice('Stopping server ...');
     foreach ($this->serverPushHandlerRegistry->getPushers() as $handler) {
         $handler->close();
         $this->logger->info(sprintf('Stop %s push handler', $handler->getName()));
     }
     $server->emit('end');
     $server->shutdown();
     foreach ($this->periodicRegistry->getPeriodics() as $periodic) {
         if ($periodic instanceof TimerInterface && $loop->isTimerActive($periodic)) {
             $loop->cancelTimer($periodic);
         }
     }
     $loop->stop();
     $this->logger->notice('Server stopped !');
 }
 /**
  * @param bool $profile
  *
  * @throws \React\Socket\ConnectionException
  */
 public function launch($host, $port, $profile)
 {
     $this->logger->info('Starting web socket');
     //In order to avoid circular reference
     $this->topicManager->setWampApplication($this->wampApplication);
     $stack = new Builder();
     $server = new Server($this->loop);
     $server->listen($port, $host);
     if (true === $profile) {
         $memoryUsagePeriodicTimer = new PeriodicMemoryUsage($this->logger);
         $this->periodicRegistry->addPeriodic($memoryUsagePeriodicTimer);
     }
     /** @var PeriodicInterface $periodic */
     foreach ($this->periodicRegistry->getPeriodics() as $periodic) {
         $this->loop->addPeriodicTimer($periodic->getTimeout(), [$periodic, 'tick']);
         $this->logger->info(sprintf('Register periodic callback %s, executed each %s seconds', $periodic instanceof ProxyInterface ? get_parent_class($periodic) : get_class($periodic), $periodic->getTimeout()));
     }
     $allowedOrigins = array_merge(array('localhost', '127.0.0.1'), $this->originRegistry->getOrigins());
     $stack->push('Ratchet\\Server\\IoServer', $server, $this->loop)->push('Ratchet\\Http\\HttpServer');
     if ($this->originCheck) {
         $stack->push('Gos\\Bundle\\WebSocketBundle\\Server\\App\\Stack\\OriginCheck', $allowedOrigins, $this->eventDispatcher);
     }
     $stack->push('Ratchet\\WebSocket\\WsServer')->push('Gos\\Bundle\\WebSocketBundle\\Server\\App\\Stack\\WampConnectionPeriodicTimer', $this->loop)->push('Ratchet\\Session\\SessionProvider', $this->sessionHandler)->push('Ratchet\\Wamp\\WampServer', $this->topicManager);
     $app = $stack->resolve($this->wampApplication);
     //Push Transport Layer
     foreach ($this->serverPusherHandlerRegistry->getPushers() as $handler) {
         try {
             $handler->handle($this->loop, $this->wampApplication);
         } catch (\Exception $e) {
             $this->logger->error($e->getMessage(), ['code' => $e->getCode(), 'file' => $e->getFile(), 'line' => $e->getLine(), 'push_handler_name' => $handler->getName()]);
         }
     }
     /* Server Event Loop to add other services in the same loop. */
     $event = new ServerEvent($this->loop, $server);
     $this->eventDispatcher->dispatch(Events::SERVER_LAUNCHED, $event);
     $this->logger->info(sprintf('Launching %s on %s PID: %s', $this->getName(), $host . ':' . $port, getmypid()));
     $app->run();
 }