add broadcast

This commit is contained in:
iVampireSP.com 2022-09-22 12:18:35 +08:00
parent c4e71be735
commit 4f5073e6e1
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
13 changed files with 403 additions and 51 deletions

View File

@ -1,16 +0,0 @@
<?php
namespace App\Events;
class ExampleEvent extends Event
{
/**
* Create a new event instance.
*
* @return void
*/
public function __construct()
{
//
}
}

39
app/Events/UserEvent.php Normal file
View File

@ -0,0 +1,39 @@
<?php
namespace App\Events;
use App\Models\User;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Support\Facades\Log;
class UserEvent extends Event implements ShouldBroadcastNow
{
use SerializesModels;
public User $user;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(User $user)
{
//
$this->user = $user;
}
public function broadcastOn()
{
return new PrivateChannel('users.' . $this->user->id);
}
public function broadcastAs()
{
return 'user';
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Broadcast;
class BroadcastController extends Controller
{
public function authenticate(Request $request)
{
return Broadcast::auth($request);
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Log;
class LogAllRequest
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
Log::debug('request', [
'method' => $request->getMethod(),
'path' => $request->path(),
'data' => $request->all(),
]);
// Pre-Middleware Action
$response = $next($request);
// Post-Middleware Action
// Log::debug('response', $response->content);
return $response;
}
}

View File

@ -1,31 +0,0 @@
<?php
namespace App\Listeners;
use App\Events\ExampleEvent;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
class ExampleListener
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param \App\Events\ExampleEvent $event
* @return void
*/
public function handle(ExampleEvent $event)
{
//
}
}

View File

@ -5,6 +5,7 @@
use Illuminate\Support\ServiceProvider;
use Alipay\EasySDK\Kernel\Config as AlipayConfig;
use Alipay\EasySDK\Kernel\Factory as AlipayFactory;
use Illuminate\Support\Facades\Broadcast;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;

View File

@ -0,0 +1,13 @@
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class BroadcastServiceProvider extends ServiceProvider
{
public function boot()
{
require base_path('routes/channels.php');
}
}

View File

@ -76,6 +76,7 @@
$app->configure('alipay');
$app->configure('drops');
$app->configure('forum');
$app->configure('broadcasting');
/*
|--------------------------------------------------------------------------
@ -91,6 +92,7 @@
$app->middleware([
// App\Http\Middleware\ExampleMiddleware::class
\App\Http\Middleware\AllowCors::class,
// \App\Http\Middleware\LogAllRequest::class,
]);
$app->routeMiddleware([
@ -117,10 +119,13 @@
$app->register(Illuminate\Redis\RedisServiceProvider::class);
$app->register(Hhxsv5\LaravelS\Illuminate\LaravelSServiceProvider::class);
$app->register(Flipbox\LumenGenerator\LumenGeneratorServiceProvider::class);
// $app->register(App\Providers\EventServiceProvider::class);
$app->register(App\Providers\EventServiceProvider::class);
$app->register(\Anik\Form\FormRequestServiceProvider::class);
$app->register(App\Providers\RouteBindingServiceProvider::class);
// broadcast
$app->register(App\Providers\BroadcastServiceProvider::class);
/*
|--------------------------------------------------------------------------
@ -155,5 +160,4 @@
require __DIR__ . '/../routes/remote.php';
});
return $app;

View File

@ -22,7 +22,8 @@
"league/flysystem": "^3.2",
"mmghv/lumen-route-binding": "^1.0",
"mongodb/mongodb": "^1.13",
"predis/predis": "^2.0"
"predis/predis": "^2.0",
"pusher/pusher-php-server": "^7.0"
},
"require-dev": {
"fakerphp/faker": "^1.9.1",

217
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "22a158b343410ac17bf6d2ee94827c2c",
"content-hash": "8e55271fe6968a10880e3c206658b974",
"packages": [
{
"name": "adbario/php-dot-notation",
@ -4527,6 +4527,154 @@
],
"time": "2022-08-01T11:03:24+00:00"
},
{
"name": "paragonie/random_compat",
"version": "v9.99.100",
"source": {
"type": "git",
"url": "https://github.com/paragonie/random_compat.git",
"reference": "996434e5492cb4c3edcb9168db6fbb1359ef965a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/paragonie/random_compat/zipball/996434e5492cb4c3edcb9168db6fbb1359ef965a",
"reference": "996434e5492cb4c3edcb9168db6fbb1359ef965a",
"shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
},
"require": {
"php": ">= 7"
},
"require-dev": {
"phpunit/phpunit": "4.*|5.*",
"vimeo/psalm": "^1"
},
"suggest": {
"ext-libsodium": "Provides a modern crypto API that can be used to generate random bytes."
},
"type": "library",
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Paragon Initiative Enterprises",
"email": "security@paragonie.com",
"homepage": "https://paragonie.com"
}
],
"description": "PHP 5.x polyfill for random_bytes() and random_int() from PHP 7",
"keywords": [
"csprng",
"polyfill",
"pseudorandom",
"random"
],
"support": {
"email": "info@paragonie.com",
"issues": "https://github.com/paragonie/random_compat/issues",
"source": "https://github.com/paragonie/random_compat"
},
"time": "2020-10-15T08:29:30+00:00"
},
{
"name": "paragonie/sodium_compat",
"version": "v1.18.0",
"source": {
"type": "git",
"url": "https://github.com/paragonie/sodium_compat.git",
"reference": "906e0b925895d3a5941eda25f371fbafb3cbc22f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/paragonie/sodium_compat/zipball/906e0b925895d3a5941eda25f371fbafb3cbc22f",
"reference": "906e0b925895d3a5941eda25f371fbafb3cbc22f",
"shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
},
"require": {
"paragonie/random_compat": ">=1",
"php": "^5.2.4|^5.3|^5.4|^5.5|^5.6|^7|^8"
},
"require-dev": {
"phpunit/phpunit": "^3|^4|^5|^6|^7|^8|^9"
},
"suggest": {
"ext-libsodium": "PHP < 7.0: Better performance, password hashing (Argon2i), secure memory management (memzero), and better security.",
"ext-sodium": "PHP >= 7.0: Better performance, password hashing (Argon2i), secure memory management (memzero), and better security."
},
"type": "library",
"autoload": {
"files": [
"autoload.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"ISC"
],
"authors": [
{
"name": "Paragon Initiative Enterprises",
"email": "security@paragonie.com"
},
{
"name": "Frank Denis",
"email": "jedisct1@pureftpd.org"
}
],
"description": "Pure PHP implementation of libsodium; uses the PHP extension if it exists",
"keywords": [
"Authentication",
"BLAKE2b",
"ChaCha20",
"ChaCha20-Poly1305",
"Chapoly",
"Curve25519",
"Ed25519",
"EdDSA",
"Edwards-curve Digital Signature Algorithm",
"Elliptic Curve Diffie-Hellman",
"Poly1305",
"Pure-PHP cryptography",
"RFC 7748",
"RFC 8032",
"Salpoly",
"Salsa20",
"X25519",
"XChaCha20-Poly1305",
"XSalsa20-Poly1305",
"Xchacha20",
"Xsalsa20",
"aead",
"cryptography",
"ecdh",
"elliptic curve",
"elliptic curve cryptography",
"encryption",
"libsodium",
"php",
"public-key cryptography",
"secret-key cryptography",
"side-channel resistant"
],
"support": {
"issues": "https://github.com/paragonie/sodium_compat/issues",
"source": "https://github.com/paragonie/sodium_compat/tree/v1.18.0"
},
"time": "2022-09-13T20:54:27+00:00"
},
{
"name": "phpoption/phpoption",
"version": "1.8.1",
@ -5224,6 +5372,73 @@
},
"time": "2022-07-28T14:25:11+00:00"
},
{
"name": "pusher/pusher-php-server",
"version": "7.0.2",
"source": {
"type": "git",
"url": "https://github.com/pusher/pusher-http-php.git",
"reference": "af3eeaefc0c7959f5b3852f0a4dd5547245d33df"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/pusher/pusher-http-php/zipball/af3eeaefc0c7959f5b3852f0a4dd5547245d33df",
"reference": "af3eeaefc0c7959f5b3852f0a4dd5547245d33df",
"shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
},
"require": {
"ext-curl": "*",
"ext-json": "*",
"guzzlehttp/guzzle": "^7.2",
"paragonie/sodium_compat": "^1.6",
"php": "^7.3|^8.0",
"psr/log": "^1.0|^2.0|^3.0"
},
"require-dev": {
"overtrue/phplint": "^2.3",
"phpunit/phpunit": "^8.5|^9.3"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "5.0-dev"
}
},
"autoload": {
"psr-4": {
"Pusher\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "Library for interacting with the Pusher REST API",
"keywords": [
"events",
"messaging",
"php-pusher-server",
"publish",
"push",
"pusher",
"real time",
"real-time",
"realtime",
"rest",
"trigger"
],
"support": {
"issues": "https://github.com/pusher/pusher-http-php/issues",
"source": "https://github.com/pusher/pusher-http-php/tree/7.0.2"
},
"time": "2021-12-07T13:09:00+00:00"
},
{
"name": "ralouphie/getallheaders",
"version": "3.0.3",

62
config/broadcasting.php Normal file
View File

@ -0,0 +1,62 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Broadcaster
|--------------------------------------------------------------------------
|
| This option controls the default broadcaster that will be used by the
| framework when an event needs to be broadcast. You may set this to
| any of the connections defined in the "connections" array below.
|
| Supported: "pusher", "redis", "log", "null"
|
*/
'default' => env('BROADCAST_DRIVER', 'null'),
/*
|--------------------------------------------------------------------------
| Broadcast Connections
|--------------------------------------------------------------------------
|
| Here you may define all of the broadcast connections that will be used
| to broadcast events to other systems or over websockets. Samples of
| each available type of connection are provided inside this array.
|
*/
'connections' => [
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY', 'app-key'),
'secret' => env('PUSHER_APP_SECRET', 'app-secret'),
'app_id' => env('PUSHER_APP_ID', 'app-id'),
'options' => [
'host' => env('PUSHER_HOST', '127.0.0.1'),
'port' => env('PUSHER_PORT', 6001),
'scheme' => env('PUSHER_SCHEME', 'http'),
'encrypted' => true,
'useTLS' => env('PUSHER_SCHEME') === 'https',
],
],
'redis' => [
'driver' => 'redis',
'connection' => env('BROADCAST_REDIS_CONNECTION', 'default'),
],
'log' => [
'driver' => 'log',
],
'null' => [
'driver' => 'null',
],
],
];

View File

@ -2,6 +2,8 @@
/** @var \Laravel\Lumen\Routing\Router $router */
use Illuminate\Support\Facades\Broadcast;
$router->get('/users', [
'uses' => 'UserController@index'
]);
@ -103,3 +105,7 @@
$router->patch('/{route:.*}/', $controller);
$router->delete('/{route:.*}/', $controller);
});
$router->get('broadcasting/auth', ['uses' => 'BroadcastController@authenticate']);
$router->post('broadcasting/auth', ['uses' => 'BroadcastController@authenticate']);

7
routes/channels.php Normal file
View File

@ -0,0 +1,7 @@
<?php
use Illuminate\Support\Facades\Broadcast;
Broadcast::channel('users.{userId}', function ($user, $userId) {
return (int) $user->id === (int) $userId;
});