Asked by:
MVC5 - SignalR OnDisconnected work each time i click link ?

Question
-
User-1846805900 posted
Hi
i try to set user online or offline using SignalR- so i have create hub with Code below:
public class LoginHub : Hub { public override System.Threading.Tasks.Task OnConnected() {
// here will set user to be Online in the database using EF var username = Context.User.Identity.Name; return base.OnConnected(); } public override System.Threading.Tasks.Task OnReconnected() { var username = Context.User.Identity.Name; return base.OnReconnected(); } public override System.Threading.Tasks.Task OnDisconnected(bool s) {
// here will set user to be Offline in the database using EF var xx = Context.Headers.GetValues("Host"); var oooo = xx.FirstOrDefault(); var username = Context.User.Identity.Name; return base.OnDisconnected(true); } }and i have add js code in the layout :
<script> $(function() { var loghub = $.connection.loginHub; debugger; loghub.client.SetOnline = function() {}; // Start the connection. $.connection.hub.start().done(function () { }); }); </script>
now when i click any link on the site menu to go to the view i need OnDisconnected fired and that mean it will set user Offline - and after new page is load OnConnected is fired and set user Online again ??!!
so please how can i make OnDisconnected works only if user close browser or logoff - not with each request for any page ?
Wednesday, February 8, 2017 3:37 AM
All replies
-
User-1902643333 posted
when i click any link on the site menu to go to the view i need OnDisconnected firedThis will automatically get fired.
after new page is load OnConnected is fired and set user Online again ??!!Of course, because a new creation of session is created.
so please how can i make OnDisconnected works only if user close browser or logoff1) OnDisconnected will get fired at:
a) 5 minutes when you do nothing to the page.
b) you close the webbrowser.
c) When 1st time to open the page, first OnDisconnected then OnConnected.
2) Just write your own hub method as "LogOff", delete the status in your db to forcely make the user "LogOff".
Wednesday, February 8, 2017 10:05 AM -
User-1902643333 posted
@a.amin:
Plz read this following: https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/handling-connection-lifetime-events
Wednesday, February 8, 2017 10:08 AM -
User-1846805900 posted
a.amin
when i click any link on the site menu to go to the view i need OnDisconnected firedThis will automatically get fired.
a.amin
after new page is load OnConnected is fired and set user Online again ??!!Of course, because a new creation of session is created.
a.amin
so please how can i make OnDisconnected works only if user close browser or logoff1) OnDisconnected will get fired at:
a) 5 minutes when you do nothing to the page.
b) you close the webbrowser.
c) When 1st time to open the page, first OnDisconnected then OnConnected.
2) Just write your own hub method as "LogOff", delete the status in your db to forcely make the user "LogOff".
as i understand you that event must fired so i can't do nothing i can:
Just write your own hub method as "LogOff", delete the status in your db to forcely make the user "LogOff".
are that can make server slow response ?
Wednesday, February 8, 2017 10:41 AM -
User-1902643333 posted
are that can make server slow response ?No.
Thursday, February 9, 2017 1:16 AM