PHP Classes

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

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 166 All time: 8,899 This week: 80Up
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.

Picture of Payam Naderi
  Performance   Level  
Innovation award
Innovation award
Nominee: 2x

 

Documentation

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

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:166
This week:0
All time:8,899
This week:80Up