Laravel 8 授權(quán)私有廣播頻道

2021-07-26 09:56 更新

如果你的單頁面應(yīng)用需要通過 private / presence broadcast channels 進(jìn)行身份認(rèn)證,你需要在你的 routes/api.php 文件中調(diào)用 Broadcast::routes 方法:

Broadcast::routes(['middleware' => ['auth:sanctum']]); 

接下來,為了使 Pusher 的授權(quán)請求成功,你需要在初始化 Laravel Echo 時提供自定義的 Pusher authorizer。這可以讓你的應(yīng)用程序?qū)?Pusher 配置為 為了跨域請求正確配置的 axios 實(shí)例 properly configured for cross-domain requests

window.Echo = new Echo({
    broadcaster: "pusher",
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    encrypted: true,
    key: process.env.MIX_PUSHER_APP_KEY,
    authorizer: (channel, options) => {
        return {
            authorize: (socketId, callback) => {
                axios.post('/api/broadcasting/auth', {
                    socket_id: socketId,
                    channel_name: channel.name
                })
                .then(response => {
                    callback(false, response.data);
                })
                .catch(error => {
                    callback(true, error);
                });
            }
        };
    },
}) 


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號