1. Go to this page and download the library: Download lpks/php-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/ */
lpks / php-websocket example snippets
use PHPWebsocket\Core\Socket;
use PHPWebsocket\Server;
ages = [];
function getMessages()
{
global $messages;
return $messages;
}
function pushMessage($message)
{
global $messages;
$messages[] = $message;
}
$server->run(function (Socket $socket) {
echo "Connected: {$socket->id()}\n";
$socket->on('chat', function ($data, Socket $socket) {
pushMessage([
'message' => $data['message'],
'sender' => $socket->id(),
]);
$socket->broadcast()->emit('chat', [
'message' => $data['message'],
'sender' => $socket->id(),
]);
return ['status' => true];
});
$socket->on('messages', function ($data, Socket $socket) {
return ['messages' => getMessages()];
});
}, function (Socket $socket) {
echo "Disconnected: {$socket->id()}\n";
}, function () use ($server) {
echo "Listening on {$server->getAddress()}:{$server->getPort()}\n";
});
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.