laravel-websockets/src/WebSockets/Controllers/PusherController.php

92 lines
2.8 KiB
PHP
Raw Normal View History

2018-11-21 11:13:40 +00:00
<?php
2018-11-27 15:42:19 +00:00
namespace BeyondCode\LaravelWebSockets\WebSockets\Controllers;
2018-11-21 11:13:40 +00:00
2018-11-26 21:18:00 +00:00
use BeyondCode\LaravelWebSockets\Events\ConnectionEstablished;
2018-11-27 15:35:28 +00:00
use BeyondCode\LaravelWebSockets\WebSockets\Messages\RespondableMessageFactory;
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;
2018-11-27 14:55:30 +00:00
use BeyondCode\LaravelWebSockets\Server\WebSocketController;
2018-11-24 12:58:56 +00:00
use BeyondCode\LaravelWebSockets\ClientProviders\Client;
2018-11-27 15:35:28 +00:00
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\PusherException;
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\UnknownAppKey;
2018-11-21 11:13:40 +00:00
2018-11-27 14:55:30 +00:00
class PusherController extends WebSocketController
2018-11-21 11:13:40 +00:00
{
2018-11-27 15:43:59 +00:00
/** @var \BeyondCode\LaravelWebSockets\WebSockets\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-26 23:13:22 +00:00
$this
->generateSocketId($connection)
->verifyConnection($connection)
->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
2018-11-26 23:13:22 +00:00
public function onError(ConnectionInterface $connection, Exception $exception)
2018-11-22 09:54:51 +00:00
{
if ($exception instanceof PusherException) {
$connection->send(json_encode(
$exception->getPayload()
));
}
}
2018-11-22 10:29:12 +00:00
2018-11-26 23:13:22 +00:00
protected function generateSocketId(ConnectionInterface $connection)
{
$socketId = sprintf("%d.%d", getmypid(), random_int(1, 100000000));
$connection->socketId = $socketId;
return $this;
}
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-26 23:13:22 +00:00
if (!$client = Client::findByAppKey($appKey)) {
throw new UnknownAppKey($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-26 23:13:22 +00:00
return $this;
2018-11-22 10:29:12 +00:00
}
2018-11-22 10:30:57 +00:00
protected function establishConnection(ConnectionInterface $connection)
{
$connection->send(json_encode([
'event' => 'pusher:connection_established',
'data' => json_encode([
'socket_id' => $connection->socketId,
'activity_timeout' => 60,
])
]));
2018-11-26 21:18:00 +00:00
event(new ConnectionEstablished($connection));
2018-11-22 10:30:57 +00:00
2018-11-26 23:13:22 +00:00
return $this;
2018-11-22 10:30:57 +00:00
}
2018-11-21 11:13:40 +00:00
}