verrryyyy wip

This commit is contained in:
freek 2018-11-23 22:46:09 +01:00
parent 472bd16519
commit 193eb5eb4f
10 changed files with 184 additions and 5 deletions

View File

@ -2,7 +2,12 @@
return [ return [
'allowedOrigins' => [], /*
* TODO: add the laravel style comment here
*/
'allowedOrigins' => [
],
/* /*
* Define the optional SSL context for your websocket connections. * Define the optional SSL context for your websocket connections.
@ -29,4 +34,27 @@ return [
'passphrase' => null '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
*/
]; ];

View File

@ -22,7 +22,6 @@ class StartWebSocketServer extends Command
// TODO: add flag for verbose mode, to send more things to console // TODO: add flag for verbose mode, to send more things to console
$websocketServer = $this->createWebsocketServer(); $websocketServer = $this->createWebsocketServer();
$websocketServer->run(); $websocketServer->run();
} }
@ -39,6 +38,8 @@ class StartWebSocketServer extends Command
return (new WebSocketServer($routes)) return (new WebSocketServer($routes))
->setHost($this->option('host')) ->setHost($this->option('host'))
->setPort($this->option('port')) ->setPort($this->option('port'))
->setConsoleOutput($this->output)
->enableLogging(config('app.debug'))
->setLoop($loop); ->setLoop($loop);
} }
} }

View File

@ -75,6 +75,7 @@ abstract class EchoController implements HttpServerInterface
public function verifyAppId(string $appId) public function verifyAppId(string $appId)
{ {
/** TODO: use client config from config file */
if ($appId !== config('broadcasting.connections.pusher.app_id')) { if ($appId !== config('broadcasting.connections.pusher.app_id')) {
throw new HttpException(401, 'Invalid App ID provided.'); throw new HttpException(401, 'Invalid App ID provided.');
} }

View File

@ -35,6 +35,7 @@ class TriggerEvent extends EchoController
"&auth_version={$request->get('auth_version')}". "&auth_version={$request->get('auth_version')}".
"&body_md5={$bodyMd5}"; "&body_md5={$bodyMd5}";
/** TODO: use client config from config file */
$authSignature = hash_hmac('sha256', $signature, config('broadcasting.connections.pusher.secret')); $authSignature = hash_hmac('sha256', $signature, config('broadcasting.connections.pusher.secret'));
if ($authSignature !== $request->get('auth_signature')) { if ($authSignature !== $request->get('auth_signature')) {

View File

@ -6,7 +6,6 @@ use Exception;
class PusherException extends Exception class PusherException extends Exception
{ {
public function getPayload() public function getPayload()
{ {
return [ return [

View File

@ -6,7 +6,8 @@ class UnknownAppKey extends PusherException
{ {
public function __construct(string $appKey) public function __construct(string $appKey)
{ {
$this->message = "Could not find app key {$appKey}"; $this->message = "Could not find app key `{$appKey}`.";
$this->code = 4001; $this->code = 4001;
} }
} }

View File

@ -2,6 +2,7 @@
namespace BeyondCode\LaravelWebSockets; namespace BeyondCode\LaravelWebSockets;
use BeyondCode\LaravelWebSockets\Server\Logger;
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;
@ -81,7 +82,12 @@ class Router
protected function wrapAction($action) protected function wrapAction($action)
{ {
if (is_subclass_of($action, WebSocketController::class)) { 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); return app($action);

121
src/Server/Logger.php Normal file
View File

@ -0,0 +1,121 @@
<?php
/**
* Created by PhpStorm.
* User: freek
* Date: 2018-11-23
* Time: 22:01
*/
namespace BeyondCode\LaravelWebSockets\Server;
use Exception;
use Psr\Http\Message\RequestInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Http\HttpServerInterface;
use Ratchet\RFC6455\Messaging\Message;
use Ratchet\RFC6455\Messaging\MessageInterface;
use Ratchet\WebSocket\MessageComponentInterface;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Output\OutputInterface;
class Logger implements MessageComponentInterface
{
/** @var \Ratchet\Http\HttpServerInterface */
protected $app;
/** @var \Symfony\Component\Console\Output\OutputInterface */
protected $consoleOutput;
/** @var bool */
protected $enabled;
public function __construct(MessageComponentInterface $app)
{
$this->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</$style>" : $message;
$this->consoleOutput->writeln($styled);
}
}

View File

@ -21,6 +21,7 @@ class OriginCheck implements HttpServerInterface
public function __construct(MessageComponentInterface $component, array $allowedOrigins = []) public function __construct(MessageComponentInterface $component, array $allowedOrigins = [])
{ {
$this->_component = $component; $this->_component = $component;
$this->allowedOrigins = $allowedOrigins; $this->allowedOrigins = $allowedOrigins;
} }

View File

@ -9,6 +9,7 @@ use Ratchet\Http\HttpServer;
use Ratchet\Server\IoServer; use Ratchet\Server\IoServer;
use React\EventLoop\LoopInterface; use React\EventLoop\LoopInterface;
use React\EventLoop\Factory as LoopFactory; use React\EventLoop\Factory as LoopFactory;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Routing\RequestContext; use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Matcher\UrlMatcher; use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouteCollection;
@ -27,6 +28,11 @@ class WebSocketServer
/** @var \Symfony\Component\Routing\RouteCollection */ /** @var \Symfony\Component\Routing\RouteCollection */
protected $routes; protected $routes;
/** @var Symfony\Component\Console\Output\OutputInterface */
protected $consoleOutput;
protected $enableLogging = false;
public function __construct(RouteCollection $routes) public function __construct(RouteCollection $routes)
{ {
$this->loop = LoopFactory::create(); $this->loop = LoopFactory::create();
@ -55,6 +61,20 @@ class WebSocketServer
return $this; 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() public function run()
{ {
$server = $this->createServer(); $server = $this->createServer();