php - 为什么Laravel广播事件始终使用 “laravel_database_channel” channel 以及如何更改它?

我尝试在项目中加入Laravel-echo。
我正在使用docker-compose容器,并为redis和Laravel-echo-server添加了2个容器。所有容器均正常工作。 (php,nginx,postgres,redis,laravel-echo)

设置.env以进行广播和Redis。
Made事件进行广播,并尝试使用以下简单执行脚本

事件(新的LegalizationImporterMessage(“test”)));

在客户端js(Vue)上,我可以连接到Laravel echo。

事件始终使用“laravel_database_channel”

问题是我不知道必须在哪里更改事件使用我的 channel 而不是“laravel_database_channel”

我试过更改“broadcastOn”函数,但是它不起作用。

.env文件

BROADCAST_DRIVER=redis
CACHE_DRIVER=file
QUEUE_CONNECTION=redis
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=redis
REDIS_PASSWORD=null
REDIS_PORT=6379

Laravel-echo-server.json
{
  "authHost": "nginx",
  "authEndpoint": "/broadcasting/auth",
  "clients": [],
  "database": "redis",
  "databaseConfig": {
    "redis": {
      "port": "6379",
      "host": "redis"
    }
  },
  "devMode": true,
  "host": "",
  "port": "6001",
  "protocol": "http",
  "socketio": {},
  "secureOptions": 67108864,
  "sslCertPath": "",
  "sslKeyPath": "",
  "sslCertChainPath": "",
  "sslPassphrase": "",
  "subscribers": {
    "http": true,
    "redis": true
  },
  "apiOriginAllow": {}
}


事件文件
class LegalizationImporterMessage implements ShouldBroadcast
{
    use  SerializesModels;

    public $message;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($message)
    {
        $this->message = $message;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('channel');
    }
}

laravel-echo_1  | Channel: laravel_database_channel
laravel-echo_1  | Event: App\Events\LegalizationImporterMessage
laravel-echo_1  | Channel: laravel_database_channel
laravel-echo_1  | Event: App\Events\LegalizationImporterMessage
laravel-echo_1  | Channel: laravel_database_channel
laravel-echo_1  | Event: App\Events\LegalizationImporterMessage
laravel-echo_1  | Channel: laravel_database_channel
laravel-echo_1  | Event: App\Events\LegalizationImporterMessage
laravel-echo_1  | Channel: laravel_database_channel
laravel-echo_1  | Event: App\Events\LegalizationImporterMessage
laravel-echo_1  | Channel: laravel_database_channel
laravel-echo_1  | Event: App\Events\LegalizationImporterMessage
laravel-echo_1  | Channel: laravel_database_channel
laravel-echo_1  | Event: App\Events\LegalizationImporterMessage
laravel-echo_1  | Channel: laravel_database_channel
laravel-echo_1  | Event: App\Events\LegalizationImporterMessage

最佳答案

这是由config / database.php中的config('database.redis.options.prefix')设置的。要设置空白前缀,请将其添加到您的.env中:

REDIS_PREFIX=

这样可以确保您没有所需的前缀,并且广播现在应该可以正常工作。