2017-09-09 107 views
0

我最近設置了我的Laravel應用程序並配置了Echo以接收推送通知。不幸的是,回調函數並沒有被觸發,即使事件被接收並且數據也是如此。你可以找到我的代碼如下:Laravel Echo不觸發回調

web.php

Route::get('/pusher', function() { 
    $location = ['lang' => 'langTets', 'lat' => 'latTest']; 
    event(new Freight\Events\LocationUpdated($location)); 
    return "Event has been sent!"; 
}); 

bootstrap.js

import Echo from 'laravel-echo' 

window.Pusher = require('pusher-js'); 
Pusher.logToConsole = true; 

window.Echo = new Echo({ 
    broadcaster: 'pusher', 
    key: 'iscorrect', 
    cluster: 'eu', 
    encrypted: true 
}); 

window.Echo.channel('location-updates') 
    .listen('.LocationUpdated', function(location) { 
     console.log("Test"); 
    }); 

我app.blade

<!DOCTYPE html> 
<html lang="{{ app()->getLocale() }}"> 
<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 

    <!-- CSRF Token --> 
    <meta name="csrf-token" content="{{ csrf_token() }}"> 

    <title>Freight</title> 

    <!-- Styles --> 
    <link href="{{ asset('css/app.css') }}" rel="stylesheet"> 
    <!-- JS --> 
    <script src="{{ asset('js/app.js') }}"></script> 
</head> 
<body> 
... 

這會出現在我的控制檯,當我打開頁面:

app.js:32652 Pusher : State changed : initialized -> connecting 
app.js:32652 Pusher : Connecting : {"transport":"ws","url":"censoredstuff"} 
app.js:32652 Pusher : State changed : connecting -> connected with new socket ID morecensoredstuff 
app.js:32652 Pusher : Event sent : {"event":"pusher:subscribe","data":{"channel":"location-updates"}} 
app.js:32652 Pusher : Event recd : {"event":"pusher_internal:subscription_succeeded","data":{},"channel":"location-updates"} 
app.js:32652 Pusher : No callbacks on location-updates for pusher:subscription_succeeded 

現在在一個觸發事件,這是出現在控制檯:

Pusher : Event recd : {"event":"Freight\\Events\\LocationUpdated","data":{"location":{"lang":"langTets","lat":"latTest"}},"channel":"location-updates"} 
app.js:32652 Pusher : No callbacks on location-updates for Freight\Events\LocationUpdated 

你有任何線索,爲什麼的console.log工作不正常?如果需要更多信息,請隨時詢問。

回答

0

我仍然沒有找出問題的根源。然而,這完美地解決了它:

  • 實現該方法broadcastAs()在你的事件是這樣的:

broadcastAs(){ return 'myneweventname'; }

  • 變化的.js文件,以便您正在收聽的新創建的事件別名(沒有前導期)。