【问题标题】:Laravel Socket.io,Redis event is broadcasting but not showing on client sideLaravel Socket.io,Redis 事件正在广播但未在客户端显示
【发布时间】:2018-08-30 13:49:47
【问题描述】:

在我的laravel.log 中,我可以看到该事件正在频道上播放,即

[2018-08-30 13:41:27] local.INFO: Broadcasting [App\Events\NewRating] on channels [rating] with payload:
{
    "music": {
        "id": 42,
        "name": "1535368873_admin.mp3",
        "path": "public\/music\/1535368873_admin.mp3",
        "rating": 53,
        "user_id": 4,
        "created_at": "2018-08-27 11:21:13",
        "updated_at": "2018-08-27 11:21:13",
        "is_last_played": 1,
        "is_now_playing": 0,
        "location_id": 1
    },
    "socket": null
}  

据我所知,我已经按照要求完成了所有工作。我的 redis 服务器正在运行。laravel-echo-server 正在运行,它正在加入我的频道,即

[18:41:24] - VX9bkHqs-QHRv3MvAAAC joined channel: rating

这是我订阅频道并收听事件的地方

window.app = new Vue({
        el: '#app',

        beforeMount: function () {
        alert("asdas");
        this.joinPublicChatChannel();
        },

        methods: {
        joinPublicChatChannel: function () {
            Echo.channel('rating').listen('App\\Events\\NewRating', (event) => {
                console.log("Event fired!"); //This doesnt work
            });
            }
        }
});

这是我的事件类

class NewRating implements ShouldBroadcastNow
{ 
    use Dispatchable, InteractsWithSockets, SerializesModels;

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

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

这是我的环境文件

APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=laravel_restaurant
DB_USERNAME=root
DB_PASSWORD=

BROADCAST_DRIVER=redis
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=

不知道背后的原因:/

更新

刚刚清除了所有的缓存,现在我在laravel-echo-server 控制台得到这个

Channel: rating
Event: App\Events\NewRating
Channel: rating
Event: App\Events\NewRating
Channel: rating
Event: App\Events\NewRating

【问题讨论】:

    标签: laravel redis socket.io


    【解决方案1】:

    你可以在你的活动文件中使用它:

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

    【讨论】:

      【解决方案2】:

      没关系,我已经开始运行了 :)。刚刚清除了缓存并进入

      Echo.channel('rating').listen('App\\Events\\NewRating', (event) => {
                  console.log("Event fired!"); //This doesnt work
              });
      

      我用NewRating代替App\\Events\\NewRating

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-28
        • 2017-12-21
        • 2016-07-21
        • 2016-06-12
        • 1970-01-01
        • 2016-01-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多