PHP code example of vakata / websocket
1. Go to this page and download the library: Download vakata/websocket 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/ */
vakata / websocket example snippets
php
// this handler will forward each message to all clients (except the sender)
$server = new \vakata\websocket\Server('ws://127.0.0.1:8080');
$server->onMessage(function ($sender, $message, $server) {
foreach ($server->getClients() as $client) {
if ($client !== $sender) {
$client->send($message);
}
}
});
$server->run();
php
// this handler will echo each message to standard output
$client = new \vakata\websocket\Client('ws://127.0.0.1:8080');
$client->onMessage(function ($message, $client) {
echo $message . "\r\n";
});
$client->connect();