From 193eb5eb4f37270badf61334781697f1bdecfdf0 Mon Sep 17 00:00:00 2001 From: freek Date: Fri, 23 Nov 2018 22:46:09 +0100 Subject: [PATCH] verrryyyy wip --- config/config.php | 30 ++++- src/Console/StartWebSocketServer.php | 3 +- .../Http/Controllers/EchoController.php | 1 + .../Http/Controllers/TriggerEvent.php | 1 + .../Pusher/Exceptions/PusherException.php | 1 - .../Pusher/Exceptions/UnknownAppKey.php | 3 +- src/Router.php | 8 +- src/Server/Logger.php | 121 ++++++++++++++++++ src/Server/OriginCheck.php | 1 + src/Server/WebSocketServer.php | 20 +++ 10 files changed, 184 insertions(+), 5 deletions(-) create mode 100644 src/Server/Logger.php diff --git a/config/config.php b/config/config.php index d2498b4..5461cab 100644 --- a/config/config.php +++ b/config/config.php @@ -2,7 +2,12 @@ return [ - 'allowedOrigins' => [], + /* + * TODO: add the laravel style comment here + */ + 'allowedOrigins' => [ + + ], /* * Define the optional SSL context for your websocket connections. @@ -29,4 +34,27 @@ return [ 'passphrase' => null ], + /* + * TODO:: add client config + * + * Default: one item in the array with env PUSHER_APP_ID, _KEY, _SECRET + * + * Add notice app id should be numeric + * + * "clients": [ + { + "appId": "cbf9b001405e51d4", + "key": "d886dd1900a5911d00996b41638d7026" + "secret": + } + ], +` + * + 'clients' => [ + ...[] + ], + + 'client_provider' => ConfigProvider + */ + ]; \ No newline at end of file diff --git a/src/Console/StartWebSocketServer.php b/src/Console/StartWebSocketServer.php index d5750c0..0e857e8 100644 --- a/src/Console/StartWebSocketServer.php +++ b/src/Console/StartWebSocketServer.php @@ -22,7 +22,6 @@ class StartWebSocketServer extends Command // TODO: add flag for verbose mode, to send more things to console $websocketServer = $this->createWebsocketServer(); - $websocketServer->run(); } @@ -39,6 +38,8 @@ class StartWebSocketServer extends Command return (new WebSocketServer($routes)) ->setHost($this->option('host')) ->setPort($this->option('port')) + ->setConsoleOutput($this->output) + ->enableLogging(config('app.debug')) ->setLoop($loop); } } \ No newline at end of file diff --git a/src/LaravelEcho/Http/Controllers/EchoController.php b/src/LaravelEcho/Http/Controllers/EchoController.php index 0edaa63..9199800 100644 --- a/src/LaravelEcho/Http/Controllers/EchoController.php +++ b/src/LaravelEcho/Http/Controllers/EchoController.php @@ -75,6 +75,7 @@ abstract class EchoController implements HttpServerInterface public function verifyAppId(string $appId) { + /** TODO: use client config from config file */ if ($appId !== config('broadcasting.connections.pusher.app_id')) { throw new HttpException(401, 'Invalid App ID provided.'); } diff --git a/src/LaravelEcho/Http/Controllers/TriggerEvent.php b/src/LaravelEcho/Http/Controllers/TriggerEvent.php index 4f50d52..d338a0f 100644 --- a/src/LaravelEcho/Http/Controllers/TriggerEvent.php +++ b/src/LaravelEcho/Http/Controllers/TriggerEvent.php @@ -35,6 +35,7 @@ class TriggerEvent extends EchoController "&auth_version={$request->get('auth_version')}". "&body_md5={$bodyMd5}"; + /** TODO: use client config from config file */ $authSignature = hash_hmac('sha256', $signature, config('broadcasting.connections.pusher.secret')); if ($authSignature !== $request->get('auth_signature')) { diff --git a/src/LaravelEcho/Pusher/Exceptions/PusherException.php b/src/LaravelEcho/Pusher/Exceptions/PusherException.php index 0e32252..12c2933 100644 --- a/src/LaravelEcho/Pusher/Exceptions/PusherException.php +++ b/src/LaravelEcho/Pusher/Exceptions/PusherException.php @@ -6,7 +6,6 @@ use Exception; class PusherException extends Exception { - public function getPayload() { return [ diff --git a/src/LaravelEcho/Pusher/Exceptions/UnknownAppKey.php b/src/LaravelEcho/Pusher/Exceptions/UnknownAppKey.php index fc1de78..76e856f 100644 --- a/src/LaravelEcho/Pusher/Exceptions/UnknownAppKey.php +++ b/src/LaravelEcho/Pusher/Exceptions/UnknownAppKey.php @@ -6,7 +6,8 @@ class UnknownAppKey extends PusherException { public function __construct(string $appKey) { - $this->message = "Could not find app key {$appKey}"; + $this->message = "Could not find app key `{$appKey}`."; + $this->code = 4001; } } \ No newline at end of file diff --git a/src/Router.php b/src/Router.php index 9bdd167..da9aa99 100644 --- a/src/Router.php +++ b/src/Router.php @@ -2,6 +2,7 @@ namespace BeyondCode\LaravelWebSockets; +use BeyondCode\LaravelWebSockets\Server\Logger; use Ratchet\WebSocket\WsServer; use Symfony\Component\Routing\Route; use Ratchet\Http\HttpServerInterface; @@ -81,7 +82,12 @@ class Router protected function wrapAction($action) { if (is_subclass_of($action, WebSocketController::class)) { - return new WsServer(app($action)); + + $app = app($action); + + $appThatLogs = new Logger($app); + + return new WsServer($appThatLogs); } return app($action); diff --git a/src/Server/Logger.php b/src/Server/Logger.php new file mode 100644 index 0000000..ef54d1e --- /dev/null +++ b/src/Server/Logger.php @@ -0,0 +1,121 @@ +app = $app; + + /* + $this->consoleOutput = $consoleOutput; + + $this->enabled = $enabled; + */ + } + + public function enable() + { + $this->enabled = true; + } + + public function onOpen(ConnectionInterface $connection) + { + $request = $connection->httpRequest; + + $queryParameters = []; + parse_str($request->getUri()->getQuery(), $queryParameters); + + $this->warn("New connection opened for app key {$queryParameters['appKey']}."); + + $this->app->onOpen($connection); + } + + public function onMessage(ConnectionInterface $connection, MessageInterface $message) + { + $this->info("{$connection->appId}: connection id {$connection->socketId} received message: {$message->getPayload()}."); + + $this->app->onMessage($connection, $message); + } + + public function onClose(ConnectionInterface $connection) + { + $this->warn("{$connection->appId}: connection id {$connection->socketId} closed."); + + $this->app->onClose($connection); + } + + public function onError(ConnectionInterface $connection, Exception $exception) + { + $exceptionClass = get_class($exception); + + $message = "{$connection->appId}: execption `{$exceptionClass}` thrown: `{$exception->getMessage()}`"; + + /* + * TODO: add verbose option + if ($this->isVerbose) { + $message .= $exception->getTraceAsString(); + } + */ + + $this->error($message); + + $this->app->onError($connection, $exception); + } + + protected function info(string $message) + { + $this->line($message, 'info'); + } + + protected function warn(string $message) + { + $this->line($message, 'warning'); + } + + protected function error(string $message) + { + $this->line($message, 'error'); + } + + protected function line(string $message, string $style) + { + echo $message; + + return; + + $styled = $style ? "<$style>$message" : $message; + + $this->consoleOutput->writeln($styled); + } + + + + +} \ No newline at end of file diff --git a/src/Server/OriginCheck.php b/src/Server/OriginCheck.php index 1d19a14..7b8bfdb 100644 --- a/src/Server/OriginCheck.php +++ b/src/Server/OriginCheck.php @@ -21,6 +21,7 @@ class OriginCheck implements HttpServerInterface public function __construct(MessageComponentInterface $component, array $allowedOrigins = []) { $this->_component = $component; + $this->allowedOrigins = $allowedOrigins; } diff --git a/src/Server/WebSocketServer.php b/src/Server/WebSocketServer.php index 47ec959..a457eea 100644 --- a/src/Server/WebSocketServer.php +++ b/src/Server/WebSocketServer.php @@ -9,6 +9,7 @@ use Ratchet\Http\HttpServer; 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; @@ -27,6 +28,11 @@ class WebSocketServer /** @var \Symfony\Component\Routing\RouteCollection */ protected $routes; + /** @var Symfony\Component\Console\Output\OutputInterface */ + protected $consoleOutput; + + protected $enableLogging = false; + public function __construct(RouteCollection $routes) { $this->loop = LoopFactory::create(); @@ -55,6 +61,20 @@ class WebSocketServer return $this; } + public function setConsoleOutput(OutputInterface $consoleOutput) + { + $this->consoleOutput = $consoleOutput; + + return $this; + } + + public function enableLogging($enableLogging = true) + { + $this->enableLogging = $enableLogging; + + return $this; + } + public function run() { $server = $this->createServer();