2016-11-08 39 views
2

我想測試一個基本的推者觸發事件,它給了我一個簡單的警報和一個控制檯日誌,但它不會工作。響應是從推進器控制檯調試收到的,但沒有警報。我正在使用laravel 5.3及其路線和對此的看法。以下是我的代碼。我審查了敏感信息。推者不回電

路由文件web.php

Route::get('/bridge', function() { 

    error_reporting(E_ALL); 

    $options = array(
     'cluster' => 'ap1', 
     'encrypted' => true 
    ); 
    $pusher = new Pusher(
     'key censored', 
     'secret censored', 
     'app id censore', 
     $options 
    ); 

    $data['message'] = 'hello world'; 
    $pusher->trigger('test_channel', 'my_event', $data); 

    return view('pusher'); 
}); 

,並查看pusher.blade.php

<!DOCTYPE html> 
<head> 
    <title>Pusher Test</title> 
    <script src="https://js.pusher.com/3.2/pusher.min.js"></script> 
    <script> 

     // Enable pusher logging - don't include this in production 
     Pusher.logToConsole = true; 

     var pusher = new Pusher('e5bbf707214a6223d044', { 
      cluster: 'ap1', 
      encrypted: true 
     }); 

     var channel = pusher.subscribe('test_channel'); 
     channel.bind('my_event', function(data) { 
      alert(data); 
      console.log(data); 
     }); 
    </script> 
</head> 

鉻控制檯給了我下面的日誌。

Pusher : State changed : initialized -> connecting 
Pusher : Connecting : {"transport":"ws","url":"wss://ws-ap1.pusher.com:443/app/censored key?protocol=7&client=js&version=3.2.2&flash=false"} 
Pusher : State changed : connecting -> connected with new socket ID 5034.8700909 
Pusher : Event sent : {"event":"pusher:subscribe","data":{"channel":"test_channel"}} 
Pusher : Event recd : {"event":"pusher_internal:subscription_succeeded","data":{},"channel":"test_channel"} 
Pusher : No callbacks on test_channel for pusher:subscription_succeeded 
+0

但是當你有這樣的應用程序與控制檯打開運行....在另一個選項卡登錄到推杆網站的創建一個新的通知,那麼其他選項卡上,應該體現在console.log上...你做了那個測試嗎? – Hackerman

+0

我做到了。兩個標籤,並重新加載一個 –

回答

4

您爲my_event事件創建了一個綁定。此錯誤是抱怨您沒有pusher:subscription_succeeded事件的回調。如果你想抓住並處理它,你需要創建一個綁定。

https://pusher.com/docs/client_api_guide/client_presence_channels#pusher-subscription-succeeded

channel.bind('pusher:subscription_succeeded', function(members) { 
    alert('successfully subscribed!'); 
}); 
+0

我是否用my_event替換這個channel.bind? –

+0

您可以綁定到多個事件。該錯誤表示您沒有綁定到'pusher:subscription_succeeded'事件的處理程序。 –

+1

謝謝:)這個工程。我以前不知道。 –