From 0f611000585ab1df27bff640fd23ecc5cda0bb88 Mon Sep 17 00:00:00 2001 From: Exodus4D Date: Sat, 21 Jan 2017 23:20:23 +0100 Subject: [PATCH] - 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; }