准备工作
npm install -g laravel-echo-server
laravel-echo-server init
BROADCAST_DRIVER=redis
CACHE_DRIVER=redis
QUEUE_CONNECTION=redis
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=xxxxx
DB_USERNAME=xxxxx
DB_PASSWORD=xxxx
DB_PREFIX=
composer require predis/predis
//根据情况执行(依赖lv自带的auth) php artisan migrate
composer require laravel/ui
php artisan ui vue --auth
npm install
npm install --save socket.io-client
npm run dev
php artisan make:controller Home/IndexController
<?php
namespace App\Http\Controllers\Home;
use App\Events\TestEvent;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;
class IndexController extends Controller {
public function testBroadcast()
{
$user = Auth::user();
broadcast(new TestEvent($user));
}
public function showBroadcast()
{
return view('index/testBroadcast');
}
}
import Echo from 'laravel-echo';
window.Echo = new Echo({
broadcaster: 'socket.io',
host:window.location.hostname + ':6001'
});
npm run dev
- views/index/testBroadcast.blade.php
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>Laravel</title>
</head>
<body>
<div class="flex-center position-ref full-height">
<div class="content">
<div class="title m-b-md">
Laravel
</div>
<div id="app">
</div>
<script src="//{{ Request::getHost() }}:6001/socket.io/socket.io.js"></script>
<script src="{{ asset('js/app.js') }}"></script>
<script>
console.log(Echo);
/* 公共频道
Echo.channel('test')
.listen('TestEvent', (e) => {
console.log(e);
console.log(e.arr);
});*/
/**
* 授权频道
*/
Echo.private('test.222').listen('TestEvent', (e) => {
console.log(e);
console.log(e.arr);
});
</script>
</div>
</div>
</body>
</html>
php artisan make:event TestEvent
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Auth\User;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class TestEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $arr;
public $user;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(User $user)
{
$this->arr = [
'id'=>1,
];
$this->user = $user;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
// return new Channel('test'); //公共频道
return new PrivateChannel('test.'.$this->user->id); //授权频道
}
}
Route::get('/testBroadcast', 'Home\IndexController@testBroadcast');
Route::get('/showBroadcast', 'Home\IndexController@showBroadcast');
//下面私有授权频道必须要,不然监听启动报错
Route::post('/broadcasting/auth', function (\Illuminate\Http\Request $request){});
- 使用
- 启动 laravel-echo-server start
- 启动queue php artisan queue:listen
- host/testBroadcast触发通知
- 观察host/showBroadcast的console查看打印情况