wip
This commit is contained in:
parent
6768eed53f
commit
02d788ac78
|
|
@ -41,6 +41,8 @@ class StartWebSocketServer extends Command
|
||||||
// TODO: add an option to not start the echo server
|
// TODO: add an option to not start the echo server
|
||||||
WebSocketRouter::echo();
|
WebSocketRouter::echo();
|
||||||
|
|
||||||
|
// TODO: add flag for verbose mode, to send more things to console
|
||||||
|
|
||||||
(new WebsocketServer($this->option('port'), '0.0.0.0', $loop))->run();
|
(new WebsocketServer($this->option('port'), '0.0.0.0', $loop))->run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,71 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace BeyondCode\LaravelWebSockets\LaravelEcho\Pusher;
|
||||||
|
|
||||||
|
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager;
|
||||||
|
use BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket\RespondableMessage;
|
||||||
|
use Ratchet\ConnectionInterface;
|
||||||
|
use stdClass;
|
||||||
|
|
||||||
|
class PusherMessage implements RespondableMessage
|
||||||
|
{
|
||||||
|
/** @var \BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\stdClass */
|
||||||
|
protected $payload;
|
||||||
|
|
||||||
|
/** @var \React\Socket\ConnectionInterface */
|
||||||
|
protected $connection;
|
||||||
|
|
||||||
|
/** @var \BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager */
|
||||||
|
protected $channelManager;
|
||||||
|
|
||||||
|
public function __construct(stdClass $payload, ConnectionInterface $connection, ChannelManager $channelManager)
|
||||||
|
{
|
||||||
|
$this->payload = $payload;
|
||||||
|
|
||||||
|
$this->connection = $connection;
|
||||||
|
|
||||||
|
$this->channelManager = $channelManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function respond()
|
||||||
|
{
|
||||||
|
$eventName = camel_case(str_after($this->payload->event, ':'));
|
||||||
|
|
||||||
|
if (method_exists($this, $eventName)) {
|
||||||
|
call_user_func([$this, $eventName], $this->connection, $this->payload->data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @link https://pusher.com/docs/pusher_protocol#ping-pong
|
||||||
|
*
|
||||||
|
* @param ConnectionInterface $connection
|
||||||
|
* @param $payload
|
||||||
|
*/
|
||||||
|
protected function ping(ConnectionInterface $connection, $payload)
|
||||||
|
{
|
||||||
|
$connection->send(json_encode([
|
||||||
|
'event' => 'pusher:pong',
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @link https://pusher.com/docs/pusher_protocol#pusher-subscribe
|
||||||
|
*
|
||||||
|
* @param ConnectionInterface $conn
|
||||||
|
* @param $payload
|
||||||
|
*/
|
||||||
|
protected function subscribe(ConnectionInterface $connection, $payload)
|
||||||
|
{
|
||||||
|
$channel = $this->channelManager->findOrCreate($connection->appId, $payload->channel);
|
||||||
|
|
||||||
|
$channel->subscribe($connection, $payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function unsubscribe(ConnectionInterface $connection, stdClass $payload)
|
||||||
|
{
|
||||||
|
$channel = $this->channelManager->findOrCreate($connection->appId, $payload->channel);
|
||||||
|
|
||||||
|
$channel->unsubscribe($connection);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket;
|
namespace BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket;
|
||||||
|
|
||||||
|
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\PusherMessage;
|
||||||
use Ratchet\ConnectionInterface;
|
use Ratchet\ConnectionInterface;
|
||||||
use Ratchet\RFC6455\Messaging\MessageInterface;
|
use Ratchet\RFC6455\Messaging\MessageInterface;
|
||||||
use BeyondCode\LaravelWebSockets\WebSocketController;
|
use BeyondCode\LaravelWebSockets\WebSocketController;
|
||||||
|
|
@ -20,12 +21,13 @@ class EchoServer extends WebSocketController
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When a new connection is opened it will be passed to this method
|
* When a new connection is opened it will be passed to this method
|
||||||
|
*
|
||||||
* @param ConnectionInterface $conn The socket/connection that just connected to your application
|
* @param ConnectionInterface $conn The socket/connection that just connected to your application
|
||||||
|
*
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
function onOpen(ConnectionInterface $conn)
|
function onOpen(ConnectionInterface $conn)
|
||||||
{
|
{
|
||||||
|
|
||||||
dump("Client connected");
|
dump("Client connected");
|
||||||
/**
|
/**
|
||||||
* There are a couple things we need to do here:
|
* There are a couple things we need to do here:
|
||||||
|
|
@ -51,29 +53,12 @@ class EchoServer extends WebSocketController
|
||||||
]));
|
]));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onMessage(ConnectionInterface $conn, MessageInterface $msg)
|
public function onMessage(ConnectionInterface $conn, MessageInterface $message)
|
||||||
{
|
{
|
||||||
$payload = json_decode($msg->getPayload());
|
$message = RespondableMessageFactory::createForMessage($message, $conn, $this->channelManager);
|
||||||
|
|
||||||
dump("Received payload", $payload);
|
$message->respond();
|
||||||
|
|
||||||
/**
|
|
||||||
* Pusher events get a special treatment
|
|
||||||
*/
|
|
||||||
if (starts_with($payload->event, 'pusher:')) {
|
|
||||||
$event = camel_case(str_replace(':', '_', $payload->event));
|
|
||||||
|
|
||||||
if (method_exists($this, $event)) {
|
|
||||||
call_user_func([$this, $event], $conn, $payload->data);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Try to find a channel and broadcast the message to the clients.
|
|
||||||
$channel = $this->channelManager->find($conn->appId, $payload->channel);
|
|
||||||
|
|
||||||
if ($channel) {
|
|
||||||
$channel->broadcast($payload);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onClose(ConnectionInterface $connection)
|
public function onClose(ConnectionInterface $connection)
|
||||||
|
|
@ -81,36 +66,6 @@ class EchoServer extends WebSocketController
|
||||||
$this->channelManager->removeFromAllChannels($connection);
|
$this->channelManager->removeFromAllChannels($connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @link https://pusher.com/docs/pusher_protocol#ping-pong
|
|
||||||
* @param ConnectionInterface $conn
|
|
||||||
* @param $payload
|
|
||||||
*/
|
|
||||||
protected function pusherPing(ConnectionInterface $conn, $payload)
|
|
||||||
{
|
|
||||||
$conn->send($this->buildPayload('pusher:pong'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @link https://pusher.com/docs/pusher_protocol#pusher-subscribe
|
|
||||||
* @param ConnectionInterface $conn
|
|
||||||
* @param $payload
|
|
||||||
*/
|
|
||||||
protected function pusherSubscribe(ConnectionInterface $conn, $payload)
|
|
||||||
{
|
|
||||||
$channel = $this->channelManager->findOrCreate($conn->appId, $payload->channel);
|
|
||||||
|
|
||||||
$channel->subscribe($conn, $payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function pusherUnsubscribe(ConnectionInterface $connection, stdClass $payload)
|
|
||||||
{
|
|
||||||
$channel = $this->channelManager->findOrCreate($connection->appId, $payload->channel);
|
|
||||||
|
|
||||||
$channel->unsubscribe($connection);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function buildPayload($event, $data = [])
|
protected function buildPayload($event, $data = [])
|
||||||
{
|
{
|
||||||
return json_encode([
|
return json_encode([
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,39 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket;
|
||||||
|
|
||||||
|
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager;
|
||||||
|
use Ratchet\ConnectionInterface;
|
||||||
|
use stdClass;
|
||||||
|
|
||||||
|
class Message implements RespondableMessage
|
||||||
|
{
|
||||||
|
/** \stdClass */
|
||||||
|
protected $payload;
|
||||||
|
|
||||||
|
/** @var \Ratchet\ConnectionInterface */
|
||||||
|
protected $connection;
|
||||||
|
|
||||||
|
/** @var \BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager */
|
||||||
|
protected $channelManager;
|
||||||
|
|
||||||
|
public function __construct(stdClass $payload, ConnectionInterface $connection, ChannelManager $channelManager)
|
||||||
|
{
|
||||||
|
$this->payload = $payload;
|
||||||
|
|
||||||
|
$this->connection = $connection;
|
||||||
|
|
||||||
|
$this->channelManager = $channelManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function respond()
|
||||||
|
{
|
||||||
|
$channel = $this->channelManager->find($this->connection->appId, $this->payload->channel);
|
||||||
|
|
||||||
|
if (!$channel) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$channel->broadcast($this->payload);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
<?php
|
||||||
|
namespace BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket;
|
||||||
|
|
||||||
|
interface RespondableMessage
|
||||||
|
{
|
||||||
|
public function respond();
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,24 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket;
|
||||||
|
|
||||||
|
|
||||||
|
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager;
|
||||||
|
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\PusherMessage;
|
||||||
|
use Ratchet\ConnectionInterface;
|
||||||
|
use Ratchet\RFC6455\Messaging\MessageInterface;
|
||||||
|
|
||||||
|
class RespondableMessageFactory
|
||||||
|
{
|
||||||
|
public static function createForMessage(
|
||||||
|
MessageInterface $message,
|
||||||
|
ConnectionInterface $connection,
|
||||||
|
ChannelManager $channelManager): RespondableMessage
|
||||||
|
{
|
||||||
|
$payload = json_decode($message->getPayload());
|
||||||
|
|
||||||
|
return starts_with($payload->event, 'pusher:')
|
||||||
|
? new PusherMessage($payload, $connection, $channelManager)
|
||||||
|
: new Message($payload, $connection, $channelManager);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue