PHP code example of warriorxk / phpwebsockets

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

    

warriorxk / phpwebsockets example snippets




$wrapper = new \PHPWebSockets\UpdatesWrapper();
$wrapper->setMessageHandler(function(\PHPWebSockets\AConnection $connection, string $message, int $opcode) {

    echo 'Got message with length ' . strlen($message) . PHP_EOL;
    $connection->write($message, $opcode);

});


$server = new \PHPWebSockets\Server('tcp://0.0.0.0:9001');

while (TRUE) {
    $wrapper->update(0.1, $server->getConnections(TRUE));
}



$wrapper = new \PHPWebSockets\UpdatesWrapper();
$wrapper->setMessageHandler(function(\PHPWebSockets\AConnection $connection, string $message, int $opcode) {

    echo 'Got message with length ' . strlen($message) . PHP_EOL;
    $connection->write($message, $opcode);

});


$client = new \PHPWebSockets\Client();
if (!$client->connect('tcp://localhost:9001/webSocket')) {
    die('Unable to connect to server: ' . $client->getLastError() . PHP_EOL);
}

while (TRUE) {
    $wrapper->update(0.1, [$client]);
}