Laravel Socket.io,Redis 事件正在广播但未在客户端显示

标签 laravel redis socket.io

在我的 laravel.log 中,我可以看到事件正在 channel 上广播,即

[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 正在运行并且它正在加入我的 channel ,即

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

这是我订阅 channel 和收听事件的地方

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

最佳答案

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

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

关于Laravel Socket.io,Redis 事件正在广播但未在客户端显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52098728/

相关文章:

javascript - 如何滚动到要附加数据的动态 ul 的底部 [在代码中使用 jquery、socket.io]

php - Laravel Eloquent 关系 - 表的多列引用相同的外键

php - 具有多列主键的 Laravel 模型

php - Laravel 5 如何在我看来正确使用设置变量

python - 超时不工作 django-redis-cache

redis - 什么是 Redis "out_"前缀键?

node.js - 从 Socket.IO 中删除端口并更改目录

php - Laravel 迁移两表外键依赖

ruby - 如何为 Redis-As-Rails-Cache(使用 Redis 作为 rails 缓存)Rails 4.1 实现连接池?

javascript - ESP8266 与 Socket.io 的 Socket 连接