redis laravel Echo不侦听通道和事件

epfja78i  于 2023-02-03  发布在  Redis
关注(0)|答案(2)|浏览(186)

为了在频道上广播事件,我使用了laravelEchoredissocket-io
这是我的活动

class ChatNewMessage implements ShouldBroadcast
    {
        use InteractsWithSockets, SerializesModels;

        public $targetUsers;
        public $message;

        public function __construct ($message)
        {
            $this->targetUsers = $message->chat->users->pluck('user_id');
            $this->message     = $message;
        }

        public function broadcastOn ()
        {
            $userChannels = [];
            foreach ($this->targetUsers as $id) {
                $userChannels[] = 'user-channel.' . $id;
            }
            return $userChannels;

        }

        public function broadcastAs()
        {
            return 'NewMessage';
        }

    }

对于Socket.io服务器,我使用laravel-echo-server,配置如下:

{
    "authHost": "http://api.pars-app.dev",
    "authEndpoint": "/broadcasting/auth",
    "clients": [
        {
            "appId": "1924bf1d59b3759d",
            "key": "eaf9e3c843493421f4be488fba11f49d"
        }
    ],
    "database": "redis",
    "databaseConfig": {
        "redis": {},
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": true,
    "host": "127.0.0.1",
    "port": "3000",
    "protocol": "http",
    "socketio": {},
    "sslCertPath": "",
    "sslKeyPath": ""
}

这是bootstrap.js文件中的回波结构:

import Echo from "laravel-echo";
window.echo = new Echo({
    broadcaster: 'socket.io',
    host: 'http://127.0.0.1:3000',
    auth: {
        headers: {
            Authorization: 'bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjUsImlzcyI6Imh0dHA6XC9cL2FwaS5wYXJzLWFwcC5kZXZcL3YxXC9hdXRoXC9zaWduSW4iLCJpYXQiOjE0ODkzMTY1ODgsImV4cCI6MTQ5MTkwNDk4OCwibmJmIjoxNDg5MzE2NTg4LCJqdGkiOiIwNzdiN2NjYjNlODE4MDU2NjBiMWNjYTkzZjI1ZTZmMyJ9.Y_BvVnJIq8k3uN1sQQuBna1CcLsrn7VnpH3RxXS8JEQ'
        }
    }
});
echo.channel('user-channel.5').listen('NewMessage', (e) => {
    console.log(e);
});

运行laravel-echo-server和调用事件后,控制台中显示以下日志,其中显示了对通道的事件广播:

[2:26:05 PM] - Xt_VSaSRHirHHxtUAAAE left channel: user-channel.5 (transport close)
[2:26:06 PM] - FwfOFMZqfrI9_H11AAAF joined channel: user-channel.5
Channel: user-channel.2
Event: NewMessage
CHANNEL user-channel.2
Channel: user-channel.1
Event: NewMessage
CHANNEL user-channel.1
Channel: user-channel.5
Event: NewMessage
CHANNEL user-channel.5

但Echo无法收听频道和发生的事件。
有什么问题?

vmpqdwk3

vmpqdwk31#

我找到解决办法了。
根据laravel-echo-server github页面上的这条评论,事件名称需要用一个点作为前缀。

echo.channel('user-channel.5').listen('.NewMessage', (e) => {
    console.log(e);
});
6pp0gazn

6pp0gazn2#

当Laravel Horizon显示成功信息时,我已经面临同样的问题好几天了:

[2019-07-18 15:18:50][123] Processing: App\Events\EventClass
[2019-07-18 15:18:50][123] Processed:  App\Events\EventClass

Laravel Echo服务器也显示成功消息:

[5:18:27 PM] - xxxxxxxxxx left channel: laravel_database_testChannel (transport close)
[5:18:28 PM] - xxxxxxxxxx joined channel: laravel_database_testChannel
Channel: laravel_database_testChannel
Event: App\Events\EventClass

下面是我在EventClass中的broadcastOn()函数:

public function broadcastOn()
{
    return new Channel('testChannel');
}

正如您所看到的,我在testChannel频道中广播了我的事件,但是在某个地方频道名称的前缀是laravel_database_
提示!因此,要找到正确的频道名称,您应该在Laravel Echo Server消息中找到它。然后您将确保使用正确的频道名称。
此外,我加入了没有.符号的通道en listen to event,如下所示:

Echo.channel('laravel_database_testChannel').listen('EventClass', (e) => {
     console.log(e);
});

这样一切都很好。希望这能保存别人面对同样问题时的时间。

相关问题