A more default settings
This commit is contained in:
parent
7e98657c23
commit
6f115a2a72
|
|
@ -170,7 +170,7 @@ return [
|
||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
'delete_statistics_older_than_days' => 60,
|
'delete_statistics_older_than_days' => 7,
|
||||||
|
|
||||||
],
|
],
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,8 @@ class StartServer extends Command
|
||||||
protected $signature = 'websockets:serve
|
protected $signature = 'websockets:serve
|
||||||
{--host=0.0.0.0}
|
{--host=0.0.0.0}
|
||||||
{--port=6001}
|
{--port=6001}
|
||||||
{--disable-statistics : Disable the statistics tracking.}
|
{--cache-driver=file : The cache driver to use for the server. Redis will not work due to concurrency issues.}
|
||||||
|
{--disable-statistics=true : Disable the statistics tracking.}
|
||||||
{--statistics-interval= : The amount of seconds to tick between statistics saving.}
|
{--statistics-interval= : The amount of seconds to tick between statistics saving.}
|
||||||
{--debug : Forces the loggers to be enabled and thereby overriding the APP_DEBUG setting.}
|
{--debug : Forces the loggers to be enabled and thereby overriding the APP_DEBUG setting.}
|
||||||
{--loop : Programatically inject the loop.}
|
{--loop : Programatically inject the loop.}
|
||||||
|
|
@ -73,6 +74,21 @@ class StartServer extends Command
|
||||||
*/
|
*/
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
|
$this->components->info('Handling websocket server with pid ' . getmypid() . '...');
|
||||||
|
|
||||||
|
// For is_fork() helper
|
||||||
|
if (! defined('LARAVEL_PARENT_PID')) {
|
||||||
|
define('LARAVEL_PARENT_PID', getmypid());
|
||||||
|
}
|
||||||
|
|
||||||
|
// For is_websocket() helper
|
||||||
|
if (! defined('LARAVEL_IS_WEBSOCKET')) {
|
||||||
|
define('LARAVEL_IS_WEBSOCKET', true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fixes redis concurrency issues
|
||||||
|
config(['cache.default' => $this->option('cache-driver', 'file')]);
|
||||||
|
|
||||||
$this->laravel->singleton(LoopInterface::class, function () {
|
$this->laravel->singleton(LoopInterface::class, function () {
|
||||||
return $this->loop;
|
return $this->loop;
|
||||||
});
|
});
|
||||||
|
|
@ -182,7 +198,7 @@ class StartServer extends Command
|
||||||
// then stopping the loop.
|
// then stopping the loop.
|
||||||
|
|
||||||
if (! extension_loaded('pcntl')) {
|
if (! extension_loaded('pcntl')) {
|
||||||
return;
|
throw new \RuntimeException('The pcntl extension is required to handle concurrency.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->loop->addSignal(SIGTERM, function () {
|
$this->loop->addSignal(SIGTERM, function () {
|
||||||
|
|
@ -279,7 +295,8 @@ class StartServer extends Command
|
||||||
protected function buildServer()
|
protected function buildServer()
|
||||||
{
|
{
|
||||||
$this->server = new ServerFactory(
|
$this->server = new ServerFactory(
|
||||||
$this->option('host'), $this->option('port')
|
$this->option('host'),
|
||||||
|
$this->option('port')
|
||||||
);
|
);
|
||||||
|
|
||||||
if ($loop = $this->option('loop')) {
|
if ($loop = $this->option('loop')) {
|
||||||
|
|
@ -301,7 +318,8 @@ class StartServer extends Command
|
||||||
protected function getLastRestart()
|
protected function getLastRestart()
|
||||||
{
|
{
|
||||||
return Cache::get(
|
return Cache::get(
|
||||||
'blax:websockets:restart', 0
|
'blax:websockets:restart',
|
||||||
|
0
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,36 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace BlaxSoftware\LaravelWebSockets\Events;
|
||||||
|
|
||||||
|
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||||
|
use Illuminate\Broadcasting\PrivateChannel;
|
||||||
|
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
|
||||||
|
use Illuminate\Foundation\Events\Dispatchable;
|
||||||
|
|
||||||
|
|
||||||
|
class WebsocketMessageEvent implements ShouldBroadcastNow
|
||||||
|
{
|
||||||
|
use Dispatchable, InteractsWithSockets;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
public string $event,
|
||||||
|
public $data
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function broadcastOn()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
new PrivateChannel('websocket'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function broadcastWith()
|
||||||
|
{
|
||||||
|
return $this->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function broadcastAs()
|
||||||
|
{
|
||||||
|
return $this->event;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,22 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace BlaxSoftware\LaravelWebSockets\Services;
|
||||||
|
|
||||||
|
use BlaxSoftware\LaravelWebSockets\Events\WebsocketMessageEvent;
|
||||||
|
|
||||||
|
class WebsocketService
|
||||||
|
{
|
||||||
|
public static function send($data)
|
||||||
|
{
|
||||||
|
// TODO make work to send via websocket from anywhere
|
||||||
|
// WebsocketMessageEvent::dispatch(
|
||||||
|
// optional(optional(tenant())->tenantable)->public_id,
|
||||||
|
// $d['event'],
|
||||||
|
// (is_array($d['data']))
|
||||||
|
// ? $d['data']
|
||||||
|
// : ['data' => $d['data']]
|
||||||
|
// );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -47,6 +47,8 @@ class WebSocketsServiceProvider extends ServiceProvider
|
||||||
__DIR__.'/Websocket' => app_path('Websocket')
|
__DIR__.'/Websocket' => app_path('Websocket')
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$this->registerDefaultWebsocketChannels();
|
||||||
|
|
||||||
$this->registerEventLoop();
|
$this->registerEventLoop();
|
||||||
|
|
||||||
$this->registerSQLiteDatabase();
|
$this->registerSQLiteDatabase();
|
||||||
|
|
@ -78,6 +80,17 @@ class WebSocketsServiceProvider extends ServiceProvider
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers Broadcast::channel('websocket', fn () => true); in channels.php
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function registerDefaultWebsocketChannels()
|
||||||
|
{
|
||||||
|
\Illuminate\Support\Facades\Broadcast::channel('websocket', fn() => true);
|
||||||
|
}
|
||||||
|
|
||||||
protected function registerEventLoop()
|
protected function registerEventLoop()
|
||||||
{
|
{
|
||||||
$this->app->singleton(LoopInterface::class, function () {
|
$this->app->singleton(LoopInterface::class, function () {
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ use BlaxSoftware\LaravelWebSockets\Server\Exceptions\WebSocketException as Excep
|
||||||
use BlaxSoftware\LaravelWebSockets\Server\Messages\PusherMessageFactory;
|
use BlaxSoftware\LaravelWebSockets\Server\Messages\PusherMessageFactory;
|
||||||
use BlaxSoftware\LaravelWebSockets\Server\QueryParameters;
|
use BlaxSoftware\LaravelWebSockets\Server\QueryParameters;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
use Ratchet\ConnectionInterface;
|
use Ratchet\ConnectionInterface;
|
||||||
|
|
@ -160,14 +161,6 @@ class Handler implements MessageComponentInterface
|
||||||
*/
|
*/
|
||||||
public function onClose(ConnectionInterface $connection): void
|
public function onClose(ConnectionInterface $connection): void
|
||||||
{
|
{
|
||||||
if (optional($connection)->tenant) {
|
|
||||||
if (optional($connection->tenant)->tenantable) {
|
|
||||||
$connection->tenant->tenantable->logActivity('Disconnected from websocket', $connection->tenant->tenantable, 'info', 'websocket');
|
|
||||||
} else {
|
|
||||||
$connection->tenant->logActivity('Disconnected from websocket', $connection->tenant, 'info', 'websocket');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// remove connection from $channel_connections
|
// remove connection from $channel_connections
|
||||||
foreach ($this->channel_connections as $channel => $connections) {
|
foreach ($this->channel_connections as $channel => $connections) {
|
||||||
if (in_array($connection->socketId, $connections)) {
|
if (in_array($connection->socketId, $connections)) {
|
||||||
|
|
@ -413,22 +406,6 @@ class Handler implements MessageComponentInterface
|
||||||
request()->offsetUnset($key);
|
request()->offsetUnset($key);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (optional($connection)->tenant) {
|
|
||||||
request()->merge([
|
|
||||||
'tenant' => $connection->tenant ?? null,
|
|
||||||
'tenantable' => $connection->tenant->tenantable ?? null,
|
|
||||||
'user' => optional($connection->tenant)->tenantable instanceof \App\Models\User ? $connection->tenant->tenantable : null,
|
|
||||||
'organization' => optional($connection->tenant)->organization,
|
|
||||||
'organization_id' => optional($connection->tenant)->organization_id,
|
|
||||||
]);
|
|
||||||
} else {
|
|
||||||
request()->offsetUnset('tenant');
|
|
||||||
request()->offsetUnset('tenantable');
|
|
||||||
request()->offsetUnset('user');
|
|
||||||
request()->offsetUnset('organization');
|
|
||||||
request()->offsetUnset('organization_id');
|
|
||||||
}
|
|
||||||
|
|
||||||
request()->merge(@$message['data'] ?? []);
|
request()->merge(@$message['data'] ?? []);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -455,8 +432,14 @@ class Handler implements MessageComponentInterface
|
||||||
|
|
||||||
// Set auth or logout
|
// Set auth or logout
|
||||||
($connection->user)
|
($connection->user)
|
||||||
? auth()->login($connection->user)
|
? Auth::login($connection->user)
|
||||||
: auth()->logout();
|
: Auth::logout();
|
||||||
|
|
||||||
|
if (Auth::user()) {
|
||||||
|
/** @var \App\Models\User */
|
||||||
|
$user = Auth::user();
|
||||||
|
$user->refresh();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function addDataCheckLoop(
|
private function addDataCheckLoop(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue