Laravel-Pusher-检测用户何时不再在网站中或离开页面 [英] Laravel - Pusher - Detect when a user is no longer in the website or leaves the page

查看:74
本文介绍了Laravel-Pusher-检测用户何时不再在网站中或离开页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发的实时应用程序遇到问题.我的问题是关于使用Pusher的用户状态.我想获得一种正确的方法来检测用户何时离开页面或保持一段时间不活动以将其状态设置为离线".我怎样才能做到这一点?我几乎可以肯定,不建议在客户端开发此功能,而应该在服务器端开发,但是如何?我如何问客户是否还在?我应该创建一个命令并使用cron运行它吗?是否有任何机制可以让服务器检测"套接字断开连接?任何帮助将不胜感激,Thx!

更新

使用NoseJS和SocketIO非常简单,您只需编写:

  io.on('connection',function(socket){//客户端连接时socket.on('disconnect',function(){//监听与SERVER-SIDE的断开连接//用户断开连接时的逻辑.这就是我要的!});}); 

但是在Laravel中,我只有这个:

 //一个实现shouldBroadcast接口的Laravel事件类...公共函数broadcastOn(){//我认为这是发生连接的地方,但我不知道如何检测断开连接" :(返回新的PresenceChannel('someString');} 

PD :我正在使用Laravel Echo和Pusher:)

解决方案

您可以使用在线渠道实现这一目标.然后,只要用户进入或离开频道,您就可以使用存在webhook 来通知您的服务器./p>

I'm facing a problem with a real time app that i'm developing. My problem is about user's status using Pusher. I would like to get the correct way to detect when a user leaves the page or keep unactive for a while to set it's status to 'offline', for example. How could I achieve that? I'm almost sure that is not recommended develop this feature at client-side, it should be at server-side, but how? How can I ask to clients if they are still there? Should I create a command and run it wit cron? Are there any mechanism to let the server 'detect' the socket disconnection? Any help will be appreciated, Thx!

Update

With NoseJS and SocketIO it's really simple, you just have to write:

io.on('connection', function (socket) { // When client connects
    socket.on('disconnect', function() { // Listen to disconnections from SERVER-SIDE
        // Logic when a user disconnects. This is what I want!
    });
});

But in Laravel, I only have this:

// A Laravel Event Class that implements the shouldBroadcast interface...
    public function broadcastOn()
    {
        // I think here is where connections happen, but I don't know how to detect 'disconnections' :(
        return new PresenceChannel('someString');
    }

PD: I'm using Laravel Echo and Pusher :)

解决方案

You can use presence channels to achieve this. You can then use presence webhooks to notify your server whenever a user enters or leaves the channel.

这篇关于Laravel-Pusher-检测用户何时不再在网站中或离开页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆