Laravel 事件中的 broadcastOn() 方法从不触发
Laravel broadcastOn() method in event never fires
我是 laravel 和 websockets 的新手。我已经让我的 websockets 在 laravel-websockets 仪表板上工作,现在我试图用这个 javascript 命令触发一个 websocket 事件:
axios.post('updatequeue', {queue_position: newPos});
newPos 是一个数字。
这是我的控制器方法:
public function updateQueue(Request $request){
$queueposition = $request->input('queue_position');
event(new QueueUpdate($queueposition));
}
这是我的活动:
class QueueUpdate implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $queue_position;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($queue_position)
{
$this->queue_position = $queue_position;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new Channel('sessionid');
}
}
当我在仪表板中查看事件时,没有任何显示。我从 axios 请求中得到 200 响应。我在整个过程中都放置了日志,并且调用了我的事件 __construct 方法,但没有调用 broadcastOn()。
我真的被困在这里,如果有人有任何想法,我将不胜感激。
编辑
这是我的 broadcasting.php:
<?php
return [
'default' => env('BROADCAST_DRIVER', 'null'),
'connections' => [
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => env('PUSHER_APP_CLUSTER'),
'host' => env('WEBSOCKET_BROADCAST_HOST'),
'port' => env('WEBSOCKET_BROADCAST_PORT'),
'scheme' => env('WEBSOCKET_SCHEME'),
'encrypted' => env('WEBSOCKET_ENCRYPTED'),
],
],
'redis' => [
'driver' => 'redis',
'connection' => 'default',
],
'log' => [
'driver' => 'log',
],
'null' => [
'driver' => 'null',
],
],
];
和websockets.php:
<?php
use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize;
return [
'dashboard' => [
'port' => env('LARAVEL_WEBSOCKETS_PORT', 6001),
],
'apps' => [
[
'id' => env('PUSHER_APP_ID'),
'name' => env('APP_NAME'),
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'enable_client_messages' => false,
'enable_statistics' => true,
],
],
'app_provider' => BeyondCode\LaravelWebSockets\Apps\ConfigAppProvider::class,
'allowed_origins' => [
//
],
'max_request_size_in_kb' => 250,
'path' => 'laravel-websockets',
'middleware' => [
'web',
Authorize::class,
],
'statistics' => [
'model' => \BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry::class,
'interval_in_seconds' => 60,
'delete_statistics_older_than_days' => 60,
'perform_dns_lookup' => false,
],
'ssl' => [
'local_cert' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT', null),
'local_pk' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_PK', null),
'passphrase' => env('LARAVEL_WEBSOCKETS_SSL_PASSPHRASE', null),
'verify_peer' => env('LARAVEL_WEBSOCKETS_SSL_VERIFY_PEER', true),
],
'channel_manager' => \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager::class,
];
编辑
我是本地人。
以下是 broadcasting.php 的值:
'driver' => 'pusher',
'key' => portalkey,
'secret' => secret,
'app_id' => portalID,
'cluster' => portalcluster,
'host' => 127.0.0.1,
'port' => 6001,
'scheme' => http,
'encrypted' => false,
如果您可以连接到 laravel-websockets
仪表板但事件不会显示,可能是您的 /laravel-websockets/auth
请求因 csrf 令牌而失败。尝试将 laravel-websockets
添加到 VerifyCsrfToken
中间件中的 $except
变量。
您应该在 .env
文件中设置 QUEUE_CONNECTION=sync
。
这将使广播正常工作并解决您的问题。
我是 laravel 和 websockets 的新手。我已经让我的 websockets 在 laravel-websockets 仪表板上工作,现在我试图用这个 javascript 命令触发一个 websocket 事件:
axios.post('updatequeue', {queue_position: newPos});
newPos 是一个数字。
这是我的控制器方法:
public function updateQueue(Request $request){
$queueposition = $request->input('queue_position');
event(new QueueUpdate($queueposition));
}
这是我的活动:
class QueueUpdate implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $queue_position;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($queue_position)
{
$this->queue_position = $queue_position;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new Channel('sessionid');
}
}
当我在仪表板中查看事件时,没有任何显示。我从 axios 请求中得到 200 响应。我在整个过程中都放置了日志,并且调用了我的事件 __construct 方法,但没有调用 broadcastOn()。 我真的被困在这里,如果有人有任何想法,我将不胜感激。
编辑
这是我的 broadcasting.php:
<?php
return [
'default' => env('BROADCAST_DRIVER', 'null'),
'connections' => [
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => env('PUSHER_APP_CLUSTER'),
'host' => env('WEBSOCKET_BROADCAST_HOST'),
'port' => env('WEBSOCKET_BROADCAST_PORT'),
'scheme' => env('WEBSOCKET_SCHEME'),
'encrypted' => env('WEBSOCKET_ENCRYPTED'),
],
],
'redis' => [
'driver' => 'redis',
'connection' => 'default',
],
'log' => [
'driver' => 'log',
],
'null' => [
'driver' => 'null',
],
],
];
和websockets.php:
<?php
use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize;
return [
'dashboard' => [
'port' => env('LARAVEL_WEBSOCKETS_PORT', 6001),
],
'apps' => [
[
'id' => env('PUSHER_APP_ID'),
'name' => env('APP_NAME'),
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'enable_client_messages' => false,
'enable_statistics' => true,
],
],
'app_provider' => BeyondCode\LaravelWebSockets\Apps\ConfigAppProvider::class,
'allowed_origins' => [
//
],
'max_request_size_in_kb' => 250,
'path' => 'laravel-websockets',
'middleware' => [
'web',
Authorize::class,
],
'statistics' => [
'model' => \BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry::class,
'interval_in_seconds' => 60,
'delete_statistics_older_than_days' => 60,
'perform_dns_lookup' => false,
],
'ssl' => [
'local_cert' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT', null),
'local_pk' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_PK', null),
'passphrase' => env('LARAVEL_WEBSOCKETS_SSL_PASSPHRASE', null),
'verify_peer' => env('LARAVEL_WEBSOCKETS_SSL_VERIFY_PEER', true),
],
'channel_manager' => \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager::class,
];
编辑
我是本地人。 以下是 broadcasting.php 的值:
'driver' => 'pusher',
'key' => portalkey,
'secret' => secret,
'app_id' => portalID,
'cluster' => portalcluster,
'host' => 127.0.0.1,
'port' => 6001,
'scheme' => http,
'encrypted' => false,
如果您可以连接到 laravel-websockets
仪表板但事件不会显示,可能是您的 /laravel-websockets/auth
请求因 csrf 令牌而失败。尝试将 laravel-websockets
添加到 VerifyCsrfToken
中间件中的 $except
变量。
您应该在 .env
文件中设置 QUEUE_CONNECTION=sync
。
这将使广播正常工作并解决您的问题。