This commit is contained in:
Marcel Pociot 2018-11-27 21:06:04 +01:00
parent 72ae8f42dc
commit 15aa70d7c9
4 changed files with 11 additions and 103 deletions

View File

@ -7,7 +7,8 @@ use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchChannel;
use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchChannels;
use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchUsers;
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 Symfony\Component\Routing\Route;
use Ratchet\Http\HttpServerInterface;
@ -70,7 +71,7 @@ class Router
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/{channelName}', FetchChannel::class);
@ -85,7 +86,7 @@ class Router
*/
protected function wrapAction($action)
{
if (is_subclass_of($action, WebSocketController::class)) {
if (is_subclass_of($action, MessageComponentInterface::class)) {
$app = app($action);
if (MessageLogger::isEnabled()) {

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

View File

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