首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Laravel 广播功能 前端 Vue 接受不到后端私有频道的推送?

Laravel 广播功能 前端 Vue 接受不到后端私有频道的推送?

提问于 2023-01-30 14:25:09
回答 0关注 0查看 86

路由

代码语言:js
复制
//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);
});

事件

代码语言:js
复制
<?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');
    }
}

前端

代码语言:javascript
复制
<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>

有没有大佬帮忙看看,卡这两天了

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档