无法从laravel echo服务器接收广播的消息

时间:2019-05-17 12:18:33

标签: php laravel laravel-echo

在laravel-echo-console中:

L A R A V E L  E C H O  S E R V E R

version 1.5.2

⚠ Starting server in DEV mode...

✔  Running at localhost on port 6001
✔  Channels are ready.
✔  Listening for redis events...

Server ready!

[7:07:00 PM] - YSTAl2zDJP0HtuoCAAAA joined channel: public
[7:07:00 PM] - Sending auth request to: https://xxxxx/broadcasting/auth

[7:07:00 PM] - YSTAl2zDJP0HtuoCAAAA authenticated for: private-survey.2
[7:07:00 PM] - YSTAl2zDJP0HtuoCAAAA joined channel: private-survey.2


Channel: laravel_database_private-survey.2
Event: server.message

一切看起来不错,但我无法在浏览器中收到此消息:

window.Echo.private('survey.2')
 .listen('.server.message', (e) => {
        console.log(e)  
});
e {events: {…}, name: "private-survey.2", socket: p, options: {…}, eventFormatter: t}

我的活动课:

   public function __construct($message)
    {
        $this->message = $message;
    }

    public function broadcastOn()
    {
        return new PrivateChannel('survey.2');
    }

    public function broadcastAs()
    {
        return 'server.message';
    }

在laravel望远镜中,我看到了:

{
  message: "wdfwsfwf",
  socket: null
}

套接字为null是否正常? 此外,此事件的侦听器始终为0。

怎么了?

2 个答案:

答案 0 :(得分:1)

我的问题在这里Channel: laravel_database_ private-survey.2config/database.php

中进行更改

在这种情况下,回声必须加入“ survey.2

如果broadcasAs

 public function broadcastAs()
    {
        return 'server.message';
    }

在说不使用命名空间之前,echo必须收听“ . server.message”-dot-这是事件-不是通道!

答案 1 :(得分:0)

根据https://github.com/laravel/framework/issues/28210 注释掉 'prefix' 中 redis 的 config/database.php 行应该可以解决问题,尽管我建议进一步阅读链接以获得更多见解。

    'redis' => [

        'client' => env('REDIS_CLIENT', 'predis'),

        'options' => [
            'cluster' => env('REDIS_CLUSTER', 'predis'),
//            'prefix' => Str::slug(env('APP_NAME', 'laravel'), '_').'_database_',
        ],