nps-php 1.3.0

New methods

Server::setWelcome

Define application logic on peer connection established

$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

$server->setPending(
    function (
        string $request,
        string $connect
    ): ?string
    {
        printf(
            'connection: %s requested: %s',
            $connect,
            $request,
        );

        return sprintf(
            'received: %s',
            $request
        );
    }
);

Server::getPending

Get current Pending function, null by default

Server::setHandler

Define basic application logic on complete packet received

$server->setHandler(
    function (
          bool $success,
        string $content,
        string $request,
        string $connect
    ): ?string
    {
        printf(
            'connection: %s request: %s',
            $connect,
            $request
        );

        if ($success)
        {
            var_dump(
                $content
            );
        }

        return 'thank you!';
    }
);

Server::getHandler

Get current Handler function, null by default

Example

$server->start();
> nc 127.0.0.1 1915
< welcome, 127.0.0.1:38028
> test
< received: test
> 1
> 2
> 3
> .
< thank you!

Links

Download nps-php 1.3.0