我在我的项目中使用了 pusher。我按照 laravel 文档配置广播。当我解雇我的事件推送器时,它对我不起作用。但是当我从推送器控制台发送数据时,推送器会收到此数据。
我也尝试vinkla/pusher。它的工作正常,但 Laravel 事件广播不起作用。
请帮我。
这是我的 TestEvent.php
代码
namespace Factum\Events;
use Factum\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class TestEvent implements ShouldBroadcast
{
use SerializesModels;
public $text;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($text)
{
$this->text = $text;
}
/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return ['test-channel'];
}
}
最佳答案
我遇到了类似的问题,并逐步解决了问题。
我假设您正在运行 Laravel 5.3。
以下是可能有帮助的分步演练:
config\broadcasting.php
:'connections' => [
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_KEY'),
'secret' => env('PUSHER_SECRET'),
'app_id' => env('PUSHER_ID'),
'options' => [
'cluster' => 'eu',
'encrypted' => true,
// 'host' => 'api-eu.pusher.com'
// 'debug' => true,
],
],
],
web.php
中创建用于测试的路由文件:Route::get('/broadcast', function() {
event(new \Factum\Events\TestEvent('Sent from my Laravel application'));
return 'ok';
});
TestEvent.php
事件文件,您应该添加此方法以指定您的事件名称:/**
* The event's broadcast name.
*
* @return string
*/
public function broadcastAs()
{
return 'my_event';
}
保持页面打开,以便您可以注意到是否收到了来自应用程序的成功请求。
另一个重要的元素是 worker ——队列处理器。
如果没有事件工作人员运行来处理您的队列,作业 (TestEvent) 将“保留”在工作表中,这意味着工作处于挂起状态,并且在事件工作人员开始处理队列之前不会再发生任何事情。
您可以像这样启动工作程序:
www@yourmachine# PHP artisan queue:work --tries=3
可选步骤:
如果仍然缺少某些内容,您可以像这样调试应用程序与 Pusher 的交互:
在您的测试 route 尝试这样做:
Route::get('/broadcast', function() {
// New Pusher instance with our config data
$pusher = new \Pusher(
config('broadcasting.connections.pusher.key'),
config('broadcasting.connections.pusher.secret'),
config('broadcasting.connections.pusher.app_id'),
config('broadcasting.connections.pusher.options')
);
// Enable pusher logging - I used an anonymous class and the Monolog
$pusher->set_logger(new class {
public function log($msg)
{
\Log::info($msg);
}
});
// Your data that you would like to send to Pusher
$data = ['text' => 'hello world from Laravel 5.3'];
// Sending the data to channel: "test_channel" with "my_event" event
$pusher->trigger( 'test_channel', 'my_event', $data);
return 'ok';
});
我希望它也对你有用!祝你编码愉快! ;)
关于Laravel 事件广播不适用于推送器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37854005/