【发布时间】:2016-11-25 18:54:01
【问题描述】:
尝试将 Laravel 5.3 与 pusher 一起使用,但在我的代码中似乎无法正常工作。
我的 .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->trigger 时,没有事件,它工作正常,并且我在 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中有同样问题的问题,但没有回复。如果有人能帮助我,我将不胜感激,因为我几天都找不到解决方案
【问题讨论】:
标签: laravel events websocket pusher broadcasting