laravel-websockets/src/LaravelEcho/WebSocket/PusherServer.php

86 lines
2.6 KiB
PHP
Raw Normal View History

2018-11-21 11:13:40 +00:00
<?php
namespace BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket;
2018-11-25 21:24:31 +00:00
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Dashboard;
2018-11-25 23:42:45 +00:00
use BeyondCode\LaravelWebSockets\QueryParameters;
2018-11-22 09:54:51 +00:00
use Exception;
2018-11-21 11:13:40 +00:00
use Ratchet\ConnectionInterface;
use Ratchet\RFC6455\Messaging\MessageInterface;
use BeyondCode\LaravelWebSockets\WebSocketController;
2018-11-24 12:58:56 +00:00
use BeyondCode\LaravelWebSockets\ClientProviders\Client;
2018-11-21 11:13:40 +00:00
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager;
2018-11-26 08:15:39 +00:00
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\PusherException;
2018-11-23 23:06:28 +00:00
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\UnknownAppKeyException;
2018-11-21 11:13:40 +00:00
2018-11-22 10:22:16 +00:00
class PusherServer extends WebSocketController
2018-11-21 11:13:40 +00:00
{
2018-11-21 23:21:42 +00:00
/** @var \BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager */
2018-11-21 11:13:40 +00:00
protected $channelManager;
2018-11-24 22:52:55 +00:00
public function __construct(ChannelManager $channelManager)
2018-11-21 11:13:40 +00:00
{
$this->channelManager = $channelManager;
}
2018-11-21 23:21:42 +00:00
function onOpen(ConnectionInterface $connection)
2018-11-21 11:13:40 +00:00
{
2018-11-22 10:30:57 +00:00
$this->generateSocketId($connection);
2018-11-21 21:11:44 +00:00
2018-11-22 10:29:12 +00:00
$this->verifyConnection($connection);
2018-11-22 10:30:57 +00:00
$this->establishConnection($connection);
2018-11-21 11:13:40 +00:00
}
2018-11-22 07:31:45 +00:00
public function onMessage(ConnectionInterface $connection, MessageInterface $message)
2018-11-21 11:13:40 +00:00
{
2018-11-22 07:31:45 +00:00
$message = RespondableMessageFactory::createForMessage($message, $connection, $this->channelManager);
2018-11-21 11:13:40 +00:00
2018-11-21 22:47:46 +00:00
$message->respond();
2018-11-21 11:13:40 +00:00
}
2018-11-21 21:36:50 +00:00
public function onClose(ConnectionInterface $connection)
{
$this->channelManager->removeFromAllChannels($connection);
}
2018-11-22 09:54:51 +00:00
function onError(ConnectionInterface $connection, Exception $exception)
{
if ($exception instanceof PusherException) {
$connection->send(json_encode(
$exception->getPayload()
));
}
}
2018-11-22 10:29:12 +00:00
protected function verifyConnection(ConnectionInterface $connection)
{
2018-11-25 23:42:45 +00:00
$appKey = QueryParameters::create($connection->httpRequest)->get('appKey');
2018-11-22 10:30:57 +00:00
2018-11-25 23:42:45 +00:00
if (! $client = Client::findByAppKey($appKey)) {
throw new UnknownAppKeyException($appKey);
2018-11-22 10:29:12 +00:00
}
2018-11-22 16:59:56 +00:00
2018-11-24 21:07:53 +00:00
$connection->client = $client;
2018-11-22 10:29:12 +00:00
}
2018-11-22 10:30:57 +00:00
protected function establishConnection(ConnectionInterface $connection)
{
2018-11-25 21:24:31 +00:00
Dashboard::connection($connection);
2018-11-22 10:30:57 +00:00
$connection->send(json_encode([
'event' => 'pusher:connection_established',
'data' => json_encode([
'socket_id' => $connection->socketId,
'activity_timeout' => 60,
])
]));
}
protected function generateSocketId(ConnectionInterface $connection)
{
$socketId = sprintf("%d.%d", getmypid(), random_int(1, 100000000));
$connection->socketId = $socketId;
}
2018-11-21 11:13:40 +00:00
}