0
希望大家都很好。 我使用MVC C#,AspNet.Identities並擁有一個功能齊全的賬戶控制器。如何在通過Web SignalR Hub斷開連接時註銷用戶?
我最近在項目中引入了一個基本的SignalR Hub,我想在用戶從集線器斷開連接後登錄用戶。我的想法是從賬戶控制器調用LogOff方法。
Hub非常簡單,事實上,如果我沒有弄錯,我從Scott Hanselman的視頻中看到了這一點。只是hitCounter的一部分...現在我試圖添加註冊()功能。
這是我到目前爲止。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
AuthenticationManager.SignOut();
return RedirectToAction("Welcome", "Home");
}//this is in my account controller
[Authorize]
[HubName("hitCounter")]
public class GameplayHub : Hub
{
private static int intCounter = 0;
public void RecordHit()
{
intCounter += 1;
this.Clients.All.onHitRecorded(intCounter);
}
public override System.Threading.Tasks.Task OnDisconnected(bool stopCalled)
{
Final.Controllers.AccountController obj = new Final.Controllers.AccountController();
intCounter -= 1;
this.Clients.All.onHitRecorded(intCounter);
this.Clients.Caller.onHitRecorded(obj.LogOff());
obj.LogOff();
return base.OnDisconnected(stopCalled);
}
}//this is the hub
<div id="hitCount" style="font-size:50px;"></div>
<script src="~/Scripts/jquery-1.6.4.js"></script>
<script src="~/Scripts/jquery.signalR-2.1.1.js"></script>
<script src="/signalr/hubs"></script>
<script>
$(function() {
var con=$.hubConnection();
var hub=con.createHubProxy('hitCounter');
hub.on('onHitRecorded', function (i) {
$('#hitCount').text(i);
});
con.start(function() {
hub.invoke('recordHit');
});
})
</script>//My view...just shows how many people active at that point...
活躍的人數,顯示正確。我在我的應用程序中試圖做的是,如果用戶位於2個選項卡上,並且他關閉了一個,則必須在兩個選項卡上註銷。
任何幫助將不勝感激!謝謝:)