PHP code example of clue / multicast-react

1. Go to this page and download the library: Download clue/multicast-react 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/ */

    

clue / multicast-react example snippets




ory = new Clue\React\Multicast\Factory();
$socket = $factory->createReceiver('224.10.20.30:4050');

$socket->on('message', function ($data, $remote) use ($socket) {
    echo 'Sending back ' . strlen($data) . ' bytes to ' . $remote . PHP_EOL;
    $socket->send($data, $remote);
});


$factory = new Clue\React\Multicast\Factory();

$socket = $factory->createSender();

// send a multicast message to everybody listening on the given address
$socket->send('hello?', '224.10.20.30:4050');

// report incoming unicast replies
$socket->on('message', function ($data, $address) {
    echo 'received ' . strlen($data) . ' bytes from ' . $address . PHP_EOL;
});

$socket = $factory->createReceiver('224.10.20.30:4050');

// report incoming multicast messages 
$socket->on('message', function ($data, $remote) use ($socket) {
    echo 'Sending back ' . strlen($data) . ' bytes to ' . $remote . PHP_EOL;
    
    // send a unicast reply to the remote
    $socket->send($data, $remote);
});

$socket->send($message, $address);

$socket->on('message', function ($message, $address) { });
$socket->on('close', function() { });

$socket->pause();
$socket->resume();

$socket->end();
$socket->close();
bash
php vendor/bin/phpunit