2018-11-21 11:13:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
2018-11-27 20:41:12 +00:00
|
|
|
namespace BeyondCode\LaravelWebSockets\WebSockets;
|
2018-11-21 11:13:40 +00:00
|
|
|
|
2018-11-29 10:59:17 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Dashboard\DashboardLogger;
|
2018-12-03 12:27:45 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger;
|
2018-11-27 20:06:04 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\WebSocketException;
|
2018-12-01 14:10:01 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Messages\PusherMessageFactory;
|
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-12-01 12:57:02 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Apps\App;
|
2018-11-27 15:35:28 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
|
|
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\UnknownAppKey;
|
2018-11-27 20:06:04 +00:00
|
|
|
use Ratchet\WebSocket\MessageComponentInterface;
|
2018-11-21 11:13:40 +00:00
|
|
|
|
2018-11-27 20:06:04 +00:00
|
|
|
class WebSocketHandler implements MessageComponentInterface
|
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-30 16:24:55 +00:00
|
|
|
public function onOpen(ConnectionInterface $connection)
|
2018-11-21 11:13:40 +00:00
|
|
|
{
|
2018-11-26 23:13:22 +00:00
|
|
|
$this
|
2018-12-01 12:40:18 +00:00
|
|
|
->verifyAppKey($connection)
|
2018-11-26 23:13:22 +00:00
|
|
|
->generateSocketId($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-12-01 14:10:01 +00:00
|
|
|
$message = PusherMessageFactory::createForMessage($message, $connection, $this->channelManager);
|
2018-11-21 11:13:40 +00:00
|
|
|
|
2018-11-21 22:47:46 +00:00
|
|
|
$message->respond();
|
2018-12-03 12:33:20 +00:00
|
|
|
|
2018-12-03 15:23:20 +00:00
|
|
|
StatisticsLogger::webSocketMessage($connection);
|
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-12-03 10:10:02 +00:00
|
|
|
|
|
|
|
|
DashboardLogger::disconnection($connection);
|
2018-12-03 12:33:20 +00:00
|
|
|
|
2018-12-03 15:23:20 +00:00
|
|
|
StatisticsLogger::disconnection($connection);
|
2018-11-21 21:36:50 +00:00
|
|
|
}
|
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
|
|
|
{
|
2018-11-27 20:06:04 +00:00
|
|
|
if ($exception instanceof WebSocketException) {
|
2018-11-22 09:54:51 +00:00
|
|
|
$connection->send(json_encode(
|
|
|
|
|
$exception->getPayload()
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-22 10:29:12 +00:00
|
|
|
|
2018-12-01 12:40:18 +00:00
|
|
|
protected function verifyAppKey(ConnectionInterface $connection)
|
2018-11-22 10:29:12 +00:00
|
|
|
{
|
2018-11-25 23:42:45 +00:00
|
|
|
$appKey = QueryParameters::create($connection->httpRequest)->get('appKey');
|
2018-11-22 10:30:57 +00:00
|
|
|
|
2018-12-01 13:12:15 +00:00
|
|
|
if (!$app = App::findByKey($appKey)) {
|
2018-11-26 23:13:22 +00:00
|
|
|
throw new UnknownAppKey($appKey);
|
2018-11-22 10:29:12 +00:00
|
|
|
}
|
2018-11-22 16:59:56 +00:00
|
|
|
|
2018-12-01 13:12:15 +00:00
|
|
|
$connection->app = $app;
|
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
|
|
|
|
2018-12-01 12:40:18 +00:00
|
|
|
protected function generateSocketId(ConnectionInterface $connection)
|
|
|
|
|
{
|
|
|
|
|
$socketId = sprintf("%d.%d", random_int(1, 1000000000), random_int(1, 1000000000));
|
|
|
|
|
|
|
|
|
|
$connection->socketId = $socketId;
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
2018-11-28 16:09:50 +00:00
|
|
|
'activity_timeout' => 30,
|
2018-11-22 10:30:57 +00:00
|
|
|
])
|
|
|
|
|
]));
|
2018-11-26 21:18:00 +00:00
|
|
|
|
2018-11-29 10:59:17 +00:00
|
|
|
DashboardLogger::connection($connection);
|
2018-11-22 10:30:57 +00:00
|
|
|
|
2018-12-03 15:23:20 +00:00
|
|
|
StatisticsLogger::connection($connection);
|
2018-12-03 12:27:45 +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
|
|
|
}
|