PHP code example of kanata-php / conveyor-server-client

1. Go to this page and download the library: Download kanata-php/conveyor-server-client 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/ */

    

kanata-php / conveyor-server-client example snippets


use Kanata\ConveyorServerClient\Client;

$options = [
    'onMessageCallback' => function(Client $currentClient, string $message) {
        echo 'Message received: ' . $message . PHP_EOL;
    },
];

$client = new Client($options);
$client->connect();

[
    /**
     * @var string
     */
    'protocol' => 'ws',
    
    /**
     * @var string
     */
    'uri' => '127.0.0.1',
    
    /**
     * @var int
     */
    'port' => 8000,
    
    /**
     * @var string
     */
    'query' => '',
    
    /**
     * @var ?string
     */
    'channel' =>  null,
    
    /**
     * @var ?string
     */
    'listen' => null,
    
    /**
     * @var ?callable
     */
    'onOpenCallback' => null,
    
    /**
     * @var ?callable
     */
    'onReadyCallback' => null,
    
    /**
     * Callback for incoming messages.
     * Passed parameters:
     *   - \WebSocket\Client $client
     *   - string $message
     *
     * @var ?callable
     */
    'onMessageCallback' => null,
    
    /**
     * Callback for disconnection.
     * Passed parameters:
     *   - \WebSocket\Client $client
     *   - int $reconnectionAttemptsCount
     *
     * @var ?callable
     */
    'onDisconnectCallback' => null,
    
    /**
     * Callback for Reconnection moment.
     * Passed parameters:
     *   - \WebSocket\Client $client
     *   - int \Throwable $e
     *
     * @var ?callable
     */
    'onReconnectionCallback' => null,
    
    /**
     * When positive, considered in seconds
     *
     * @var int
     */
    'timeout' => -1,
    
    /**
     * @var bool
     */
    'reconnect' => false;
    
    /**
     * Number of attempts if disconnects
     * For this to keeps trying forever, set it to -1. 
     *
     * @var int
     */
    'reconnectionAttempts' => = 0;
    
    /**
     * Interval to reconnect in seconds 
     * 
     * @var int 
     */
    'reconnectionInterval' => = 2;
]

namespace Kanata\ConveyorServerClient;

use WebSocket\Client;

interface ClientInterface
{
    public function connect(): void;
    public function getClient(): ?Client;
    public function close(): void;
    public function send(string $message): void;
    public function sendRaw(string $message): void;
}