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

This commit is contained in:
freek 2018-11-27 21:07:43 +01:00
commit c90822557b
6 changed files with 15 additions and 134 deletions

View File

@ -2,14 +2,14 @@
namespace BeyondCode\LaravelWebSockets\Exceptions; namespace BeyondCode\LaravelWebSockets\Exceptions;
use BeyondCode\LaravelWebSockets\Server\WebSocketController; use Ratchet\WebSocket\MessageComponentInterface;
class InvalidWebSocketController extends \Exception class InvalidWebSocketController extends \Exception
{ {
public static function withController(string $controllerClass) public static function withController(string $controllerClass)
{ {
$websocketControllerClass = WebSocketController::class; $messageComponentInterfaceClass = MessageComponentInterface::class;
return new static("Invalid WebSocket Controller provided. Expected instance of `{$websocketControllerClass}`, but received `{$controllerClass}`."); return new static("Invalid WebSocket Controller provided. Expected instance of `{$messageComponentInterfaceClass}`, but received `{$controllerClass}`.");
} }
} }

View File

@ -7,7 +7,8 @@ use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchChannel;
use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchChannels; use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchChannels;
use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchUsers; use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchUsers;
use BeyondCode\LaravelWebSockets\HttpApi\Controllers\TriggerEvent; use BeyondCode\LaravelWebSockets\HttpApi\Controllers\TriggerEvent;
use BeyondCode\LaravelWebSockets\WebSockets\Controllers\PusherController; use BeyondCode\LaravelWebSockets\WebSockets\Controllers\WebSocketHandler;
use Ratchet\WebSocket\MessageComponentInterface;
use Ratchet\WebSocket\WsServer; use Ratchet\WebSocket\WsServer;
use Symfony\Component\Routing\Route; use Symfony\Component\Routing\Route;
use Ratchet\Http\HttpServerInterface; use Ratchet\Http\HttpServerInterface;
@ -26,7 +27,7 @@ class Router
public function websocket(string $uri, $action) public function websocket(string $uri, $action)
{ {
if (!is_subclass_of($action, WebSocketController::class)) { if (!is_subclass_of($action, MessageComponentInterface::class)) {
throw InvalidWebSocketController::withController($action); throw InvalidWebSocketController::withController($action);
} }
@ -70,7 +71,7 @@ class Router
public function echo() public function echo()
{ {
$this->get('/app/{appKey}', PusherController::class); $this->get('/app/{appKey}', WebSocketHandler::class);
$this->get('/apps/{appId}/channels', FetchChannels::class); $this->get('/apps/{appId}/channels', FetchChannels::class);
$this->get('/apps/{appId}/channels/{channelName}', FetchChannel::class); $this->get('/apps/{appId}/channels/{channelName}', FetchChannel::class);
@ -86,7 +87,7 @@ class Router
*/ */
protected function wrapAction($action) protected function wrapAction($action)
{ {
if (is_subclass_of($action, WebSocketController::class)) { if (is_subclass_of($action, MessageComponentInterface::class)) {
$app = app($action); $app = app($action);
if (MessageLogger::isEnabled()) { if (MessageLogger::isEnabled()) {

View File

@ -1,27 +0,0 @@
<?php
namespace BeyondCode\LaravelWebSockets\Server;
use Exception;
use Ratchet\ConnectionInterface;
use Ratchet\RFC6455\Messaging\MessageInterface;
use Ratchet\WebSocket\MessageComponentInterface;
class WebSocketController implements MessageComponentInterface
{
function onOpen(ConnectionInterface $connection)
{
}
public function onMessage(ConnectionInterface $connection, MessageInterface $message)
{
}
function onClose(ConnectionInterface $connection)
{
}
function onError(ConnectionInterface $connection, Exception $exception)
{
}
}

View File

@ -1,93 +0,0 @@
<?php
namespace BeyondCode\LaravelWebSockets\Server;
use Ratchet\Http\Router;
use React\Socket\SecureServer;
use React\Socket\Server;
use Ratchet\Server\IoServer;
use React\EventLoop\LoopInterface;
use React\EventLoop\Factory as LoopFactory;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RouteCollection;
class WebSocketServer
{
/** @var string */
protected $host = '127.0.0.1';
/** @var int */
protected $port = 8080;
/** @var \React\EventLoop\LoopInterface */
protected $loop;
/** @var \Symfony\Component\Routing\RouteCollection */
protected $routes;
/** @var Symfony\Component\Console\Output\OutputInterface */
protected $consoleOutput;
public function __construct(RouteCollection $routes)
{
$this->loop = LoopFactory::create();
$this->routes = $routes;
}
public function setHost(string $host)
{
$this->host = $host;
return $this;
}
public function setPort(string $port)
{
$this->port = $port;
return $this;
}
public function setLoop(LoopInterface $loop)
{
$this->loop = $loop;
return $this;
}
public function setConsoleOutput(OutputInterface $consoleOutput)
{
$this->consoleOutput = $consoleOutput;
return $this;
}
public function run()
{
$server = $this->createServer();
$server->run();
}
protected function createServer(): IoServer
{
$socket = new Server("{$this->host}:{$this->port}", $this->loop);
if (config('websockets.ssl.local_cert')) {
$socket = new SecureServer($socket, $this->loop, config('websockets.ssl'));
}
$urlMatcher = new UrlMatcher($this->routes, new RequestContext);
$router = new Router($urlMatcher);
$app = new OriginCheck($router, config('websockets.allowedOrigins', []));
$httpServer = new HttpServer($app, config('websockets.maxRequestSize'));
return new IoServer($httpServer, $socket, $this->loop);
}
}

View File

@ -3,18 +3,18 @@
namespace BeyondCode\LaravelWebSockets\WebSockets\Controllers; namespace BeyondCode\LaravelWebSockets\WebSockets\Controllers;
use BeyondCode\LaravelWebSockets\Events\ConnectionEstablished; use BeyondCode\LaravelWebSockets\Events\ConnectionEstablished;
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\WebSocketException;
use BeyondCode\LaravelWebSockets\WebSockets\Messages\RespondableMessageFactory; use BeyondCode\LaravelWebSockets\WebSockets\Messages\RespondableMessageFactory;
use BeyondCode\LaravelWebSockets\QueryParameters; use BeyondCode\LaravelWebSockets\QueryParameters;
use Exception; use Exception;
use Ratchet\ConnectionInterface; use Ratchet\ConnectionInterface;
use Ratchet\RFC6455\Messaging\MessageInterface; use Ratchet\RFC6455\Messaging\MessageInterface;
use BeyondCode\LaravelWebSockets\Server\WebSocketController;
use BeyondCode\LaravelWebSockets\ClientProviders\Client; use BeyondCode\LaravelWebSockets\ClientProviders\Client;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\PusherException;
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\UnknownAppKey; use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\UnknownAppKey;
use Ratchet\WebSocket\MessageComponentInterface;
class PusherController extends WebSocketController class WebSocketHandler implements MessageComponentInterface
{ {
/** @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager */ /** @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager */
protected $channelManager; protected $channelManager;
@ -46,7 +46,7 @@ class PusherController extends WebSocketController
public function onError(ConnectionInterface $connection, Exception $exception) public function onError(ConnectionInterface $connection, Exception $exception)
{ {
if ($exception instanceof PusherException) { if ($exception instanceof WebSocketException) {
$connection->send(json_encode( $connection->send(json_encode(
$exception->getPayload() $exception->getPayload()
)); ));

View File

@ -5,19 +5,19 @@ namespace BeyondCode\LaravelWebSockets\Tests;
use BeyondCode\LaravelWebSockets\ClientProviders\Client; use BeyondCode\LaravelWebSockets\ClientProviders\Client;
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\InvalidSignature; use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\InvalidSignature;
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\UnknownAppKey; use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\UnknownAppKey;
use BeyondCode\LaravelWebSockets\WebSockets\Controllers\PusherController; use BeyondCode\LaravelWebSockets\WebSockets\Controllers\WebSocketHandler;
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message; use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
class ConnectionTest extends TestCase class ConnectionTest extends TestCase
{ {
/** @var \BeyondCode\LaravelWebSockets\WebSockets\Controllers\PusherController */ /** @var \BeyondCode\LaravelWebSockets\WebSockets\Controllers\WebSocketHandler */
protected $pusherServer; protected $pusherServer;
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
$this->pusherServer = app(PusherController::class); $this->pusherServer = app(WebSocketHandler::class);
} }
/** @test */ /** @test */