首页 > 解决方案 > 无法使用 laravel-websockets 通过 Echo 接收通知

问题描述

当我通过 Channel 而不是 PrivateChannel 连接时,一切都按预期工作。但是当我通过 PrivateChannel 连接时,我突然停止在我的 javascript 中接收事件我的事件出现在 WebSocket 仪表板中。我像这样发送我的事件:

            FullRecipeSaved::dispatch($recipe, $rld);

FullRecipeSaved.php


class FullRecipeSaved implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $recipe;

    public $recipeLiftingDate;

    public function __construct(Recipe $recipe, RecipeLiftingDate $recipeLiftingDate)
    {
        $this->recipe = $recipe;
        $this->recipe = $recipeLiftingDate;
    }

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

    /**
     * The event's broadcast name.
     *
     * @return string
     */
    public function broadcastAs()
    {
        return 'recipe.update';
    }
}

频道.php

<?php

Broadcast::channel('recipe', function($recipe, $recipeLiftingDate) {
    return true;
});

引导程序.js

window.Echo = new Echo({
    broadcaster: "pusher",
    key: process.env.MIX_PUSHER_APP_KEY,
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    wsHost: window.location.hostname,
    wsPort: 6001,
    forceTLS: false,
    disableStats: false,
    authEndpoint: GLOBAL.appUrl,
});

GLOBAL.appUrl 是来自 .env 文件的 APP_URL(http://localhost/****/public)。这是我用来连接频道的代码

window.Echo.private("recipe").listen(
        ".recipe.update",
        (event) => {
            console.log("ok");
        }
    );

标签: phplaravelwebsocket

解决方案


推荐阅读