代码之家  ›  专栏  ›  技术社区  ›  Dave

Laravel推进器。js-计数用户会话

  •  0
  • Dave  · 技术社区  · 7 年前

    Pusher blog .在这里 source code

    示例:Jane和John连接我得到2个 usersEditing.length

    <script>
    export default {
        props: [
            'note',
        ],
    
        data() {
            return {
                title: this.note.title,
                body: this.note.body,
                usersEditing: [],
                status: ''
            }
        },
    
        mounted() {
            Echo.join(`note.${this.note.slug}`)
                .here(users => {
                    this.usersEditing = users;
                })
                .joining(user => {
                    this.usersEditing.push(user);
                })
                .leaving(user => {
                    this.usersEditing = this.usersEditing.filter(u => u != user);
                })
                .listenForWhisper('editing', (e) => {
                    this.title = e.title;
                    this.body = e.body;
                })
                .listenForWhisper('saved', (e) => {
                    this.status = e.status;
    
                    // clear is status after 1s
                    setTimeout(() => {
                        this.status = '';
                    }, 1000);
                });
        },
    
        methods: {
            editingNote() {
                let channel = Echo.join(`note.${this.note.slug}`);
    
                // show changes after 1s
                setTimeout(() => {
                    channel.whisper('editing', {
                        title: this.title,
                        body: this.body
                    });
                }, 1000);
            },
    
            updateNote() {
                let note = {
                    title: this.title, 
                    body:  this.body
                };
    
                // persist to database
                axios.patch(`/edit/${this.note.slug}`, note)
                    .then(response => {
                        // show saved status
                        this.status = response.data;
    
                        // clear is status after 1s
                        setTimeout(() => {
                            this.status = '';
                        }, 1000);
    
                        // show saved status to others
                        Echo.join(`note.${this.note.slug}`)
                            .whisper('saved', {
                                status: response.data
                            });
                    });
            }
        }
    }
    

    我需要为每个用户提供额外的价值会话。所以我可以按用户和总数显示连接数。

    有人可以帮我吗。

    Thx提前, 戴夫

    1 回复  |  直到 7 年前
        1
  •  0
  •   leesio    7 年前

    usersEditing.length 如果Jane从两个浏览器连接,我得到1 .

    这是因为pusher的存在通道基于唯一用户,而不是连接。如果您希望保持总连接数或会话数,您需要自己实现一些功能。

    /channels endpoint 在推送器中,http api允许您检索 subscription_count 以及 user_count .

    订阅计数 。如果每次订阅成功时都从客户端调用此端点。然后,您可以在每个客户端中维护订阅者计数。

    var channel = pusher.subscribe('presence-meeting-11');
    
    channel.bind('pusher:subscription_succeeded', function(members) {
      // call your new endpoint
    });