Merge branch 'master' of github.com:beyondcode/laravel-websockets
This commit is contained in:
commit
33a3145911
|
|
@ -1,8 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace BeyondCode\LaravelWebSockets\Dashboard;
|
|
||||||
|
|
||||||
class EventHandler
|
|
||||||
{
|
|
||||||
//TODO: listen for events, pass them to Dashboard:: methods
|
|
||||||
}
|
|
||||||
|
|
@ -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');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
namespace BeyondCode\LaravelWebSockets\LaravelEcho\Http\Controllers;
|
namespace BeyondCode\LaravelWebSockets\LaravelEcho\Http\Controllers;
|
||||||
|
|
||||||
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Dashboard;
|
use BeyondCode\LaravelWebSockets\Events\ApiMessageSent;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
class TriggerEvent extends EchoController
|
class TriggerEvent extends EchoController
|
||||||
|
|
@ -12,13 +12,6 @@ class TriggerEvent extends EchoController
|
||||||
$this->ensureValidSignature($request);
|
$this->ensureValidSignature($request);
|
||||||
|
|
||||||
foreach ($request->json()->get('channels', []) as $channelId) {
|
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);
|
$channel = $this->channelManager->find($request->appId, $channelId);
|
||||||
|
|
||||||
optional($channel)->broadcastToEveryoneExcept([
|
optional($channel)->broadcastToEveryoneExcept([
|
||||||
|
|
@ -26,6 +19,13 @@ class TriggerEvent extends EchoController
|
||||||
'event' => $request->json()->get('name'),
|
'event' => $request->json()->get('name'),
|
||||||
'data' => $request->json()->get('data'),
|
'data' => $request->json()->get('data'),
|
||||||
], $request->json()->get('socket_id'));
|
], $request->json()->get('socket_id'));
|
||||||
|
|
||||||
|
event(new ApiMessageSent(
|
||||||
|
$request->appId,
|
||||||
|
$channelId,
|
||||||
|
$request->json()->get('name'),
|
||||||
|
$request->json()->get('data')
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $request->json()->all();
|
return $request->json()->all();
|
||||||
|
|
|
||||||
|
|
@ -61,7 +61,6 @@ class Channel
|
||||||
|
|
||||||
if (! $this->hasConnections()) {
|
if (! $this->hasConnections()) {
|
||||||
event(new ChannelVacated($connection, $this->channelId));
|
event(new ChannelVacated($connection, $this->channelId));
|
||||||
Dashboard::vacated($connection, $this->channelId);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket;
|
namespace BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket;
|
||||||
|
|
||||||
|
use BeyondCode\LaravelWebSockets\Events\ClientMessageSent;
|
||||||
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager;
|
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager;
|
||||||
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Dashboard;
|
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Dashboard;
|
||||||
use Ratchet\ConnectionInterface;
|
use Ratchet\ConnectionInterface;
|
||||||
|
|
@ -30,7 +31,7 @@ class Message implements RespondableMessage
|
||||||
public function respond()
|
public function respond()
|
||||||
{
|
{
|
||||||
if (starts_with($this->payload->event, 'client-')) {
|
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);
|
$channel = $this->channelManager->find($this->connection->client->appId, $this->payload->channel);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket;
|
namespace BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket;
|
||||||
|
|
||||||
|
use BeyondCode\LaravelWebSockets\Events\ConnectionEstablished;
|
||||||
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Dashboard;
|
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Dashboard;
|
||||||
use BeyondCode\LaravelWebSockets\QueryParameters;
|
use BeyondCode\LaravelWebSockets\QueryParameters;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
@ -66,8 +67,6 @@ class PusherServer extends WebSocketController
|
||||||
|
|
||||||
protected function establishConnection(ConnectionInterface $connection)
|
protected function establishConnection(ConnectionInterface $connection)
|
||||||
{
|
{
|
||||||
Dashboard::connection($connection);
|
|
||||||
|
|
||||||
$connection->send(json_encode([
|
$connection->send(json_encode([
|
||||||
'event' => 'pusher:connection_established',
|
'event' => 'pusher:connection_established',
|
||||||
'data' => json_encode([
|
'data' => json_encode([
|
||||||
|
|
@ -75,6 +74,8 @@ class PusherServer extends WebSocketController
|
||||||
'activity_timeout' => 60,
|
'activity_timeout' => 60,
|
||||||
])
|
])
|
||||||
]));
|
]));
|
||||||
|
|
||||||
|
event(new ConnectionEstablished($connection));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function generateSocketId(ConnectionInterface $connection)
|
protected function generateSocketId(ConnectionInterface $connection)
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
namespace BeyondCode\LaravelWebSockets;
|
namespace BeyondCode\LaravelWebSockets;
|
||||||
|
|
||||||
|
use BeyondCode\LaravelWebSockets\Dashboard\EventSubscriber;
|
||||||
|
use Illuminate\Support\Facades\Event;
|
||||||
use Illuminate\Support\Facades\Gate;
|
use Illuminate\Support\Facades\Gate;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
use BeyondCode\LaravelWebSockets\ClientProviders\ClientProvider;
|
use BeyondCode\LaravelWebSockets\ClientProviders\ClientProvider;
|
||||||
|
|
@ -27,6 +29,8 @@ class WebSocketsServiceProvider extends ServiceProvider
|
||||||
$this->commands([
|
$this->commands([
|
||||||
Console\StartWebSocketServer::class,
|
Console\StartWebSocketServer::class,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
Event::subscribe(EventSubscriber::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function registerRoutes()
|
protected function registerRoutes()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue