当您使用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);
}
}
UserId 通知会将通知发送到不同的选项卡以及用户登录的浏览器。如果您只想将通知发送到仅 1 个浏览器中的所有选项卡,您可以使用NotifySession(session.Id) API 代替。
要向 ServiceStack 注册您的 AuthEvents 处理程序,您只需在 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
}
});