首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Node + vue js用户离线/在线状态(服务器端)

Node + vue js用户离线/在线状态(服务器端)
EN

Stack Overflow用户
提问于 2020-06-16 18:34:31
回答 1查看 472关注 0票数 1

我希望能够看到用户当前是否在网站上浏览。我知道你可以检测用户是否在离线/在线客户端,但我想监控他们是否处于打开状态,并显示状态消息/颜色。

如果不是PHP,也不是socket io,我该怎么做呢?因为我认为一些防火墙会阻止socket io连接,这会破坏我的应用程序。

我使用vue js和node js,并希望检测用户当前是否正在浏览并使用它的服务器端。我发现的其他解决方案是设置超时,但是如何针对用户而不是服务器进行设置呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-16 19:34:54

既然您不想使用套接字,那么您的用户表应该有一个is_active列。在前端,在应用程序的入口点调用api将活动状态设置为1,这意味着它们是在线的。

您的后端控制器可以是这样的,使用express

代码语言:javascript
复制
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

您的根组件

代码语言:javascript
复制
// 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
});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62406465

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档