Simple and multifunctional PHP WebSocket server
The recommended way to install WebSockets is through Composer.
# Install Composer
curl -sS https://getcomposer.org/installer | php
Next, run the Composer command to install the latest stable version of WebSockets:
php composer.phar require ollyxar/websockets
After installing, you need to require Composer's autoloader:
require 'vendor/autoload.php';
use Ollyxar\WebSockets\{
Server as WServer,
Ssl as Wsl,
Worker as Handler,
Frame as WFrame
};
class MyHandler extends Handler
{
/**
* MyHandler constructor.
* @param $server
* @param $master
*/
public function __construct($server, $master)
{
parent::__construct($server, $master);
echo "I'm: #{$this->pid}\n";
}
/**
* @param $client
*/
protected function onConnect($client): void
{
$this->sendToAll(WFrame::encode(json_encode([
'type' => 'system',
'message' => 'User {' . (int)$client . ') Connected.'
])));
}
/**
* @param $clientNumber
*/
protected function onClose($clientNumber): void
{
$this->sendToAll(WFrame::encode(json_encode([
'type' => 'system',
'message' => "User {$clientNumber} disconnected."
])));
}
/**
* @param string $message
*/
protected function onDirectMessage(string $message): void
{
$message = json_decode($message);
$userName = $message->name;
$userMessage = $message->message;
$response = WFrame::encode(json_encode([
'type' => 'usermsg',
'name' => $userName,
'message' => $userMessage
]));
$this->sendToAll($response);
}
}
/**
* Lets start our server
*/
(new WServer('0.0.0.0', 2083, 6, true))
->setCert()
->setPassPhrase()
->setHandler(MyHandler::class)
->run();
use Ollyxar\WebSockets\Server as WServer;
use Ollyxar\WebSockets\Frame;
$socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
socket_connect($socket, WServer::$connector);
$data = new stdClass();
$data->type = 'system';
$data->message = 'hello World!';
$msg = Frame::encode(json_encode($data));
socket_write($socket, $msg);
socket_close($socket);