This commit is contained in:
freek 2018-11-26 22:05:24 +01:00
parent e8e79dfef3
commit cc9fb7d7eb
6 changed files with 55 additions and 10 deletions

View File

@ -1,8 +0,0 @@
<?php
namespace BeyondCode\LaravelWebSockets\Dashboard;
class EventHandler
{
//TODO: listen for events, pass them to Dashboard:: methods
}

View File

@ -0,0 +1,27 @@
<?php
namespace BeyondCode\LaravelWebSockets\Dashboard;
use BeyondCode\LaravelWebSockets\Events\ChannelVacated;
use BeyondCode\LaravelWebSockets\Events\ClientMessageSent;
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Dashboard;
use Illuminate\Events\Dispatcher;
class EventSubscriber
{
public function onChannelVacated(ChannelVacated $event)
{
Dashboard::vacated($event->connection, $event->channelId);
}
public function onClientMessageSent(ClientMessageSent $event)
{
Dashboard::clientMessage($event->connection, $event->payload);
}
public function subscribe(Dispatcher $events)
{
$events->listen(ChannelVacated::class, static::class . '@onChannelVacated');
$events->listen(ClientMessageSent::class, static::class . '@onClientMessageSent');
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace BeyondCode\LaravelWebSockets\Events;
use Ratchet\ConnectionInterface;
use stdClass;
class ClientMessageSent
{
/** @var \Ratchet\ConnectionInterface */
public $connection;
/** @var string */
public $payload;
public function __construct(ConnectionInterface $connection, stdClass $payload)
{
$this->connection = $connection;
$this->payload = $payload;
}
}

View File

@ -61,7 +61,6 @@ class Channel
if (! $this->hasConnections()) {
event(new ChannelVacated($connection, $this->channelId));
Dashboard::vacated($connection, $this->channelId);
}
}

View File

@ -2,6 +2,7 @@
namespace BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket;
use BeyondCode\LaravelWebSockets\Events\ClientMessageSent;
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager;
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Dashboard;
use Ratchet\ConnectionInterface;
@ -30,7 +31,7 @@ class Message implements RespondableMessage
public function respond()
{
if (starts_with($this->payload->event, 'client-')) {
Dashboard::clientMessage($this->connection, $this->payload);
event(new ClientMessageSent($this->connection, $this->payload));
$channel = $this->channelManager->find($this->connection->client->appId, $this->payload->channel);

View File

@ -2,6 +2,8 @@
namespace BeyondCode\LaravelWebSockets;
use BeyondCode\LaravelWebSockets\Dashboard\EventSubscriber;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Route;
use BeyondCode\LaravelWebSockets\ClientProviders\ClientProvider;
@ -27,6 +29,8 @@ class WebSocketsServiceProvider extends ServiceProvider
$this->commands([
Console\StartWebSocketServer::class,
]);
Event::subscribe(EventSubscriber::class);
}
protected function registerRoutes()