diff --git a/app/Main/MapUpdate.php b/app/Main/MapUpdate.php index 0a4e03c..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 @@ -44,6 +51,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 @@ -78,6 +92,9 @@ class MapUpdate implements MessageComponentInterface { case 'subscribe': $this->subscribe($conn, $load); break; + case 'healthCheck': + $this->validateHealthCheck($conn, $load); + break; default: break; } @@ -103,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 @@ -388,7 +425,8 @@ class MapUpdate implements MessageComponentInterface { $response = $this->deleteMapId($task, $load); break; case 'healthCheck': - $response = 1; + $this->healthCheckToken = (float)$load; + $response = 'OK'; break; } @@ -509,6 +547,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 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 );