- Improved "health check" for WebSocket

This commit is contained in:
Exodus4D
2017-01-21 23:20:23 +01:00
parent 0f7ba57a2a
commit 0f61100058

View File

@@ -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;
}