我構建了一個 Laravel 應用程序,嘗試通過 Pusher.com 實現 Web Sockets(第一次)。雖然我的公共頻道訂閱工作正常,但我很難讓私人頻道正常工作。根據 laravel 文檔,您需要在我擁有的 配置文件中取消注釋 App\Providers\BroadcastServiceProvider::class 。app.php我的channels.php 有以下規則Broadcast::channel('App.User.{id}', function ($user, $id) { return (int) $user->id === (int) $id;});Broadcast::channel('private-queue.business.{business}', function ($user, Business $business) { // @todo: add real authentication return true;});我還需要添加其他內容才能使/pusher/auth端點正常工作嗎?
2 回答

守著一只汪
TA貢獻1872條經驗 獲得超4個贊
從 Laravel 7.x 開始,廣播端點是 broadcasting/auth 而不是 pusher/auth。
我需要像這樣更新我的 JS 才能定義自定義身份驗證端點:
const pusher = new Pusher('{{ env('PUSHER_APP_KEY') }}', {
cluster: '{{ env('PUSHER_APP_CLUSTER') }}',
authEndpoint: '/broadcasting/auth',
auth: {
headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}',
}
}
});
您需要添加 CSRF-TOKEN,否則您將收到頁面過期錯誤。

慕桂英3389331
TA貢獻2036條經驗 獲得超8個贊
你能嘗試一下嗎: 嘗試這個:
取消注釋 config/app.php 中的 App\Providers\BroadcastServiceProvider::class 使用 php artisan config:cache 使用 php artisan 路由:緩存 使用 php artisan route:list 檢查新路由廣播/身份驗證
- 2 回答
- 0 關注
- 204 瀏覽
添加回答
舉報
0/150
提交
取消