From 7fe9f786292b11d84fd138a088a50975bab05266 Mon Sep 17 00:00:00 2001 From: Exodus4D Date: Sun, 8 Oct 2017 00:15:16 +0200 Subject: [PATCH] - added log file write ability - added unSubscribeCharacterIds() for multi logout characters --- app/Main/Handler/LogFileHandler.php | 72 +++++++++++++++++++++++++++++ app/Main/MapUpdate.php | 34 ++++++++++---- app/WebSockets.php | 16 +++++-- 3 files changed, 107 insertions(+), 15 deletions(-) create mode 100644 app/Main/Handler/LogFileHandler.php diff --git a/app/Main/Handler/LogFileHandler.php b/app/Main/Handler/LogFileHandler.php new file mode 100644 index 0000000..374bc50 --- /dev/null +++ b/app/Main/Handler/LogFileHandler.php @@ -0,0 +1,72 @@ +stream = $stream; + $this->dir = dirname($this->stream); + $this->createDir(); + } + + /** + * write log data into to file + * @param array $log + */ + public function write(array $log){ + $log = (string)json_encode($log, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); + if( !empty($log) ){ + $stream = fopen($this->stream, 'a'); + flock($stream, LOCK_EX); + fwrite($stream, $log . PHP_EOL); + flock($stream, LOCK_UN); + fclose($stream); + } + } + + /** + * create directory + */ + private function createDir(){ + // Do not try to create dir if it has already been tried. + if ($this->dirCreated){ + return; + } + + if ($this->dir && !is_dir($this->dir)){ + $status = mkdir($this->dir, 0777, true); + if (false === $status) { + throw new \UnexpectedValueException(sprintf(self::ERROR_DIR_CREATE, $this->dir)); + } + } + $this->dirCreated = true; + } +} \ No newline at end of file diff --git a/app/Main/MapUpdate.php b/app/Main/MapUpdate.php index 47d2ac9..fb3f4a7 100644 --- a/app/Main/MapUpdate.php +++ b/app/Main/MapUpdate.php @@ -8,6 +8,7 @@ namespace Exodus4D\Socket\Main; +use Exodus4D\Socket\Main\Handler\LogFileHandler; use Ratchet\MessageComponentInterface; use Ratchet\ConnectionInterface; @@ -58,14 +59,7 @@ class MapUpdate implements MessageComponentInterface { */ protected $debug = false; - /** - * internal socket for response calls - * @var null | \React\ZMQ\SocketWrapper - */ - protected $internalSocket = null; - - public function __construct($socket) { - $this->internalSocket = $socket; + public function __construct() { $this->characterAccessData = []; $this->mapAccessData = []; $this->characters = []; @@ -229,6 +223,19 @@ class MapUpdate implements MessageComponentInterface { return true; } + /** + * unSubscribe $characterIds from ALL maps + * @param array $characterIds + * @return bool + */ + private function unSubscribeCharacterIds(array $characterIds): bool{ + $response = false; + foreach($characterIds as $characterId){ + $response = $this->unSubscribeCharacterId($characterId); + } + return $response; + } + /** * delete mapId from subscriptions and broadcast "delete msg" to clients * @param string $task @@ -410,7 +417,7 @@ class MapUpdate implements MessageComponentInterface { switch($data['task']){ case 'characterLogout': - $response = $this->unSubscribeCharacterId($load); + $response = $this->unSubscribeCharacterIds($load); break; case 'mapConnectionAccess': $response = $this->setConnectionAccess($load); @@ -428,9 +435,11 @@ class MapUpdate implements MessageComponentInterface { $this->healthCheckToken = (float)$load; $response = 'OK'; break; + case 'logData': + $this->handleLogData((array)$load['meta'], (array)$load['log']); + break; } - // $this->internalSocket->send($response); } /** @@ -536,6 +545,11 @@ class MapUpdate implements MessageComponentInterface { return $response; } + private function handleLogData(array $meta, array $log){ + $logHandler = new LogFileHandler((string)$meta['stream']); + $logHandler->write($log); + } + // logging ==================================================================================== diff --git a/app/WebSockets.php b/app/WebSockets.php index c21afdb..6e4ca61 100644 --- a/app/WebSockets.php +++ b/app/WebSockets.php @@ -34,20 +34,26 @@ 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); + // Socket for map update data ------------------------------------------------------------- $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 ); // main app -> inject socket for response - $mapUpdate = new Main\MapUpdate($pull); + $mapUpdate = new Main\MapUpdate(); // "onMessage" listener $pull->on('message', [$mapUpdate, 'receiveData']); - // Set up our WebSocket server for clients wanting real-time updates - $webSock = new React\Socket\Server($loop); + // Socket for log data -------------------------------------------------------------------- + //$pullSocketLogs = $context->getSocket(\ZMQ::SOCKET_PULL); + //$pullSocketLogs->bind( "tcp://127.0.0.1:5555" ); + //$pullSocketLogs->on('message', [$mapUpdate, 'receiveLogData']) ; + // Binding to 0.0.0.0 means remotes can connect (Web Clients) - $webSock->listen($this->wsListenPort, $this->wsListenHost); + $webSocketURI = $this->wsListenHost . ':' . $this->wsListenPort; + + // Set up our WebSocket server for clients wanting real-time updates + $webSock = new React\Socket\Server($webSocketURI, $loop); new IoServer( new HttpServer( new WsServer(