72
app/Main/Handler/LogFileHandler.php
Normal file
72
app/Main/Handler/LogFileHandler.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: exodu
|
||||
* Date: 03.09.2017
|
||||
* Time: 17:02
|
||||
*/
|
||||
|
||||
namespace Exodus4D\Socket\Main\Handler;
|
||||
|
||||
|
||||
class LogFileHandler {
|
||||
|
||||
const ERROR_DIR_CREATE = 'There is no existing directory at "%s" and its not buildable.';
|
||||
|
||||
/**
|
||||
* steam uri
|
||||
* @var string
|
||||
*/
|
||||
private $stream = '';
|
||||
|
||||
/**
|
||||
* stream dir
|
||||
* @var string
|
||||
*/
|
||||
private $dir = '.';
|
||||
|
||||
/**
|
||||
* file base dir already created
|
||||
* @var bool
|
||||
*/
|
||||
private $dirCreated = false;
|
||||
|
||||
public function __construct(string $stream){
|
||||
$this->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;
|
||||
}
|
||||
}
|
||||
@@ -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,16 @@ class MapUpdate implements MessageComponentInterface {
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* dispatch log writing to a LogFileHandler
|
||||
* @param array $meta
|
||||
* @param array $log
|
||||
*/
|
||||
private function handleLogData(array $meta, array $log){
|
||||
$logHandler = new LogFileHandler((string)$meta['stream']);
|
||||
$logHandler->write($log);
|
||||
}
|
||||
|
||||
|
||||
// logging ====================================================================================
|
||||
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
"require": {
|
||||
"php-64bit": ">=7.0",
|
||||
"ext-zmq": "1.1.*",
|
||||
"cboden/ratchet": "0.3.*",
|
||||
"react/zmq": "0.3.*"
|
||||
"cboden/ratchet": "0.4.x-dev",
|
||||
"react/zmq": "dev-master"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user