Cometd:用于在线用户管理的多个选项卡会创建错误的在线状态 [英] Cometd : Multiple tabs for online-users management creating false online-status

查看:109
本文介绍了Cometd:用于在线用户管理的多个选项卡会创建错误的在线状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个基于Spring-MVC的网络应用程序,该应用程序使用Cometd进行聊天。为了实时管理哪个用户在线,我们在用户在线时发送通知。因此,当窗口关闭时,通知不会出现,并且30秒钟后将确定用户处于脱机状态或无法访问。



现在,当用户使用多个浏览器时,就会发生此问题。现在让它保留2个。因此,在10分钟后,我们将用户状态设置为远离键盘(AFK)。但是,如果用户在一个浏览器中处于在线状态,则我们将处于闪烁状态,由于浏览器处于空闲模式,因此会持续几秒钟,我们会得到一个AFK,从活动计算机中我们会得到一个可用状态。 / p>

如何解决此问题?任何想法,建议。我想到要使用布尔值标志,并加上IP地址,在覆盖通知之前将对其进行检查,但存在过时通知的问题。



这是我的代码,用于向所有收听者(用户的朋友)在线发送通知。



代码:

  @Listener(value = / service / online)
public void OnlineNotifications(ServerSession远程,ServerMessage。可变消息){
人员发送者= this.personService.getCurrentlyAuthenticatedUser();
Map< String,Object>输入= message.getDataAsMap();
String onlineStatus =(String)input.get( status);
Map< String,Object>输出=新的HashMap<>();
output.put( status,onlineStatus);
output.put( id,sender.getId());
ServerChannel serverChannel = bayeux.createChannelIfAbsent( / online / + sender.getId())。getReference();
serverChannel.setPersistent(true);
serverChannel.publish(serverSession,输出);
}

欢迎任何想法。非常感谢。 :-)

解决方案

您的应用程序可以轻松地将多个设备与相同的 userName
您可以从 SecurityPolicy 中执行此操作,只需将 userName 字符串与 List< ServerSession>
或任何适合您情况的数据结构。



您的在线状态,用于特定的 userName <因此,/ code>是此特定 userName 的所有 ServerSession 的函数。
这样,它就是从 userName 到在线状态列表的映射。



如果想象用户 bob 从两个浏览器登录,其在线状态的映射可以是:

 鲍勃-> [ afk,在线] 

计算的在线状态bob ,您只需查看每个 ServerSession 的所有联机状态,并且是否至少有一个在线则在线,如果全部都是 afk 则它不在键盘上,依此类推。



您可以通过多种方式实现此逻辑(例如,将在线状态存储为 ServerSession 属性,使用其他数据结构,缓存每个用户的在线状态,等等),



每当您从一个 ServerSession 获得在线状态更改时,您都会更新服务器-数据结构,然后从该用户的所有 ServerSession 中重新计算在线状态,这就是您发送给其他用户的信息。


I am working on a Spring-MVC based web-app which uses Cometd for chat purposes. For real-time management of which user is online, we are sending notifications when the user is online. So when window is closed, then notifications don't appear and after 30 seconds it is determined that the user is offline or not reachable.

Now the problem happens when user is over multiple browsers. Lets just keep it for 2 now. So, after 10 minutes we are setting user status to 'Away from Keyboard'(AFK). But if the user is online in one browser, then we are getting a blinking status, for few seconds because of the browser in 'Idle Mode', we get a AFK, and from the active machine we get an 'Available' status.

How can we solve this problem? Any ideas, suggestions. I thought of using a boolean flag, and couple with IP address, which will be checked before overwriting the notification, but it has a problem of stale notifications.

This is my code for sending out notifications for online to all listeners(Friends of user).

Code :

 @Listener(value = "/service/online")
    public void OnlineNotifications(ServerSession remote, ServerMessage.Mutable message) {
        Person sender = this.personService.getCurrentlyAuthenticatedUser();
        Map<String, Object> input = message.getDataAsMap();
        String onlineStatus = (String) input.get("status");
        Map<String, Object> output = new HashMap<>();
        output.put("status", onlineStatus);
        output.put("id", sender.getId());
        ServerChannel serverChannel = bayeux.createChannelIfAbsent("/online/" + sender.getId()).getReference();
        serverChannel.setPersistent(true);
        serverChannel.publish(serverSession, output);
    }

Any ideas are welcome. Thanks a lot. :-)

解决方案

Your application can easily associate multiple devices with the same userName. You can do this from your SecurityPolicy, just associate the userName string with a List<ServerSession> or whatever data structure fits better your case.

Your online state for a particular userName is therefore a function of all the ServerSessions of this particular userName. As such, it's a mapping from the userName to a list of online states.

If you imagine user bob logged in from two browsers, the mapping for its online status can be:

"bob" -> ["afk", "online"]

To compute the online status for bob you just run through all the online statuses for each ServerSession and if there is at least one online then it's online, if all are afk then it's away from the keyboard, and so on.

You can implement this logic in several ways (e.g. storing the online status as a ServerSession attribute, use a different data structure, cache the per-user online status, etc.), but you get the idea.

Whenever you get an online status change from one ServerSession, you update your server-side data structure, and then recompute the online status from all ServerSessions for that user, and that is what you send to the other users.

这篇关于Cometd:用于在线用户管理的多个选项卡会创建错误的在线状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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