97 lines
2.9 KiB
PHP
97 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace BeyondCode\LaravelWebSockets\WebSockets;
|
|
|
|
use BeyondCode\LaravelWebSockets\Dashboard\DashboardLogger;
|
|
use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger;
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\WebSocketException;
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Messages\PusherMessageFactory;
|
|
use BeyondCode\LaravelWebSockets\QueryParameters;
|
|
use Exception;
|
|
use Ratchet\ConnectionInterface;
|
|
use Ratchet\RFC6455\Messaging\MessageInterface;
|
|
use BeyondCode\LaravelWebSockets\Apps\App;
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\UnknownAppKey;
|
|
use Ratchet\WebSocket\MessageComponentInterface;
|
|
|
|
class WebSocketHandler implements MessageComponentInterface
|
|
{
|
|
/** @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager */
|
|
protected $channelManager;
|
|
|
|
public function __construct(ChannelManager $channelManager)
|
|
{
|
|
$this->channelManager = $channelManager;
|
|
}
|
|
|
|
public function onOpen(ConnectionInterface $connection)
|
|
{
|
|
$this
|
|
->verifyAppKey($connection)
|
|
->generateSocketId($connection)
|
|
->establishConnection($connection);
|
|
}
|
|
|
|
public function onMessage(ConnectionInterface $connection, MessageInterface $message)
|
|
{
|
|
$message = PusherMessageFactory::createForMessage($message, $connection, $this->channelManager);
|
|
|
|
$message->respond();
|
|
}
|
|
|
|
public function onClose(ConnectionInterface $connection)
|
|
{
|
|
$this->channelManager->removeFromAllChannels($connection);
|
|
|
|
DashboardLogger::disconnection($connection);
|
|
}
|
|
|
|
public function onError(ConnectionInterface $connection, Exception $exception)
|
|
{
|
|
if ($exception instanceof WebSocketException) {
|
|
$connection->send(json_encode(
|
|
$exception->getPayload()
|
|
));
|
|
}
|
|
}
|
|
|
|
protected function verifyAppKey(ConnectionInterface $connection)
|
|
{
|
|
$appKey = QueryParameters::create($connection->httpRequest)->get('appKey');
|
|
|
|
if (!$app = App::findByKey($appKey)) {
|
|
throw new UnknownAppKey($appKey);
|
|
}
|
|
|
|
$connection->app = $app;
|
|
|
|
return $this;
|
|
}
|
|
|
|
protected function generateSocketId(ConnectionInterface $connection)
|
|
{
|
|
$socketId = sprintf("%d.%d", random_int(1, 1000000000), random_int(1, 1000000000));
|
|
|
|
$connection->socketId = $socketId;
|
|
|
|
return $this;
|
|
}
|
|
|
|
protected function establishConnection(ConnectionInterface $connection)
|
|
{
|
|
$connection->send(json_encode([
|
|
'event' => 'pusher:connection_established',
|
|
'data' => json_encode([
|
|
'socket_id' => $connection->socketId,
|
|
'activity_timeout' => 30,
|
|
])
|
|
]));
|
|
|
|
DashboardLogger::connection($connection);
|
|
|
|
StatisticsLogger::connection($connection);
|
|
|
|
return $this;
|
|
}
|
|
} |