我正在开发一个可以在不同选项卡中打开的AngularJS Web应用程序,我想知道哪种方法是检测用户何时在这些选项卡之一中注销的最佳方法。我想在其他标签中弹出一个登录模式窗口。
我当时正在考虑使用服务器发送的事件或跟踪服务器cookie的存在。
我从来没有做过这样的事情,所以我想问一下我的选择的利弊是什么,或者我是否错过了其他一些更明智的选择。
编辑:在服务器上,我有开箱即用的支持服务器发送事件的ServiceStack(C#)
最佳答案
当您使用ServiceStack's Server Events时,您仅在用户注销时发送通知。
首先,您需要通过实现OnLogout Session or Auth Event来检测用户已注销,例如:
public class MyAuthEvents : AuthEvents
{
public IServerEvents ServerEvents { get; set; }
public override void OnLogout(IRequest httpReq,
IAuthSession session, IServiceBase authService)
{
var channel = "home"; //replace with channel tabs are subscribed to
var msg = "...";
ServerEvents.NotifyUserId(session.UserAuthId, "cmd.logout", msg, channel);
}
}
通过用户ID进行通知会将通知发送到其他选项卡以及用户登录的浏览器。如果只想将通知发送到仅1个浏览器中的所有选项卡,则可以使用
NotifySession(session.Id)
API。要将您的AuthEvents处理程序注册到ServiceStack,您只需要在IOC中注册它,例如:
container.RegisterAs<MyAuthEvents, IAuthEvents>();
然后在JavaScript ServerEvents Client中处理 cmd.logout 通知,例如:
$(source).handleServerEvents({
handlers: {
logout: function (msg) {
//Show AngularJS dialog
$mdDialog.alert({
title: 'Attention',
textContent: msg,
ok: 'Close'
});
},
//... Other custom handlers
}
});
关于javascript - 检测标签之间的 session 结束,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37491225/