2016-11-25 188 views
2

試圖使用Laravel 5.3與推杆,但它似乎不能在我的代碼中正確工作。Pusher和Laravel 5.3事件廣播

我.ENV是正確的

PUSHER_APP_ID= myappid 
PUSHER_KEY= mykey 
PUSHER_SECRET= mysecret 

這是broadcasting.php我的 '推' 配置

'pusher' => [ 
     'driver' => 'pusher', 
     'key' => env('PUSHER_KEY'), 
     'secret' => env('PUSHER_SECRET'), 
     'app_id' => env('PUSHER_APP_ID'), 
     'options' => [ 
      'cluster' => 'eu', 
      'encrypted' => true, 
     ], 
    ], 

我創建了一個事件,這是

<?php 

namespace App\Events; 

use Illuminate\Broadcasting\Channel; 
use Illuminate\Queue\SerializesModels; 
use Illuminate\Broadcasting\PrivateChannel; 
use Illuminate\Broadcasting\PresenceChannel; 
use Illuminate\Broadcasting\InteractsWithSockets; 
use Illuminate\Contracts\Broadcasting\ShouldBroadcast; 

class ProposalEvent implements ShouldBroadcast 
{ 
    use InteractsWithSockets, SerializesModels; 

    public $data; 

    /** 
* Create a new event instance. 
* 
* @return void 
*/ 
public function __construct($data) 
{ 
    $this->data = $data; 
} 

/** 
* Get the channels the event should broadcast on. 
* 
* @return Channel|array 
*/ 
public function broadcastOn() 
{ 
    return ['test-channel']; 
    // return new PrivateChannel('test-channel'); 
    // return new PresenceChannel('test-channel'); 
} 
} 

我javascript

Pusher.logToConsole = true; 

var pusher = new Pusher("mykey", { 
    cluster: 'eu', 
    encrypted: true 
}); 
var channel = pusher.subscribe('test-channel'); 
channel.bind('App\\Events\\ProposalEvent', function(data) { 
    alert(data); 
}); 

終於在我看來

event(new App\Events\ProposalEvent('some data')); 

不幸的是,這是不是爲我工作,但是當我使用pusher->觸發這個樣子,沒有事件,它做工精細,和我看到在推進調試控制檯

消息
$options = array(
    'cluster' => 'eu', 
    'encrypted' => true 
); 
$pusher = new Pusher(
    'mykey', 
    'mysecret', 
    'myid', 
    $options 
); 

$data['message'] = 'some data'; 
$pusher->trigger('test-channel', 'my-event', $data); 

我在Laravel文檔和其他資源中搜索解決方案。在stackoverflow有問題,但沒有response.I將不勝感激,如果有人可以幫助我,因爲我找不到解決方案几天

回答

0

嘗試直接通過配置/廣播傳遞推送憑據。 php

它爲我工作。

'default' => 'pusher', 
'connections' => [ 

    'pusher' => [ 
     'driver' => 'pusher', 
     'key' => '***', 
     'secret' => '**', 
     'app_id' => '**', 
     'options' => [ 
     ], 
    ], 
], 
enter code here 
1

我被困在相同的情況下,發現我沒有使用隊列!

在本文檔中,它說

之前廣播的事件,您還需要配置和運行隊列監聽器。所有事件廣播都是通過排隊作業完成的,因此應用程序的響應時間不會受到嚴重影響。

我之前刪除了config/queue.php文件,因爲我以爲我沒有使用它。也許你和我一樣,或者在排隊時遇到問題。