2016-11-25 187 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文件,因为我以为我没有使用它。也许你和我一样,或者在排队时遇到问题。