Esempio n. 1
0
 /**
  * main push request/ websocket response loop
  */
 function onRequest(Request $request, Response $response)
 {
     $content = '';
     $headers = $request->getHeaders();
     $contentLength = isset($headers['Content-Length']) ? (int) $headers['Content-Length'] : null;
     // length required, chunked encoding not supported
     if (null === $contentLength) {
         $response->writeHead(411);
         $response->end();
         return;
     }
     $request->on('data', function ($data) use($request, $response, &$content, $contentLength) {
         // read data (may be empty for GET request)
         $content .= $data;
         // handle request after receive
         if (strlen($content) >= $contentLength) {
             $headers = array('Content-Type' => 'application/json');
             try {
                 $data = $this->hub->handlePushMessage($content);
                 $headers['Content-Length'] = strlen($data);
                 if (null === $data) {
                     $response->writeHead(400, $headers);
                     $response->end();
                 } else {
                     $response->writeHead(200, $headers);
                     $response->end($data);
                 }
             } catch (\Exception $exception) {
                 $data = $this->getExceptionAsJson($exception, true);
                 $headers['Content-Length'] = strlen($data);
                 $response->writeHead(500, $headers);
                 // internal server error
                 $response->end($data);
             }
         }
     });
 }
Esempio n. 2
0
    $routes->add('r' . $routes->count(), new Route($path, array('_controller' => $controller)));
}
/*
 * Main
 */
$output = new ConsoleOutput();
$output->writeln("<info>Volkszaehler Push Server</info>");
if (!Util\Configuration::read('push.enabled')) {
    throw new \Exception("Push server is disabled in configuration", 1);
}
// read config
$localPort = Util\Configuration::read('push.server');
$remotePort = Util\Configuration::read('push.broadcast');
$output->writeln(sprintf("<info>Listening for updates on %d. Clients may connect at %d.</info>", $localPort, $remotePort));
$loop = \React\EventLoop\Factory::create();
$middleware = new MiddlewareAdapter();
// configure local httpd interface
$localSocket = new SocketServer($loop);
$localServer = new HttpReceiver($localSocket, $middleware);
$localSocket->listen($localPort, '0.0.0.0');
// remote loggers can push updates
// configure routes
$routes = new RouteCollection();
$router = new Router(new UrlMatcher($routes, new RequestContext()));
// WAMP adapter
$wampRoutes = Util\Configuration::read('push.routes.wamp');
if (is_array($wampRoutes) && count($wampRoutes)) {
    $wampAdapter = new WampClientAdapter();
    $wampServer = new WsServer(new WampServer($wampAdapter));
    foreach ($wampRoutes as $path) {
        addRoute($path, $wampServer);