From 2410cc7bfcc0ecf4fb684c67bd73576f984ce0bf Mon Sep 17 00:00:00 2001 From: yggverse Date: Sat, 4 May 2024 20:47:14 +0300 Subject: [PATCH] init nps server features --- config/example.json | 51 +++++++++++ src/Controller/Server/Nps.php | 165 ++++++++++++++++++++++++++++++++++ src/server.php | 47 ++++++++++ 3 files changed, 263 insertions(+) create mode 100644 src/Controller/Server/Nps.php create mode 100644 src/server.php diff --git a/config/example.json b/config/example.json index 222948f..f942f86 100644 --- a/config/example.json +++ b/config/example.json @@ -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": diff --git a/src/Controller/Server/Nps.php b/src/Controller/Server/Nps.php new file mode 100644 index 0000000..3da693c --- /dev/null +++ b/src/Controller/Server/Nps.php @@ -0,0 +1,165 @@ +_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(); + } +} \ No newline at end of file diff --git a/src/server.php b/src/server.php new file mode 100644 index 0000000..c8f3a7c --- /dev/null +++ b/src/server.php @@ -0,0 +1,47 @@ +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; +} \ No newline at end of file