PHP Classes
elePHPant
Icontem

Stream PSR 7 Client and Server: PSR 7 based network connection client and server

Recommend this page to a friend!
  Info   View files Documentation   View files View files (55)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2019-05-12 (16 hours ago) RSS 2.0 feedNot yet rated by the usersTotal: 137 This week: 7All time: 8,825 This week: 48Up
Version License PHP version Categories
stream7 1.0.6MIT/X Consortium ...5Networking, PHP 5, Stream wrappers, PSR
Description Author

This package is PSR 7 based network connection client and server.

The server can listen to network connections and can read and write to the client.

The client can connect to a given server and send or receive data.

  Performance   Level  
Innovation award
Innovation award
Nominee: 2x

Details

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();

  Files folder image Files  
File Role Description
Files folder imageContext (6 files, 1 directory)
Files folder imageException (1 file)
Files folder imageFilter (4 files)
Files folder imagefixes (1 file)
Files folder imageInterfaces (6 files, 4 directories)
Files folder imagePsr (2 files)
Files folder imageResource (2 files)
Files folder imageStreamable (7 files, 1 directory)
Files folder imageWrapper (4 files, 2 directories)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation
Plain text file ResourceStream.php Class Class source
Plain text file Streamable.php Class Class source
Plain text file StreamClient.php Class Class source
Plain text file StreamServer.php Class Class source
Plain text file StreamWrapperClient.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:137
This week:7
All time:8,825
This week:48Up