From 2d6e56500cf7c914f977cf0be6647a29392357dc Mon Sep 17 00:00:00 2001 From: Exodus4D Date: Thu, 19 Jan 2017 20:33:01 +0100 Subject: [PATCH 1/3] - WIP changed REQ/REP Socket config to PUSH/PULL Socket config --- app/Main/MapUpdate.php | 3 ++- app/WebSockets.php | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/Main/MapUpdate.php b/app/Main/MapUpdate.php index 0a4e03c..ee8e578 100644 --- a/app/Main/MapUpdate.php +++ b/app/Main/MapUpdate.php @@ -388,7 +388,8 @@ class MapUpdate implements MessageComponentInterface { $response = $this->deleteMapId($task, $load); break; case 'healthCheck': - $response = 1; + $this->log('Health'); + $response = 'OK'; break; } diff --git a/app/WebSockets.php b/app/WebSockets.php index 3972c15..c21afdb 100644 --- a/app/WebSockets.php +++ b/app/WebSockets.php @@ -34,7 +34,8 @@ class WebSockets { // Listen for the web server to make a ZeroMQ push after an ajax request $context = new React\ZMQ\Context($loop); - $pull = $context->getSocket(\ZMQ::SOCKET_REP); + //$pull = $context->getSocket(\ZMQ::SOCKET_REP); + $pull = $context->getSocket(\ZMQ::SOCKET_PULL); // Binding to 127.0.0.1 means, the only client that can connect is itself $pull->bind( $this->dns ); From 0f7ba57a2a53bf0e2bb55bc7a40da00f12c8dc86 Mon Sep 17 00:00:00 2001 From: Exodus4D Date: Sat, 21 Jan 2017 20:50:29 +0100 Subject: [PATCH 2/3] - Added "debug" flag/output --- app/Main/MapUpdate.php | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/app/Main/MapUpdate.php b/app/Main/MapUpdate.php index ee8e578..62f028c 100644 --- a/app/Main/MapUpdate.php +++ b/app/Main/MapUpdate.php @@ -44,6 +44,13 @@ class MapUpdate implements MessageComponentInterface { */ protected $subscriptions; + /** + * enable debug output + * -> check debug() for more information + * @var bool + */ + protected $debug = false; + /** * internal socket for response calls * @var null | \React\ZMQ\SocketWrapper @@ -388,7 +395,6 @@ class MapUpdate implements MessageComponentInterface { $response = $this->deleteMapId($task, $load); break; case 'healthCheck': - $this->log('Health'); $response = 'OK'; break; } @@ -510,6 +516,34 @@ class MapUpdate implements MessageComponentInterface { protected function log($text){ $text = date('Y-m-d H:i:s') . ' ' . $text; echo $text . "\n"; + + $this->debug(); + } + + protected function debug(){ + if( $this->debug ){ + $mapId = 1; + $characterId = 1946320202; + + $subscriptions = $this->subscriptions[$mapId]; + $connectionsForChar = count($this->characters[$characterId]); + $mapAccessData = $this->mapAccessData[$mapId][$characterId]; + echo "\n" . "========== START ==========" . "\n"; + + echo "-> characterAccessData: " . "\n"; + var_dump( $this->characterAccessData ); + + echo "\n" . "-> Subscriptions mapId: " . $mapId . " " . "\n"; + var_dump($subscriptions); + + echo "\n" . "-> connectionsForChar characterId: " . $characterId . " count: " . $connectionsForChar . " " . "\n"; + + echo "-> mapAccessData: " . "\n"; + var_dump($mapAccessData); + + echo "\n" . "========== END ==========" . "\n"; + } + } } \ No newline at end of file From 0f611000585ab1df27bff640fd23ecc5cda0bb88 Mon Sep 17 00:00:00 2001 From: Exodus4D Date: Sat, 21 Jan 2017 23:20:23 +0100 Subject: [PATCH 3/3] - Improved "health check" for WebSocket --- app/Main/MapUpdate.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/app/Main/MapUpdate.php b/app/Main/MapUpdate.php index 62f028c..302ce11 100644 --- a/app/Main/MapUpdate.php +++ b/app/Main/MapUpdate.php @@ -13,6 +13,13 @@ use Ratchet\ConnectionInterface; class MapUpdate implements MessageComponentInterface { + /** + * timestamp (ms) from last healthCheck ping + * -> timestamp received from remote TCP socket + * @var + */ + protected $healthCheckToken; + /** * expire time for map access tokens (seconds) * @var int @@ -85,6 +92,9 @@ class MapUpdate implements MessageComponentInterface { case 'subscribe': $this->subscribe($conn, $load); break; + case 'healthCheck': + $this->validateHealthCheck($conn, $load); + break; default: break; } @@ -110,6 +120,26 @@ class MapUpdate implements MessageComponentInterface { $conn->close(); } + /** + * Check token (timestamp from initial TCP healthCheck poke against token send from client + * @param ConnectionInterface $conn + * @param $token + */ + private function validateHealthCheck($conn, $token){ + $isValid = 0; + + if( + $token && $this->healthCheckToken && + $token === $this->healthCheckToken + ){ + $isValid = 1; + } + $conn->send( json_encode($isValid) ); + + // reset token + $this->healthCheckToken = null; + } + /** * subscribes a connection to valid accessible maps * @param ConnectionInterface $conn @@ -395,6 +425,7 @@ class MapUpdate implements MessageComponentInterface { $response = $this->deleteMapId($task, $load); break; case 'healthCheck': + $this->healthCheckToken = (float)$load; $response = 'OK'; break; }