准备工作
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
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.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;
public function __construct(User $user)
{
$this->arr = [
'id'=>1,
];
$this->user = $user;
}
public function broadcastOn()
{
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查看打印情况
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人