PHP code example of krypt0nn / sockets

1. Go to this page and download the library: Download krypt0nn/sockets library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

krypt0nn / sockets example snippets




ockets\Client;

$client = new Client;
$client->connect ('127.0.0.1', 53874);

while (true)
    $client->send (readline ('> '));



ockets\Listener;

$listener = new Listener (53874);

$i = 0;
$client = $listener->acceptAsync()->call(function () use (&$i)
{
    if (++$i == 10)
        return false;
    
    echo 'Waiting for connections... ('. $i .')' . PHP_EOL;
    sleep (1);

    return true;
});

if ($client === null)
    die ('Client not connected');

echo 'Client connected'. PHP_EOL;

while (true)
{
    try
    {
        $messages = @$client->read ();
    }

    catch (\Exception $e)
    {
        continue;
    }
    
    foreach ($messages as $message)
        echo '> '. $message . PHP_EOL;

    sleep (1);
}