PHP / Composer Library for NPS Protocol
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
4.3 KiB

4 weeks ago
**Archived!**
Use [Ratchet](https://github.com/ratchetphp/Ratchet) `IoServer` - asynchronous WebSocket in PHP as more featured replacement to this library.
1 month ago
# nps-php
1 month ago
1 month ago
PHP 8 / Composer Library for NPS Protocol
1 month ago
1 month ago
Like Titan for Gemini, NPS is the satellite for NEX protocol (see also [nex-php](https://github.com/YGGverse/nex-php))\
it listen for single dot in line to signal the package ending.
1 month ago
1 month ago
## Specification
`nex://nightfall.city/nps/`
1 month ago
## Usage
```
composer require yggverse/nps
```
## Server
Build interactive server instance to listen NPS protocol connections!
``` php
$server = new \Yggverse\Nps\Server;
```
1 month ago
Provide optional `host`, `port`, `size`, `line` and `live` arguments in constructor:
1 month ago
``` php
$server = new \Yggverse\Nps\Server('127.0.0.1', 1915);
```
1 month ago
Alternatively, use following setters after object initiation
1 month ago
1 month ago
#### Server::setHost
1 month ago
Bind server host to listen incoming connections, `127.0.0.1` by default
1 month ago
#### Server::getHost
1 month ago
Get current server host
1 month ago
#### Server::setPort
1 month ago
Bind server port to listen incoming connections, `1915` by default
1 month ago
#### Server::getPort
1 month ago
Get current server port
1 month ago
#### Server::setSize
1 month ago
Set total content length limit by [mb_strlen](https://www.php.net/manual/en/function.mb-strlen.php), `0` by default (unlimited)
1 month ago
#### Server::getSize
1 month ago
Get current content length limit
#### Server::setLine
Set packet line limit in bytes passing to [fread](https://www.php.net/manual/en/function.fread.php#length), `1024` by default
#### Server::getLine
Get current packet line limit
1 month ago
#### Server::setLive
1 month ago
Set server status `true`|`false` to shutdown immediately
1 month ago
#### Server::getLive
1 month ago
Get current server status
#### Server::setWelcome
Define application logic on peer connection established
``` php
$server->setWelcome(
function (
string $connect
): ?string
{
printf(
"connected: %s\n\r",
$connect
);
return sprintf(
"welcome, %s\n\r",
$connect
);
}
);
```
#### Server::getWelcome
Get current `Welcome` function, `null` by default
#### Server::setPending
Define application logic on peer make initial request
``` php
$server->setPending(
function (
string $request,
string $connect
): ?string
{
printf(
1 month ago
'connection: %s requested: %s',
$connect,
$request,
);
return sprintf(
1 month ago
'received: %s',
$request
);
}
);
```
#### Server::getPending
Get current `Pending` function, `null` by default
#### Server::setHandler
1 month ago
Define basic application logic on complete packet received
1 month ago
* could be also defined as [Server::start](https://github.com/YGGverse/nps-php#serverstart) argument
``` php
$server->setHandler(
function (
bool $success,
string $content,
string $request,
string $connect
): ?string
{
printf(
'connection: %s request: %s',
$connect,
$request
);
if ($success)
{
var_dump(
$content
);
}
1 month ago
return 'thank you!';
}
);
```
#### Server::getHandler
Get current `Handler` function, `null` by default
1 month ago
#### Server::start
1 month ago
Run server object
1 month ago
1 month ago
``` php
$server->start();
```
Optionally, define handler function as the argument to process application logic dependent of client request
1 month ago
``` php
$server->start(
function (
1 month ago
bool $success,
1 month ago
string $content,
1 month ago
string $request,
string $connect
): ?string
{
1 month ago
printf(
'connection: %s request: %s',
1 month ago
$connect,
$request
);
1 month ago
if ($success)
{
var_dump(
$content
);
}
1 month ago
return 'thank you!'; // null|string response
1 month ago
}
);
```
#### Server::stop
Stop server instance.
Same to `Server::setLive(false)`
1 month ago
1 month ago
### Testing
1 month ago
1. `nc 127.0.0.1 1915` - connect server using `nc`
1 month ago
2. `test` - enter the target path
1 month ago
3. `YOUR MESSAGE GOES HERE` - enter the message text
4. `.` - commit package with dot
To send any file:
``` file.txt
1 month ago
test
1 month ago
╦ ╦╔═╗╔═╗╔╦╗╦═╗╔═╗╔═╗╦╦
╚╦╝║ ╦║ ╦ ║║╠╦╝╠═╣╚═╗║║
╩ ╚═╝╚═╝═╩╝╩╚═╩ ╩╚═╝╩╩═╝
.
```
1 month ago
`cat file.txt | nc 127.0.0.1 1915`