PHP code example of dazzle-php / socket

1. Go to this page and download the library: Download dazzle-php/socket 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/ */

    

dazzle-php / socket example snippets


$loop = new Loop(new SelectLoop);
$server = new SocketListener('tcp://127.0.0.1:2080', $loop);

$server->on('connect', function($server, SocketInterface $client) {
    $client->write("Hello!\n");
    $client->write("Welcome to Dazzle server!\n");
    $client->write("Tell me a range and I will randomize a number for you!\n\n");

    $client->on('data', function(SocketInterface $client, $data) use(&$buffer) {
        $client->write("Your number is: " . rand(...explode('-', $data)));
    });
});

$loop->onStart(function() use($server) {
    $server->start();
});
$loop->start();

$loop = new Loop(new SelectLoop);
$socket = new Socket('tcp://127.0.0.1:2080', $loop);

$socket->on('data', function($socket, $data) {
    printf("%s", $data);
});
$socket->write('1-100');

$loop->start();

$> composer