wip
This commit is contained in:
parent
92362ba918
commit
8faa7bf3f1
|
|
@ -25,10 +25,10 @@
|
||||||
"php": "^7.1",
|
"php": "^7.1",
|
||||||
"ext-json": "*",
|
"ext-json": "*",
|
||||||
"cboden/ratchet": "^0.4.1",
|
"cboden/ratchet": "^0.4.1",
|
||||||
"illuminate/console": "5.6.*|5.7.*",
|
"illuminate/console": "5.7.*",
|
||||||
"illuminate/http": "5.6.*|5.7.*",
|
"illuminate/http": "5.7.*",
|
||||||
"illuminate/routing": "5.6.*|5.7.*",
|
"illuminate/routing": "5.7.*",
|
||||||
"illuminate/support": "5.6.*|5.7.*",
|
"illuminate/support": "5.7.*",
|
||||||
"symfony/http-kernel": "~4.0",
|
"symfony/http-kernel": "~4.0",
|
||||||
"symfony/psr-http-message-bridge": "^1.1"
|
"symfony/psr-http-message-bridge": "^1.1"
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,10 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use BeyondCode\LaravelWebSockets\Http\Middleware\Authorize;
|
||||||
use BeyondCode\LaravelWebSockets\ClientProviders\ConfigClientProvider;
|
use BeyondCode\LaravelWebSockets\ClientProviders\ConfigClientProvider;
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
|
||||||
/*
|
|
||||||
* Path for the Websockets debug console
|
|
||||||
*/
|
|
||||||
'path' => '/websockets',
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* TODO: add the laravel style comment here
|
* TODO: add the laravel style comment here
|
||||||
*/
|
*/
|
||||||
|
|
@ -49,6 +45,7 @@ return [
|
||||||
*/
|
*/
|
||||||
'clients' => [
|
'clients' => [
|
||||||
[
|
[
|
||||||
|
'name' => env('APP_NAME'),
|
||||||
'app_id' => env('WEBSOCKETS_APP_ID'),
|
'app_id' => env('WEBSOCKETS_APP_ID'),
|
||||||
'app_key' => env('WEBSOCKETS_APP_KEY'),
|
'app_key' => env('WEBSOCKETS_APP_KEY'),
|
||||||
'app_secret' => env('WEBSOCKETS_APP_SECRET')
|
'app_secret' => env('WEBSOCKETS_APP_SECRET')
|
||||||
|
|
@ -63,4 +60,20 @@ return [
|
||||||
* `ClientProvier` interface.
|
* `ClientProvier` interface.
|
||||||
*/
|
*/
|
||||||
'client_provider' => ConfigClientProvider::class,
|
'client_provider' => ConfigClientProvider::class,
|
||||||
|
|
||||||
|
'dashboard' => [
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Path for the Websockets debug console
|
||||||
|
*/
|
||||||
|
'path' => '/websockets',
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Middleware that will be applied to the dashboard routes.
|
||||||
|
*/
|
||||||
|
'middleware' => [
|
||||||
|
Authorize::class,
|
||||||
|
],
|
||||||
|
|
||||||
|
]
|
||||||
];
|
];
|
||||||
|
|
@ -18,12 +18,11 @@
|
||||||
<span class="panel-title">WebSockets Console</span>
|
<span class="panel-title">WebSockets Console</span>
|
||||||
<form id="connect" class="form-inline" role="form">
|
<form id="connect" class="form-inline" role="form">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label class="sr-only" for="appKey">App Key</label>
|
<select class="form-control" name="app" id="app">
|
||||||
<input class="form-control" value="{{ env('PUSHER_APP_KEY') }}" id="appKey" placeholder="Enter App Key">
|
@foreach ($clients as $client)
|
||||||
</div>
|
<option value="{{ $client->appKey }}">{{ $client->name }}</option>
|
||||||
<div class="form-group">
|
@endforeach
|
||||||
<label class="sr-only" for="secret">Secret</label>
|
</select>
|
||||||
<input type="password" class="form-control" id="secret" placeholder="Secret">
|
|
||||||
</div>
|
</div>
|
||||||
<button type="submit" class="btn btn-primary">Connect</button>
|
<button type="submit" class="btn btn-primary">Connect</button>
|
||||||
</form>
|
</form>
|
||||||
|
|
@ -51,9 +50,7 @@
|
||||||
<script>
|
<script>
|
||||||
$(function(){
|
$(function(){
|
||||||
$('#connect').submit(function(event) {
|
$('#connect').submit(function(event) {
|
||||||
var appKey = $('#appKey').val();
|
connect($('#app').val());
|
||||||
var secret = $('#secret').val();
|
|
||||||
connect(appKey, secret);
|
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -64,17 +61,16 @@
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
function connect(appKey, secret) {
|
function connect(appKey) {
|
||||||
pusher = new Pusher(appKey, {
|
pusher = new Pusher(appKey, {
|
||||||
wsHost: window.location.hostname,
|
wsHost: window.location.hostname,
|
||||||
wsPort: 6001,
|
wsPort: 6001,
|
||||||
wsPath: '/console',
|
authEndpoint: '{{ config('websockets.dashboard.path') }}/auth',
|
||||||
authEndpoint: '{{ config('websockets.path') }}/auth',
|
|
||||||
enabledTransports: ['ws', 'flash']
|
enabledTransports: ['ws', 'flash']
|
||||||
});
|
});
|
||||||
|
|
||||||
var channel = pusher.subscribe('private-logger-new_connection');
|
pusher.subscribe('private-logger-new_connection')
|
||||||
channel.bind('private-logger-connection', function(data) {
|
.bind('private-logger-connection', function(data) {
|
||||||
alert(JSON.stringify(data));
|
alert(JSON.stringify(data));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,9 @@ class Client
|
||||||
/** @var string */
|
/** @var string */
|
||||||
public $appSecret;
|
public $appSecret;
|
||||||
|
|
||||||
|
/** @var string|null */
|
||||||
|
public $name;
|
||||||
|
|
||||||
public static function findByAppId(int $appId)
|
public static function findByAppId(int $appId)
|
||||||
{
|
{
|
||||||
return app(ClientProvider::class)->findByAppId($appId);
|
return app(ClientProvider::class)->findByAppId($appId);
|
||||||
|
|
@ -26,7 +29,7 @@ class Client
|
||||||
return app(ClientProvider::class)->findByAppKey($appKey);
|
return app(ClientProvider::class)->findByAppKey($appKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __construct($appId, string $appKey, string $appSecret)
|
public function __construct($appId, string $appKey, string $appSecret, ?string $name)
|
||||||
{
|
{
|
||||||
if (!is_numeric($appId)) {
|
if (!is_numeric($appId)) {
|
||||||
throw InvalidClient::appIdIsNotNumeric($appId);
|
throw InvalidClient::appIdIsNotNumeric($appId);
|
||||||
|
|
@ -45,6 +48,8 @@ class Client
|
||||||
$this->appKey = $appKey;
|
$this->appKey = $appKey;
|
||||||
|
|
||||||
$this->appSecret = $appSecret;
|
$this->appSecret = $appSecret;
|
||||||
|
|
||||||
|
$this->name = $name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,4 +8,6 @@ interface ClientProvider
|
||||||
public function findByAppId(int $appId): ?Client;
|
public function findByAppId(int $appId): ?Client;
|
||||||
|
|
||||||
public function findByAppKey(string $appKey): ?Client;
|
public function findByAppKey(string $appKey): ?Client;
|
||||||
|
|
||||||
|
public function all(): array;
|
||||||
}
|
}
|
||||||
|
|
@ -24,6 +24,15 @@ class ConfigClientProvider implements ClientProvider
|
||||||
return $this->instanciate($clientAttributes);
|
return $this->instanciate($clientAttributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function all(): array
|
||||||
|
{
|
||||||
|
return $this->allClients()
|
||||||
|
->map(function ($client) {
|
||||||
|
return $this->instanciate($client);
|
||||||
|
})
|
||||||
|
->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
protected function allClients(): Collection
|
protected function allClients(): Collection
|
||||||
{
|
{
|
||||||
return collect(config('websockets.clients'));
|
return collect(config('websockets.clients'));
|
||||||
|
|
@ -38,7 +47,8 @@ class ConfigClientProvider implements ClientProvider
|
||||||
return new Client(
|
return new Client(
|
||||||
$clientAttributes['app_id'],
|
$clientAttributes['app_id'],
|
||||||
$clientAttributes['app_key'],
|
$clientAttributes['app_key'],
|
||||||
$clientAttributes['app_secret']
|
$clientAttributes['app_secret'],
|
||||||
|
$clientAttributes['name'] ?? null
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2,8 +2,8 @@
|
||||||
|
|
||||||
namespace BeyondCode\LaravelWebsockets\Http\Controllers;
|
namespace BeyondCode\LaravelWebsockets\Http\Controllers;
|
||||||
|
|
||||||
use Illuminate\Contracts\Broadcasting\Broadcaster;
|
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Contracts\Broadcasting\Broadcaster;
|
||||||
|
|
||||||
class AuthenticateConsole
|
class AuthenticateConsole
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,14 @@
|
||||||
namespace BeyondCode\LaravelWebsockets\Http\Controllers;
|
namespace BeyondCode\LaravelWebsockets\Http\Controllers;
|
||||||
|
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use BeyondCode\LaravelWebSockets\ClientProviders\ClientProvider;
|
||||||
|
|
||||||
class ShowConsole
|
class ShowConsole
|
||||||
{
|
{
|
||||||
public function __invoke(Request $request)
|
public function __invoke(Request $request, ClientProvider $clients)
|
||||||
{
|
{
|
||||||
return view('websockets::console');
|
return view('websockets::console', [
|
||||||
|
'clients' => $clients->all()
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace BeyondCode\LaravelWebsockets\Http\Middleware;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Gate;
|
||||||
|
|
||||||
|
class Authorize
|
||||||
|
{
|
||||||
|
public function handle($request, $next)
|
||||||
|
{
|
||||||
|
return Gate::check('viewWebSocketDashboard') ? $next($request) : abort(403);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,36 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket;
|
|
||||||
|
|
||||||
use Ratchet\ConnectionInterface;
|
|
||||||
|
|
||||||
class ConsoleServer extends PusherServer
|
|
||||||
{
|
|
||||||
function onOpen(ConnectionInterface $connection)
|
|
||||||
{
|
|
||||||
$this->generateSocketId($connection);
|
|
||||||
|
|
||||||
$this->verifyConnection($connection);
|
|
||||||
|
|
||||||
$this->establishConnection($connection);
|
|
||||||
|
|
||||||
// TODO check connection signature
|
|
||||||
$connection->isAdmin = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function log(string $appId, string $type, string $details)
|
|
||||||
{
|
|
||||||
$channelId = "private-logger-{$type}";
|
|
||||||
|
|
||||||
$channel = $this->channelManager->find($appId, $channelId);
|
|
||||||
|
|
||||||
optional($channel)->broadcast([
|
|
||||||
'event' => $type,
|
|
||||||
'channel' => $channelId,
|
|
||||||
'data' => [
|
|
||||||
'type' => $type,
|
|
||||||
'details' => $details
|
|
||||||
]
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -2,9 +2,6 @@
|
||||||
|
|
||||||
namespace BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket;
|
namespace BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket;
|
||||||
|
|
||||||
use BeyondCode\LaravelWebSockets\ClientProviders\Client;
|
|
||||||
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\PusherException;
|
|
||||||
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\UnknownAppKey;
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Ratchet\ConnectionInterface;
|
use Ratchet\ConnectionInterface;
|
||||||
use Ratchet\RFC6455\Messaging\MessageInterface;
|
use Ratchet\RFC6455\Messaging\MessageInterface;
|
||||||
|
|
@ -19,14 +16,9 @@ class PusherServer extends WebSocketController
|
||||||
/** @var \BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager */
|
/** @var \BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager */
|
||||||
protected $channelManager;
|
protected $channelManager;
|
||||||
|
|
||||||
/** @var ConsoleServer|null */
|
public function __construct(ChannelManager $channelManager)
|
||||||
protected $consoleServer;
|
|
||||||
|
|
||||||
public function __construct(ChannelManager $channelManager, ConsoleServer $consoleServer = null)
|
|
||||||
{
|
{
|
||||||
$this->channelManager = $channelManager;
|
$this->channelManager = $channelManager;
|
||||||
|
|
||||||
$this->consoleServer = $consoleServer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function onOpen(ConnectionInterface $connection)
|
function onOpen(ConnectionInterface $connection)
|
||||||
|
|
@ -36,8 +28,6 @@ class PusherServer extends WebSocketController
|
||||||
$this->verifyConnection($connection);
|
$this->verifyConnection($connection);
|
||||||
|
|
||||||
$this->establishConnection($connection);
|
$this->establishConnection($connection);
|
||||||
|
|
||||||
$this->consoleServer->log($connection->appId, 'new_connection', '');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onMessage(ConnectionInterface $connection, MessageInterface $message)
|
public function onMessage(ConnectionInterface $connection, MessageInterface $message)
|
||||||
|
|
@ -59,7 +49,6 @@ class PusherServer extends WebSocketController
|
||||||
$exception->getPayload()
|
$exception->getPayload()
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
dump($exception);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function verifyConnection(ConnectionInterface $connection)
|
protected function verifyConnection(ConnectionInterface $connection)
|
||||||
|
|
@ -94,4 +83,20 @@ class PusherServer extends WebSocketController
|
||||||
|
|
||||||
$connection->socketId = $socketId;
|
$connection->socketId = $socketId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function log(string $appId, string $type, string $details)
|
||||||
|
{
|
||||||
|
$channelId = "private-logger-{$type}";
|
||||||
|
|
||||||
|
$channel = $this->channelManager->find($appId, $channelId);
|
||||||
|
|
||||||
|
optional($channel)->broadcast([
|
||||||
|
'event' => $type,
|
||||||
|
'channel' => $channelId,
|
||||||
|
'data' => [
|
||||||
|
'type' => $type,
|
||||||
|
'details' => $details
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2,22 +2,26 @@
|
||||||
|
|
||||||
namespace BeyondCode\LaravelWebSockets;
|
namespace BeyondCode\LaravelWebSockets;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Gate;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
use BeyondCode\LaravelWebSockets\ClientProviders\ClientProvider;
|
use BeyondCode\LaravelWebSockets\ClientProviders\ClientProvider;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
use BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket\ConsoleServer;
|
|
||||||
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager;
|
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager;
|
||||||
|
|
||||||
class LaravelWebSocketsServiceProvider extends ServiceProvider
|
class LaravelWebSocketsServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
public function boot()
|
public function boot()
|
||||||
{
|
{
|
||||||
|
Route::middlewareGroup('websockets', config('websockets.dashboard.middleware', []));
|
||||||
|
|
||||||
$this->publishes([
|
$this->publishes([
|
||||||
__DIR__.'/../config/websockets.php' => base_path('config/websockets.php'),
|
__DIR__.'/../config/websockets.php' => base_path('config/websockets.php'),
|
||||||
], 'config');
|
], 'config');
|
||||||
|
|
||||||
$this->registerRoutes();
|
$this->registerRoutes();
|
||||||
|
|
||||||
|
$this->registerDashboardGate();
|
||||||
|
|
||||||
$this->loadViewsFrom(__DIR__.'/../resources/views/', 'websockets');
|
$this->loadViewsFrom(__DIR__.'/../resources/views/', 'websockets');
|
||||||
|
|
||||||
$this->commands([
|
$this->commands([
|
||||||
|
|
@ -36,7 +40,8 @@ class LaravelWebSocketsServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'namespace' => 'BeyondCode\LaravelWebSockets\Http\Controllers',
|
'namespace' => 'BeyondCode\LaravelWebSockets\Http\Controllers',
|
||||||
'prefix' => config('websockets.path'),
|
'prefix' => config('websockets.dashboard.path'),
|
||||||
|
'middleware' => 'websockets'
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -55,9 +60,12 @@ class LaravelWebSocketsServiceProvider extends ServiceProvider
|
||||||
$this->app->singleton(ClientProvider::class, function() {
|
$this->app->singleton(ClientProvider::class, function() {
|
||||||
return app(config('websockets.client_provider'));
|
return app(config('websockets.client_provider'));
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
$this->app->singleton(ConsoleServer::class, function() {
|
protected function registerDashboardGate()
|
||||||
return new ConsoleServer(new ChannelManager());
|
{
|
||||||
|
Gate::define('viewWebSocketDashboard', function ($user = null) {
|
||||||
|
return app()->environment('local');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -67,8 +67,6 @@ class Router
|
||||||
{
|
{
|
||||||
$this->get('/app/{appKey}', LaravelEcho\WebSocket\PusherServer::class);
|
$this->get('/app/{appKey}', LaravelEcho\WebSocket\PusherServer::class);
|
||||||
|
|
||||||
$this->get('/console/app/{appKey}', LaravelEcho\WebSocket\ConsoleServer::class);
|
|
||||||
|
|
||||||
$this->get('/apps/{appId}/channels', LaravelEcho\Http\Controllers\FetchChannels::class);
|
$this->get('/apps/{appId}/channels', LaravelEcho\Http\Controllers\FetchChannels::class);
|
||||||
$this->get('/apps/{appId}/channels/{channelName}', LaravelEcho\Http\Controllers\FetchChannel::class);
|
$this->get('/apps/{appId}/channels/{channelName}', LaravelEcho\Http\Controllers\FetchChannel::class);
|
||||||
$this->get('/apps/{appId}/channels/{channelName}/users', LaravelEcho\Http\Controllers\FetchUsers::class);
|
$this->get('/apps/{appId}/channels/{channelName}/users', LaravelEcho\Http\Controllers\FetchUsers::class);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue