- improved logging

- fixed multiple problem with map sync
- added some "garbage collection" for unsubscribed connection data
- updated README.md
This commit is contained in:
Mark Friedrich
2019-07-01 19:59:52 +02:00
parent a25cf73daa
commit cbca53ad73
12 changed files with 1285 additions and 283 deletions

76
cmd.php
View File

@@ -1,33 +1,73 @@
<?php
require 'vendor/autoload.php';
use Exodus4D\Socket;
if(PHP_SAPI === 'cli'){
// optional CLI params
$options = getopt('', [
'pf_listen_host:',
'pf_listen_port:',
'pf_host:',
'pf_port:'
]);
// optional CLI params -> default values
// The default values should be fine for 99% of you!
$longOpts = [
'wsHost:' => '0.0.0.0', // WebSocket connection (for WebClients => Browser). '0.0.0.0' <-- any client can connect!
'wsPort:' => 8020, // ↪ default WebSocket URI: 127.0.0.1:8020. This is where Nginx must proxy WebSocket traffic to
'tcpHost:' => '127.0.0.1', // TcpSocket connection (for WebServer ⇄ WebSocket)
'tcpPort:' => 5555, // ↪ default TcpSocket URI: tcp://127.0.0.1:5555
'debug:' => 2 // Debug level [0-3] 0 = silent, 1 = errors, 2 = error + info, 3 = error + info + debug
];
// get options from CLI parameter + default values
$cliOpts = getopt('', array_keys($longOpts));
$options = [];
array_walk($longOpts, function($defaultVal, $optKey) use ($cliOpts, &$options) {
$key = trim($optKey, ':');
$val = $defaultVal;
if(array_key_exists($key, $cliOpts)){
$val = is_int($defaultVal) ? (int)$cliOpts[$key] : $cliOpts[$key] ;
}
$options[$key] = $val;
});
/**
* WebSocket connection (for WebClients => Browser)
* default WebSocket URI: ws://127.0.0.1:8020
*
* pf_client_ip '0.0.0.0' <-- any client can connect
* pf_ws_port 8020 <-- any client can connect
* print current config parameters to Shell
* @param array $longOpts
* @param array $options
*/
$wsListenHost = (!empty($options['pf_listen_host'])) ? $options['pf_listen_host'] : '0.0.0.0' ;
$wsListenPort = (!empty($options['pf_listen_port'])) ? (int)$options['pf_listen_port'] : 8020 ;
$showHelp = function(array $longOpts, array $options){
$optKeys = array_keys($longOpts);
$colors = new Socket\Log\ShellColors();
$data = [];
$host = (!empty($options['pf_host'])) ? $options['pf_host'] : '127.0.0.1' ;
$port = (!empty($options['pf_port'])) ? (int)$options['pf_port'] : 5555 ;
// headline for CLI config parameters
$rowData = $colors->getColoredString(str_pad(' param', 12), 'white');
$rowData .= $colors->getColoredString(str_pad('value', 18, ' ', STR_PAD_LEFT), 'white');
$rowData .= $colors->getColoredString(str_pad('default', 15, ' ', STR_PAD_LEFT), 'white');
$dsn = 'tcp://' . $host . ':' . $port;
$data[] = $rowData;
$data[] = str_pad(' ', 45, '-');
$i = 0;
foreach($options as $optKey => $optVal){
$rowData = $colors->getColoredString(str_pad(' -' . $optKey, 12), 'yellow');
$rowData .= $colors->getColoredString(str_pad($optVal, 18, ' ', STR_PAD_LEFT), 'light_purple');
$rowData .= $colors->getColoredString(str_pad($longOpts[$optKeys[$i]], 15, ' ', STR_PAD_LEFT), 'dark_gray');
$data[] = $rowData;
$i++;
}
$data[] = '';
echo implode($data, PHP_EOL) . PHP_EOL;
};
if($options['debug']){
// print if -debug > 0
$showHelp($longOpts, $options);
}
$dsn = 'tcp://' . $options['tcpHost'] . ':' . $options['tcpPort'];
new Socket\WebSockets($dsn, $options['wsPort'], $options['wsHost'], $options['debug']);
new Socket\WebSockets($dsn, $wsListenPort, $wsListenHost);
}else{
echo "Script need to be called by CLI!";
}