1

为了实现聊天在 Laravel 8/vue 2.6 我添加了 Beyondcode/laravel-websockets 并阅读了一些手册我发现我需要使用推送器包,比如 laravel-echo 和 pusher-js 而不是推送器应用程序 API。所以我试着在 .env 中制作:

BROADCAST_DRIVER=pusher

PUSHER_APP_ID=myId
PUSHER_APP_KEY=myKey
PUSHER_APP_SECRET=mySecret
PUSHER_APP_CLUSTER=eu

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

在 resources/js/bootstrap.js 中:

import Echo from 'laravel-echo';
window.Pusher = require('pusher-js');
window.Echo = new Echo({
    broadcaster: 'pusher',
    key: 'myKey',
    wsHost: 601,
    disableStats: true,
    forceTLS: false
});

在 config/app.php 我取消注释行:

 App\Providers\BroadcastServiceProvider::class,

在 config/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'),
                'encrypted' => true,
                'host'      => '127.0.0.1',
                'port'      => 6001,
                'scheme'    => 'http',

                'useTLS' => false,
            ],
        ],
        'ably'   => [
            'driver' => 'ably',
            'key'    => env('ABLY_KEY'),
        ],

        'redis' => [
            'driver'     => 'redis',
            'connection' => 'default',
        ],

        'log' => [
            'driver' => 'log',
        ],

        'null' => [
            'driver' => 'null',
        ],

    ],

];

在 routes/channels.php 中:

Broadcast::channel('chat', function ($user, $id) {
    \Log::info(  varDump($user, ' routes/channels.php -1 $user::') ); // I DO NOT SEE THESE log lines
//    return (int) $user->id === (int) $id;
    return $user;
});

在 config/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'),
            'path' => env('PUSHER_APP_PATH'),
            'capacity' => null,
            '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,

        'logger' => BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger::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),
    ],

    'channel_manager' => \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager::class,
];

我看到正在运行的服务器:

user@os:app_path$ php artisan websockets:serve
Starting the WebSocket server on port 6001...
New connection opened for app key myKey.
Connection id 447562709.864005085 sending message {"event":"pusher:connection_established","data":"{\"socket_id\":\"447562709.864005085\",\"activity_timeout\":30}"}
...

RunningWebSockets 仪表板位于

http://127.0.0.1:8000/laravel-websockets

与许多信息的成功有关,

但试图发布新事件我看到错误:https ://imgur.com/a/7zJZPyu

{"message":"The given data was invalid.","errors":{"data":["The data must be a valid JSON string."]}}

但在 brwoser 控制台中,我看到错误:

app.js:119179 WebSocket connection to 'ws://0.0.2.89/app/myKey?protocol=7&client=js&version=7.0.3&flash=false' failed:

它以“myKey”失败。也许有一些选项不使用推送服务器密钥?

谢谢!

4

1 回答 1

1

您需要确保服务器和客户端上的凭据相同。您正在设置客户端中的键值PUSHER_APP_KEY=myKey。文件中使用的变量的值是多少config/broadcasting.phpconfig/websockets.php: key' => env('PUSHER_APP_KEY'),

来源确保使用与广播配置部分相同的应用程序 ID、密钥和密码。否则从 Laravel 广播事件将不起作用。

您还需要确保使用来自调试控制台的有效 JSON 负载。

来源只需输入通道、事件名称并提供有效的 JSON 有效负载,即可将其发送到给定通道中所有连接的客户端。

于 2021-08-04T08:23:51.803 回答