locked
My hub is disconnected and connects when the page of change RRS feed

  • Question

  • User-708879383 posted

    I have an intranet and whenever page change my hub draws OnDisconnected function and Connect again.

    I need to call the function OnDisconnected only when it close the browser. When I browse the pages need it to stay connected without calling OnDisconnected function.

    Is it possible? How to solve this?

    Thank U.

    Friday, March 20, 2015 5:06 PM

Answers

  • User-1140791523 posted

    When a page is loaded and you try to connect to server, SignalR receives a new connectionId. Because a new negotiation is performed. The connectionId and its token are created by server. It is not possible to impose a connectionId to the client and ask it to connect using that old connectionId and token. These are the basic functionality and SignalR is designed this way.  ( this is incorrect. connection ids are reusable. I later discovered this. edited: 2015-03-25 13:47 )

    So, the answer to your question is negative. You cannot prevent your users disconnect and connect again whenever they leave a page and enter a new page.

    Instead I recommend to change your view and codes and instead of letting other users know about connecting and disconnecting another user whenever he navigates through your pages, inform them based on "Login"/"Logout" events. That is, whenever a user logs in, you inform others that "[username] logged in" and whenever he logs out, you tell them about this. This way they receive no other messages about connecting/disconnecting. Another thing you should take care is when the user closes his browser.

    As Edwin said, the server doesn't fire OnDisconnected event immediately. Instead it first notices a connection loss.

    Detecting closing the browser requires your own creativity. SignalR doesn't have a pre-built functionality for this.

    I have a suggestion for such a scenario that you might find useful.

    Whenever the user opens a new window or tab, a new connection is made in that new window or tab. This is the default behavior. But since the user has logged in already, you can detect that this is the same user. So, you can count the windows/tabs he has opened. Now, whenever OnDisconnected is fired, you can detect whether this was his last window/tab or not. If true, you let others know that "[username] left" (not logged out, just left).

    I hope this helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 23, 2015 9:42 PM
  • User-1140791523 posted

    To correct a wrong saying, connection ids are reusable. You can set them for your client using "id" and "ConnectionId" properties in javascript and non-javascript clients respectively.

    Regards

    M.O.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 25, 2015 5:19 AM

All replies

  • User1711366110 posted

    I need to call the function OnDisconnected only when it close the browser. When I browse the pages need it to stay connected without calling OnDisconnected function.

      As per your case, first you need to know each client connecting to a hub passes a unique connection id. You can retrieve this value in the Context.ConnectionId property of the hub context. When each client comes in, they go through OnConnected, when they leave, they go through OnDisconnected.It will raise disconnected but not immediately. By default it takes 30 seconds( inbuilt configured threshold) that SignalR will wait (after the underlying tcp connection has gone away and this isn't immediate either) before it considers a client disconnected. If the connection disconnected/dropped and reconnects before the configured timeout then it will not raise OnDisconnected.

    For more information ,refer the following similar thread link which may guide you to fix this issue :

    http://stackoverflow.com/questions/21031160/when-is-the-connection-closed-with-signalr-from-browser

    --
    with regards,
    Edwin

    Sunday, March 22, 2015 11:50 PM
  • User-708879383 posted

    I use OnDisconnected and OnConnected functions and works properly. When I close the browser of a client, all other clients are advised that I disconnected so far is correct, the problem is when I page change in my intranet, all customers are advised that I disconnected and then are told that I connected again for my new page has loaded and the HUB connected me again.

    When I close the browser is ok, the problem is in the page navigation, I did not want to be disconnected and reconnected when changing pages in my intranet.

    Is it possible to fix this?

    Thank U.

    Monday, March 23, 2015 8:14 AM
  • User-708879383 posted

    I use OnConnected method, OnDisconnected and OnReconnected. When I close the browser OnDisconnected function is called, is correct because I need to warn other users that user disconnected.

    But if I change page the same thing happens, the OnDisconnected method is called, users are advised that I desconctei, and then the new page is loaded, the OnConnected is called and I am connected again with another ID, and all users receive again a warning that I connected. This is not good because I do not desconctei intranet, I'm just browsing the pages.

    Imagine if every page I surf the intranet all users get the warning that getting disconnected and connected, disconnected and connected, disconnected and connected, all the time.

    I wish the chat from my intranet to work the same as boring facebook, view online users regardless of which page I'm me without getting disconnecting and connecting again.

    Is there a way to make it work?

    thank you.

    Monday, March 23, 2015 2:05 PM
  • User-1140791523 posted

    When a page is loaded and you try to connect to server, SignalR receives a new connectionId. Because a new negotiation is performed. The connectionId and its token are created by server. It is not possible to impose a connectionId to the client and ask it to connect using that old connectionId and token. These are the basic functionality and SignalR is designed this way.  ( this is incorrect. connection ids are reusable. I later discovered this. edited: 2015-03-25 13:47 )

    So, the answer to your question is negative. You cannot prevent your users disconnect and connect again whenever they leave a page and enter a new page.

    Instead I recommend to change your view and codes and instead of letting other users know about connecting and disconnecting another user whenever he navigates through your pages, inform them based on "Login"/"Logout" events. That is, whenever a user logs in, you inform others that "[username] logged in" and whenever he logs out, you tell them about this. This way they receive no other messages about connecting/disconnecting. Another thing you should take care is when the user closes his browser.

    As Edwin said, the server doesn't fire OnDisconnected event immediately. Instead it first notices a connection loss.

    Detecting closing the browser requires your own creativity. SignalR doesn't have a pre-built functionality for this.

    I have a suggestion for such a scenario that you might find useful.

    Whenever the user opens a new window or tab, a new connection is made in that new window or tab. This is the default behavior. But since the user has logged in already, you can detect that this is the same user. So, you can count the windows/tabs he has opened. Now, whenever OnDisconnected is fired, you can detect whether this was his last window/tab or not. If true, you let others know that "[username] left" (not logged out, just left).

    I hope this helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 23, 2015 9:42 PM
  • User-1140791523 posted

    To correct a wrong saying, connection ids are reusable. You can set them for your client using "id" and "ConnectionId" properties in javascript and non-javascript clients respectively.

    Regards

    M.O.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 25, 2015 5:19 AM