Browse Source

init nps server features

main
yggverse 7 months ago
parent
commit
2410cc7bfc
  1. 51
      config/example.json
  2. 165
      src/Controller/Server/Nps.php
  3. 47
      src/server.php

51
config/example.json

@ -5,6 +5,57 @@ @@ -5,6 +5,57 @@
"username":null,
"password":null
},
"server":
{
"nps":
{
"host":"127.0.0.1",
"port":1900,
"event":
{
"init":
{
"debug":
{
"enabled":true,
"template":"[{time}] [init] NPS server at {host}:{port}"
}
},
"open":
{
"debug":
{
"enabled":true,
"template":"[{time}] [open] connection from {host}#{crid}"
}
},
"message":
{
"debug":
{
"enabled":true,
"template":"[{time}] [message] connection from {host}#{crid} request: {path}"
}
},
"close":
{
"debug":
{
"enabled":true,
"template":"[{time}] [close] connection from {host}#{crid}"
}
},
"error":
{
"debug":
{
"enabled":true,
"template":"[{time}] [error] connection from {host}#{crid} reason: {info}"
}
}
}
}
},
"crawler":
{
"channel":

165
src/Controller/Server/Nps.php

@ -0,0 +1,165 @@ @@ -0,0 +1,165 @@
<?php
namespace Yggverse\Pulsar\Controller\Server;
use \Ratchet\MessageComponentInterface;
class Nps implements MessageComponentInterface
{
private object $_config;
private \Yggverse\Pulsar\Model\Database $_database;
public function __construct(
\Yggverse\Pulsar\Model\Config $config,
\Yggverse\Pulsar\Model\Database $database
) {
// Init config
$this->_config = $config->get()->server->nps;
// Init database
$this->_database = $database;
// Dump event on enabled
if ($this->_config->event->init->debug->enabled)
{
print(
str_ireplace(
[
'{time}',
'{host}',
'{port}'
],
[
(string) date('c'),
(string) $this->_config->host,
(string) $this->_config->port
],
$this->_config->event->init->debug->template
) . PHP_EOL
);
}
}
public function onOpen(
\Ratchet\ConnectionInterface $connection
) {
// Debug open event on enabled
if ($this->_config->event->open->debug->enabled)
{
// Print debug from template
print(
str_ireplace(
[
'{time}',
'{host}',
'{crid}'
],
[
(string) date('c'),
(string) $connection->remoteAddress,
(string) $connection->resourceId
],
$this->_config->event->open->debug->template
) . PHP_EOL
);
}
}
public function onMessage(
\Ratchet\ConnectionInterface $connection,
$request
) {
// Filter request
$request = trim(
(string) $request
);
// Send response
$connection->send(
'test'
);
// Debug message event on enabled
if ($this->_config->event->message->debug->enabled)
{
// Print debug from template
print(
str_ireplace(
[
'{time}',
'{host}',
'{crid}',
'{path}'
],
[
(string) date('c'),
(string) $connection->remoteAddress,
(string) $connection->resourceId,
(string) $request
],
$this->_config->event->message->debug->template
) . PHP_EOL
);
}
// Disconnect
$connection->close();
}
public function onClose(
\Ratchet\ConnectionInterface $connection
) {
// Debug close event on enabled
if ($this->_config->event->close->debug->enabled)
{
// Print debug from template
print(
str_ireplace(
[
'{time}',
'{host}',
'{crid}'
],
[
(string) date('c'),
(string) $connection->remoteAddress,
(string) $connection->resourceId
],
$this->_config->event->close->debug->template
) . PHP_EOL
);
}
}
public function onError(
\Ratchet\ConnectionInterface $connection,
\Exception $exception
) {
// Debug error event on enabled
if ($this->_config->event->error->debug->enabled)
{
// Print debug from template
print(
str_ireplace(
[
'{time}',
'{host}',
'{crid}',
'{info}'
],
[
(string) date('c'),
(string) $connection->remoteAddress,
(string) $connection->resourceId,
(string) $exception->getMessage()
],
$this->_config->event->error->debug->template
) . PHP_EOL
);
}
// Disconnect
$connection->close();
}
}

47
src/server.php

@ -0,0 +1,47 @@ @@ -0,0 +1,47 @@
<?php
// Load dependencies
require_once __DIR__ .
DIRECTORY_SEPARATOR . '..'.
DIRECTORY_SEPARATOR . 'vendor' .
DIRECTORY_SEPARATOR . 'autoload.php';
// Init environment
$environment = new \Yggverse\Pulsar\Model\Environment(
$argv
);
// Init config
$config = new \Yggverse\Pulsar\Model\Config(
$environment->get('config')
);
// Init database
$database = new \Yggverse\Pulsar\Model\Database(
$config->get()->database->location,
$config->get()->database->username,
$config->get()->database->password
);
// Start server
switch ($environment->get('protocol'))
{
case 'nps'|'NPS':
$server = \Ratchet\Server\IoServer::factory(
new \Yggverse\Pulsar\Controller\Server\Nps(
$config,
$database
),
$config->get()->server->nps->port,
$config->get()->server->nps->host
);
$server->run();
break;
default:
throw new \Exception;
}
Loading…
Cancel
Save