nitpicks
This commit is contained in:
parent
7393006666
commit
140d6d9951
|
|
@ -9,7 +9,7 @@ use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager;
|
||||||
|
|
||||||
class EchoServer extends WebSocketController
|
class EchoServer extends WebSocketController
|
||||||
{
|
{
|
||||||
/** @var ChannelManager */
|
/** @var \BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager */
|
||||||
protected $channelManager;
|
protected $channelManager;
|
||||||
|
|
||||||
public function __construct(ChannelManager $channelManager)
|
public function __construct(ChannelManager $channelManager)
|
||||||
|
|
@ -17,16 +17,8 @@ class EchoServer extends WebSocketController
|
||||||
$this->channelManager = $channelManager;
|
$this->channelManager = $channelManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
function onOpen(ConnectionInterface $connection)
|
||||||
* 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
|
|
||||||
*
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
function onOpen(ConnectionInterface $conn)
|
|
||||||
{
|
{
|
||||||
dump("Client connected");
|
|
||||||
/**
|
/**
|
||||||
* There are a couple things we need to do here:
|
* There are a couple things we need to do here:
|
||||||
* 1. Authenticate the incoming request by validating the provided APP-ID is known to us (JSON file lookup?)
|
* 1. Authenticate the incoming request by validating the provided APP-ID is known to us (JSON file lookup?)
|
||||||
|
|
@ -35,19 +27,22 @@ class EchoServer extends WebSocketController
|
||||||
$socketId = sprintf("%d.%d", getmypid(), random_int(1, 100000000));
|
$socketId = sprintf("%d.%d", getmypid(), random_int(1, 100000000));
|
||||||
|
|
||||||
// Store the socketId along with the connection so we can retrieve it.
|
// Store the socketId along with the connection so we can retrieve it.
|
||||||
$conn->socketId = $socketId;
|
$connection->socketId = $socketId;
|
||||||
|
|
||||||
/** @var \GuzzleHttp\Psr7\Request $request */
|
/** @var \GuzzleHttp\Psr7\Request $request */
|
||||||
$request = $conn->httpRequest;
|
$request = $connection->httpRequest;
|
||||||
|
|
||||||
$queryParameters = [];
|
$queryParameters = [];
|
||||||
parse_str($request->getUri()->getQuery(), $queryParameters);
|
parse_str($request->getUri()->getQuery(), $queryParameters);
|
||||||
|
|
||||||
$conn->appId = $queryParameters['appId'];
|
$connection->appId = $queryParameters['appId'];
|
||||||
|
|
||||||
$conn->send($this->buildPayload('pusher:connection_established', [
|
$connection->send(json_encode([
|
||||||
'socket_id' => $socketId,
|
'event' => 'pusher:connection_established',
|
||||||
'activity_timeout' => 60,
|
'data' => json_encode([
|
||||||
|
'socket_id' => $socketId,
|
||||||
|
'activity_timeout' => 60,
|
||||||
|
])
|
||||||
]));
|
]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -56,19 +51,10 @@ class EchoServer extends WebSocketController
|
||||||
$message = RespondableMessageFactory::createForMessage($message, $conn, $this->channelManager);
|
$message = RespondableMessageFactory::createForMessage($message, $conn, $this->channelManager);
|
||||||
|
|
||||||
$message->respond();
|
$message->respond();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onClose(ConnectionInterface $connection)
|
public function onClose(ConnectionInterface $connection)
|
||||||
{
|
{
|
||||||
$this->channelManager->removeFromAllChannels($connection);
|
$this->channelManager->removeFromAllChannels($connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function buildPayload($event, $data = [])
|
|
||||||
{
|
|
||||||
return json_encode([
|
|
||||||
'event' => $event,
|
|
||||||
'data' => json_encode($data)
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
@ -30,10 +30,6 @@ class Message implements RespondableMessage
|
||||||
{
|
{
|
||||||
$channel = $this->channelManager->find($this->connection->appId, $this->payload->channel);
|
$channel = $this->channelManager->find($this->connection->appId, $this->payload->channel);
|
||||||
|
|
||||||
if (!$channel) {
|
optional($channel)->broadcast($this->payload);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$channel->broadcast($this->payload);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
namespace BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket;
|
namespace BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket;
|
||||||
|
|
||||||
|
|
||||||
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager;
|
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager;
|
||||||
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\PusherMessage;
|
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\PusherMessage;
|
||||||
use Ratchet\ConnectionInterface;
|
use Ratchet\ConnectionInterface;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue