【问题标题】:Pusher Laravel Broadcast Leave Room NotificationPusher Laravel 广播离开房间通知
【发布时间】:2021-11-19 06:29:10
【问题描述】:

我将在我的应用程序中发布我所有的方法,只是为了让我清楚地阅读。这是目标。当用户进入房间时,会添加一条消息“用户已进入房间” - 工作正常。现在,我想要做的是当用户离开房间时,我希望它说“用户已经离开房间” - 通知有效,但它显示的用户名错误(它显示查看消息的人的用户名.

我错过了什么或没有掌握什么

    methods: {
        connect() {
            if (this.currentRoom?.id) {
                let vm = this;
                this.getMessages();
                Echo.join("chat." + this.currentRoom.id)
                    .here(users => {
                        this.sendUserActivityNotification(this.$page.props.user, true)
                        this.users = users
                        this.usersCount = users.length
                    })
                    .joining(user => {
                        this.users.push(user)
                        this.usersCount = this.usersCount+1
                    })
                    .leaving(user => { 
                        this.sendUserActivityNotification(this.$page.props.user, false)
                        this.users = this.users.filter(({id}) => (id !== user.id))
                        this.usersCount = this.usersCount-1
                    })
                    .listen('NewChatMessage', (e) => {
                        vm.getMessages();
                    });
            }
        },
        setRoom(room) {
            this.currentRoom = room;
        },
        getMessages() {
            axios.get('/chat/room/' + this.currentRoom.id + '/messages')
                .then(response => {
                    this.messages = response.data;
                })
                .catch(error => {
                    console.log(error);
                })
        },
        sendUserActivityNotification(user, joining) {
            const message = `${user.name} has ${joining === true ? 'entered' : 'left'} the room.`
            return axios.post(`/chat/room/${this.currentRoom.id}/notifications`, {message}).then(() => this.getMessages())
            .catch(console.error)
        },
        leaveRoom({id}) {
            this.$store.dispatch(RoomTypes.LEAVE_ROOM, id)
        }
    },

【问题讨论】:

    标签: laravel pusher


    【解决方案1】:

    我没有使用过 Laravel Echo,但是在阅读了您的代码后,我认为在 leaving 方法调用中,您传递的是当前用户,而不是您在调用 sendUserActivityNotification 方法时从事件中收到的内容。

    您的实现如下所示:

        ...
        .leaving(user => { 
            this.sendUserActivityNotification(this.$page.props.user, false)
            this.users = this.users.filter(({id}) => (id !== user.id))
            this.usersCount = this.usersCount-1
        })
        ....
    

    您将this.$page.props.user 传递给this.sendUserActivityNotification 方法,而不是传递user。

    您必须将该调用更改为 this.sendUserActivityNotification(user, false)

    【讨论】:

      猜你喜欢
      • 2018-09-22
      • 1970-01-01
      • 2017-07-06
      • 2017-07-04
      • 2019-01-22
      • 2018-01-06
      • 2017-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多