2017-08-13 114 views
1

學習Laravel賽事轉播/ Echo/Vuethis tutorial玩耍。Laravel回聲/推杆驗證失敗(403)

我不斷收到403對身份驗證的響應,我懷疑我對channels.php路由不瞭解是問題所在。我正在使用Player模型,而不是User,對於Auth,它工作正常。

ChatMessageSend事件

class ChatMessageSent implements ShouldBroadcast 
{ 
    use Dispatchable, InteractsWithSockets, SerializesModels; 

    public $channel; 
    public $player; 
    public $chatMessage; 

     /** 
     * Create a new event instance. 
     * GameChat constructor. 
     * @param $chatMessage 
     * @param $player 
     */ 
     public function __construct(ChatMessage $chatMessage, Player $player) 
     { 
      $this->channel = session()->get('chat.channel'); 

      $this->chatMessage = $chatMessage; 

      $this->player = $player; 
     } 

     /** 
     * Get the channels the event should broadcast on. 
     * 
     * @return Channel|array 
     */ 
     public function broadcastOn() 
     { 
      return new PrivateChannel($this->channel); 
     } 
    } 

監聽ChatMessageNotification(默認/空)

class ChatMessageNotification 
{ 
    /** 
    * ChatMessageNotification constructor. 
    */ 
    public function __construct() 
    { 
     // 
    } 

    /** 
    * Handle the event. 
    * 
    * @param ChatMessageSent $event 
    * @return void 
    */ 
    public function handle(ChatMessageSent $event) 
    { 
     // 
    } 
} 

控制器ChatController

class ChatController extends Controller 
{ 
    /** 
    * Send chat message 
    * 
    * @param Request $request 
    * @return \Illuminate\Database\Eloquent\Collection|static[] 
    */ 
    public function getMessages(Request $request) 
    { 
     return ChatMessage::with('player') 
      ->where('progress_id', '=', session('game.progress.id')) 
      ->orderBy('created_at', 'DESC') 
      ->get(); 
    } 

    /** 
    * Send chat message 
    * 
    * @param Request $request 
    * @return array|string 
    */ 
    public function sendMessage(Request $request) 
    { 
     $player = Auth::user(); 

     $message = $request->input('message'); 

     if ($message) { 
      $message = ChatMessage::create([ 
       'player_id' => $player->id, 
       'progress_id' => session()->get('game.progress.id'), 
       'message'  => $request->input('message') 
      ]); 
     } 

     broadcast(new ChatMessageSent($player, $message))->toOthers(); 

     return ['type' => 'success']; 
    } 
} 

路線channels.php

Broadcast::channel(session()->get('chat.channel'), function ($player, $message) { 
    return $player->inRoom(); 
}); 

在我Player

/** 
* A user can be in one chat channel 
*/ 
public function inRoom() 
{ 
    if ((Auth::check()) and ($this->games()->where('progress_id', '=', session('game.progress.id'))->get())) { 
     return true; 
    } 

    return false; 
} 

當玩家登錄時,我在會話存儲聊天室ID,我想作爲通道來使用。

vue聊天實例

Vue.component('chat-messages', require('./../generic/chat-messages.vue')); 
Vue.component('chat-form', require('./../generic/chat-form.vue')); 

const app = new Vue({ 
    el: '#toolbar-chat', 

    data: { 
     messages: [] 
    }, 

    created() { 
     this.fetchMessages(); 

     Echo.private(chat_channel) 
     .listen('chatmessagesent', (e) => { 
      this.messages.unshift({ 
       message: e.data.message, 
       player: e.data.player.nickname 
      }); 
     }); 
    }, 

    methods: { 
     fetchMessages() { 
      axios.get(chat_get_route) 
      .then(response => { 
       this.messages = response.data; 
      }); 
     }, 

     addMessage(message) { 
      this.messages.unshift(message); 

      this.$nextTick(() => { 
       this.$refs.toolbarChat.scrollTop = 0; 
      }); 

      axios.post(chat_send_route, message) 
      .then(response => { 
       console.log(response.data); 
      }); 

     } 
    } 
}); 

但我不斷收到

POST http://my-games.app/broadcasting/auth 403 (Forbidden) 
Pusher : Couldn't get auth info from your webapp : 403 

回答

0

這可能是一個想法,找出是否你的路由/ channels.php文件像您期望做的事情。也許可以添加一些日誌記錄來查看該路由是否被調用,並且您的inRoom函數正在返回您期望的內容。