2

我是 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()。我真的被困在这里,如果有人有任何想法,我将非常感激。

编辑

这是我的广播.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,
4

2 回答 2

1

如果您可以连接到laravel-websockets仪表板但未显示事件,则您的/laravel-websockets/auth请求可能由于 csrf 令牌而失败。尝试添加laravel-websockets到中间件中的$except变量。VerifyCsrfToken

于 2020-05-12T17:05:12.533 回答
0

你应该QUEUE_CONNECTION=sync在你的.env文件中设置。

这将使广播工作并解决您的问题。

于 2020-07-17T08:48:41.567 回答