Merge branch 'master' of github.com:beyondcode/laravel-websockets

This commit is contained in:
Marcel Pociot 2018-11-26 22:25:57 +01:00
commit 33a3145911
10 changed files with 126 additions and 20 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,45 @@
<?php
namespace BeyondCode\LaravelWebSockets\Dashboard;
use BeyondCode\LaravelWebSockets\Events\ApiMessageSent;
use BeyondCode\LaravelWebSockets\Events\ChannelVacated;
use BeyondCode\LaravelWebSockets\Events\ClientMessageSent;
use BeyondCode\LaravelWebSockets\Events\ConnectionEstablished;
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Dashboard;
use Illuminate\Events\Dispatcher;
class EventSubscriber
{
public function onApiMessageSent(ApiMessageSent $event)
{
Dashboard::apiMessage(
$event->appId,
$event->channeldId,
$event->name,
$event->data
);
}
public function onChannelVacated(ChannelVacated $event)
{
Dashboard::vacated($event->connection, $event->channelId);
}
public function onClientMessageSent(ClientMessageSent $event)
{
Dashboard::clientMessage($event->connection, $event->payload);
}
public function onConnectionEstablished(ConnectionEstablished $event)
{
Dashboard::connection($event->connection);
}
public function subscribe(Dispatcher $events)
{
$events->listen(ApiMessageSent::class, static::class. '@onApiMessageSent');
$events->listen(ChannelVacated::class, static::class . '@onChannelVacated');
$events->listen(ClientMessageSent::class, static::class . '@onClientMessageSent');
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace BeyondCode\LaravelWebSockets\Events;
use Ratchet\ConnectionInterface;
use stdClass;
class ApiMessageSent
{
public $appId;
public $channeldId;
public $name;
public $data;
public function __construct($appId, $channeldId, $name, $data)
{
$this->appId = $appId;
$this->channeldId = $channeldId;
$this->name = $name;
$this->data = $data;
}
}

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

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

View File

@ -2,7 +2,7 @@
namespace BeyondCode\LaravelWebSockets\LaravelEcho\Http\Controllers;
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Dashboard;
use BeyondCode\LaravelWebSockets\Events\ApiMessageSent;
use Illuminate\Http\Request;
class TriggerEvent extends EchoController
@ -12,13 +12,6 @@ class TriggerEvent extends EchoController
$this->ensureValidSignature($request);
foreach ($request->json()->get('channels', []) as $channelId) {
Dashboard::apiMessage(
$request->appId,
$channelId,
$request->json()->get('name'),
$request->json()->get('data')
);
$channel = $this->channelManager->find($request->appId, $channelId);
optional($channel)->broadcastToEveryoneExcept([
@ -26,6 +19,13 @@ class TriggerEvent extends EchoController
'event' => $request->json()->get('name'),
'data' => $request->json()->get('data'),
], $request->json()->get('socket_id'));
event(new ApiMessageSent(
$request->appId,
$channelId,
$request->json()->get('name'),
$request->json()->get('data')
));
}
return $request->json()->all();

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,7 @@
namespace BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket;
use BeyondCode\LaravelWebSockets\Events\ConnectionEstablished;
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Dashboard;
use BeyondCode\LaravelWebSockets\QueryParameters;
use Exception;
@ -66,8 +67,6 @@ class PusherServer extends WebSocketController
protected function establishConnection(ConnectionInterface $connection)
{
Dashboard::connection($connection);
$connection->send(json_encode([
'event' => 'pusher:connection_established',
'data' => json_encode([
@ -75,6 +74,8 @@ class PusherServer extends WebSocketController
'activity_timeout' => 60,
])
]));
event(new ConnectionEstablished($connection));
}
protected function generateSocketId(ConnectionInterface $connection)

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()