1. Go to this page and download the library: Download snoke/symfony-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/ */
snoke / symfony-websocket example snippets
use Snoke\Websocket\Event\MessageReceived;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
#[AsEventListener(event: MessageReceived::class, method: 'onRequestReceived')]
final class MessageListener
{
public function onRequestReceived(MessageReceived $event): void
{
$connection = $event->getConnection();
$connection->send("Salutations, intergalactic sphere!");
}
}
namespace App\EventListener;
use App\Security\Authenticator;
use Snoke\Websocket\Event\RequestReceived;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
#[AsEventListener(event: RequestReceived::class, method: 'onRequestReceived')]
final class AuthListener
{
public function __construct(
private readonly Authenticator,
private readonly SerializerInterface
) {}
public function onRequestReceived(RequestReceived $event): void
{
$request = $event->getFrame();
$connection = $event->getConnection();
if ($request['type'] === 'auth') {
$payload = $request['payload'];
$user = $this->authenticator->authenticate($payload['identifier'],$payload['password']);
$connection->setUser($user);
$connection->send($serializer->serialize($user, 'json'));
}
}
}
foreach($event->getConnections() as $connection) {
$connection->send($message);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.