PHP code example of djfm / socket-rpc

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

    

djfm / socket-rpc example snippets




use djfm\SocketRPC\Server;

$server = new Server();

$server->bind(1337);

echo sprintf("Server ready to accept connections on `%s`.\n", $server->getAddress());

$server->on('connection', function ($clientId) {
    echo "Welcome, client #$clientId!\n";
})->on('disconnected', function ($clientId) {
    echo "Bye, client #$clientId!\n";
})->on('send', function ($data, $clientId) {
    // client sends data to us, but does not expect a reply,
    // think logs, monitoring...
    var_dump($data);
})->on('query', function ($query, $respond, $clientId) {
    // client sends us something, and wants a response
    var_dump($query);
    $respond(42);
});

// this loops never returns, set things up before :)
$server->run();



use djfm\SocketRPC\Client;

$client = new Client();

$client->connect("tcp://127.0.0.1:1337");

// We just need to tell something to the server, don't wait for a reply
$client->send('hello');

// This is important, expect a reply
$response = $client->query('41 + 1 ?');

echo "Response: $response\n";