PHP Classes

File: README.md

Recommend this page to a friend!
  Classes of Payam Naderi   Stream PSR 7 Client and Server   README.md   Download  
File: README.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: Stream PSR 7 Client and Server
PSR 7 based network connection client and server
Author: By
Last change:
Date: 7 years ago
Size: 2,481 bytes
 

Contents

Class file image Download

Poirot\Stream

[Documentation is out dated for now]

Connection Oriented Transports (such as TCP)

__Server__

ob_end_clean();
header("Connection: keep-alive");
ignore_user_abort(); // optional

ob_start();
echo ('Server Listening ....');
ob_end_flush(); // Strange behaviour, will not work
flush();

$connect = function()
{
    // Create TCP Server And Bind On Port #8000
    $socket = new StreamServer('tcp://0.0.0.0:8000');
    $socket->bind();

    return $socket;
};

/ @var StreamServer $socketServer */
$socketServer = $connect();
while (1)
{
    try {
        // Listen On Port Till Client Connect ...
        $streamable = $socketServer->listen();
    } catch (TimeoutException $e) {
        // If Connection Timeout Connect Again, and listen
        $socketServer->shutdown();     // free server port
        $socketServer = $connect();    // connect again ..
        continue;
    }

    ob_start();
    echo '<br/><br/>'."Client Connected: "
        .(
            $streamable->getResource()
                ->getRemoteName()
        );
    ob_end_flush();flush();

    // While Client Not Closed Connection ...
    $response = '';
    while ($streamable->getResource()->isAlive())
    {
        $clientMessage = $streamable->readLine();

        ob_start();
        switch ($clientMessage) {
            case 'bye':
                echo '<br/>'."> bye "
                    .(
                    $streamable->getResource()
                        ->getRemoteName()
                    );

                // send back response
                $streamable->write($response);

                // close client connection
                $streamable->getResource()->close();
                break;
            case 'time':
                echo '<br/> >'.$clientMessage;
                $response .= date("D M j H:i:s Y\r\n");
                break;
            default:
                echo '<br/>'.'>'.$clientMessage. ', Not Recognized.';
        }
        ob_end_flush();flush();
    }
}

__Client__

$socks  = new StreamClient('tcp://127.0.0.1:8000');

try {
    $resrc = $socks->getConnect();
} catch (\Exception $e) {
    throw new \Exception('Not Connected.', null, $e);
}

$clientStream = new Streamable($resrc);
echo $clientStream->getResource()->getLocalName();

$clientStream->sendData("time\n");
$clientStream->sendData("not known command\n");
$clientStream->sendData("bye\n");

// Get Back Response
echo $clientStream->read();