我的活动未发送至 Pusher。调用了broadcastOn方法,但我在pusher调试控制台中没有看到该事件。
当我使用 https://github.com/pusher/pusher-http-php?tab=readme-ov-file 和我的凭据时,这不是凭据问题。它有效。
Laravel 版本:11.25.0
这是我的活动:
class PlaceAddedEvent implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $idSearch;
/**
* Create a new event instance.
*/
public function __construct($idSearch)
{
$this->idSearch = $idSearch;
Log::info(__CLASS__ . ":" . __FUNCTION__ . " id = " . $idSearch);
}
/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
Log::info('PlaceAddedEvent broadcasted on channel: channel-place');
return [
new Channel('channel-place'),
];
}
}
(顺便说一句,当我使用 ShouldBroadcast 而不是 ShouldBroadcastNow 时,不会调用 BroadcastOn 方法)
该事件在 artisan 命令中触发:
event(new PlaceAddedEvent($idSearch));
请注意,broadcastOn() 方法被触发,我在日志中看到它:
[2024-09-27 05:40:37] local.INFO: Broadcasting [App\Events\PlaceAddedEvent] on channels [channel-place] with payload:
{
"idSearch": "1",
"socket": null
}
在广播文件中,我有:(当然,环境变量在 .env 文件中正确定义)
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => 'ap3',
'host' => 'api-ap3.pusher.com',
'port' => 80,
'scheme' => 'http',
'encrypted' => false,
'useTLS' => false,
],
'client_options' => [
// Guzzle client options: https://docs.guzzlephp.org/en/stable/request-options.html
],
],
如果我使用https,也是一样的。
就在我发出事件之前,我这样做:
$pusherConfig = config('broadcasting.connections.pusher');
$broadcastDriver = config('broadcasting.default');
$this->info('Broadcast Driver: ' . $broadcastDriver);
$this->info('Pusher Config:');
$this->info(print_r($pusherConfig, true));
确保所有变量都设置正确。
有什么想法吗?
谢谢
确保您的 .env 文件具有正确的广播驱动程序:
BROADCAST_DRIVER=pusher