Laravel pusher.js - 统计用户会话
Laravel pusher.js - count user sessions
我已经开始使用 Laravel + Pusher 并使用了来自
Pusher blog. Here in the source code 描述了如何计算用户连接数。但是我正在努力如何将用户的会话计算为附加值。
示例:Jane 和 John 连接 usersEditing.length 我得到 2。如果 Jane 从两个浏览器连接,我得到 1 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
});
});
}
}
}
我需要为每个用户提供额外的价值会话。所以我可以按用户和总计显示连接数。
有人可以帮我吗
提前致谢,
戴夫
Jane and John connect I get 2 for usersEditing.length
. If Jane connects from two browser I get 1 for usersEditing.length
.
这是因为推送者的存在渠道基于唯一用户,而不是连接。如果您希望维护总连接数或会话数,您需要自己实现一些东西。
有多种方法可以做到这一点。推送器 http api 中的 /channels
endpoint 允许您检索 subscription_count
以及 user_count
.
您可以在您的服务器上配置一个端点,它从推送器 api 检索此数据并将更新广播到包含 subscription_count
的频道。如果每次订阅成功时从客户端调用此端点。然后您可以在每个客户端中维护订阅者数量。
var channel = pusher.subscribe('presence-meeting-11');
channel.bind('pusher:subscription_succeeded', function(members) {
// call your new endpoint
});
我已经开始使用 Laravel + Pusher 并使用了来自 Pusher blog. Here in the source code 描述了如何计算用户连接数。但是我正在努力如何将用户的会话计算为附加值。
示例:Jane 和 John 连接 usersEditing.length 我得到 2。如果 Jane 从两个浏览器连接,我得到 1 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
});
});
}
}
}
我需要为每个用户提供额外的价值会话。所以我可以按用户和总计显示连接数。
有人可以帮我吗
提前致谢, 戴夫
Jane and John connect I get 2 for
usersEditing.length
. If Jane connects from two browser I get 1 forusersEditing.length
.
这是因为推送者的存在渠道基于唯一用户,而不是连接。如果您希望维护总连接数或会话数,您需要自己实现一些东西。
有多种方法可以做到这一点。推送器 http api 中的 /channels
endpoint 允许您检索 subscription_count
以及 user_count
.
您可以在您的服务器上配置一个端点,它从推送器 api 检索此数据并将更新广播到包含 subscription_count
的频道。如果每次订阅成功时从客户端调用此端点。然后您可以在每个客户端中维护订阅者数量。
var channel = pusher.subscribe('presence-meeting-11');
channel.bind('pusher:subscription_succeeded', function(members) {
// call your new endpoint
});