路由
//web.php
// 用户登录
Route::get('/3',function (){
auth()->loginUsingId(3); // 能获取到数据库里面id 为3 的信息
});
// 私有广播的使用
Route::get('/private',function (){
broadcast(new Prize('私有广播'));
});
// channels.php
// 设置频道权限
Broadcast::channel('three_good', function ($user, $id) {
$users = [1, 2, 3, 4, 5];
return in_array($user->id,$users);
});
事件
<?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\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Auth;
class Prize implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public $msg;
public function __construct($msg)
{
//
$this->msg = $msg;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('three_good');
}
}
前端
<template>
<button @click="submit">123</button>
<button @click="login">login</button>
</template>
<script>
import {useTokenStore} from "../stores/store";
const {proxy} = useCurrentInstance();
// 此时共有能接受推送
proxy.$Echo.channel('Countryside')
.listen('Free',(e:any)=>{
console.log(e,123)
})
// 私有的不能接收到推送
proxy.$Echo.private('three_good')
.listen('Prize',(e)=>{
console.log(e,456)
})
let login = async ()=>{
await api.login();
}
let public= async () =>{
await api.public();
}
</script>
有没有大佬帮忙看看,卡这两天了
相似问题