Skip to content

Commit

Permalink
Upgrade react dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Nov 25, 2017
1 parent 7a93fc1 commit f01efae
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
36 changes: 21 additions & 15 deletions lib/Server/HttpReceiver.php
Expand Up @@ -24,50 +24,56 @@

namespace Volkszaehler\Server;

use React\Socket\Server as ReactSocketServer;
use React\Http\Server as ReactHttpServer;
use React\Http\Request;
use React\Http\Response;
use React\Socket\Server as SocketServer;
use React\Http\Server as HttpServer;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use RingCentral\Psr7;

/**
* HttpReceiver implements the server side interface for receiving push messages
*/
class HttpReceiver extends ReactHttpServer {
class HttpReceiver {

/**
* @var MiddlewareAdapter
*/
protected $hub;

function __construct(ReactSocketServer $socket, MiddlewareAdapter $hub) {
parent::__construct($socket);
/**
* @var HttpServer
*/
protected $http;

function __construct(SocketServer $socket, MiddlewareAdapter $hub) {
$this->hub = $hub;
$this->on('request', array($this, 'onRequest'));

$this->http = new HttpServer(array($this, 'handleRequest'));
$this->http->listen($socket);
}

/**
* Main push request/ websocket response loop
*/
function onRequest(Request $request, Response $response) {
function handleRequest(ServerRequestInterface $request): ResponseInterface {
$headers = array('Content-Type' => 'application/json');

try {
$content = $request->getBody();
$data = $this->hub->handlePushMessage($content);
$headers['Content-Length'] = strlen($data);

if (null === $data) {
$response->writeHead(400, $headers);
$response->end();
return new Psr7\Response(400, $headers);
}
else {
$response->writeHead(200, $headers);
$response->end($data);
return new Psr7\Response(400, $headers, $data);
}
}
catch (\Exception $exception) {
$data = $this->getExceptionAsJson($exception, true);
$headers['Content-Length'] = strlen($data);
$response->writeHead(500, $headers); // internal server error
$response->end($data);
return new Psr7\Response(500, $headers, $data);
}
}

Expand Down
10 changes: 6 additions & 4 deletions misc/tools/push-server.php
Expand Up @@ -29,7 +29,9 @@

use Volkszaehler\Util;

use React\Socket\Server as ReactSocketServer;
use React\EventLoop\Factory;
use React\Socket\Server as SocketServer;

use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\Http\HttpServerInterface;
Expand Down Expand Up @@ -74,11 +76,11 @@ function addRoute($path, HttpServerInterface $controller) {

$output->writeln(sprintf("<info>Listening for updates on %d. Clients may connect at %d.</info>", $localPort, $remotePort));

$loop = \React\EventLoop\Factory::create();
$loop = Factory::create();
$middleware = new MiddlewareAdapter();

// configure local httpd interface
$localSocket = new ReactSocketServer(REMOTE_IP_PORT . $localPort, $loop); // remote loggers can push updates
$localSocket = new SocketServer(REMOTE_IP_PORT . $localPort, $loop); // remote loggers can push updates
$localServer = new HttpReceiver($localSocket, $middleware);

// configure routes
Expand Down Expand Up @@ -118,7 +120,7 @@ function addRoute($path, HttpServerInterface $controller) {
}

// configure remote interface
$remoteSocket = new ReactSocketServer(REMOTE_IP_PORT . $remotePort, $loop); // remote clients can connect
$remoteSocket = new SocketServer(REMOTE_IP_PORT . $remotePort, $loop); // remote clients can connect
$remoteServer = new IoServer(
new HttpServer(
$router
Expand Down

0 comments on commit f01efae

Please sign in to comment.