我希望能够看到用户当前是否在网站上浏览。我知道你可以检测用户是否在离线/在线客户端,但我想监控他们是否处于打开状态,并显示状态消息/颜色。
如果不是PHP,也不是socket io,我该怎么做呢?因为我认为一些防火墙会阻止socket io连接,这会破坏我的应用程序。
我使用vue js和node js,并希望检测用户当前是否正在浏览并使用它的服务器端。我发现的其他解决方案是设置超时,但是如何针对用户而不是服务器进行设置呢?
发布于 2020-06-16 19:34:54
既然您不想使用套接字,那么您的用户表应该有一个is_active列。在前端,在应用程序的入口点调用api将活动状态设置为1,这意味着它们是在线的。
您的后端控制器可以是这样的,使用express
const app = express();
app.post('/users/active-status/:id', (req, res, next) => {
const userId= req.params.id;
const status = req.params.activeStatus
// then find the user and set the is_active status to status variable
next();
});在您的前端,您可以收听关闭事件,函数并将用户活动状态设置为0
您的根组件
// when the user enters the website he is active
axios.post('/users/active-status/'+ userId, {
activeStatus: 1
});
// check before they leave and set the active status to 0
window.onclose= function (e) {
axios.post('/users/active-status/'+ userId, {
activeStatus: 0
});
};
window.addEventListener('offline', function(event){
// the user has lost connection here
});https://stackoverflow.com/questions/62406465
复制相似问题