Signalr loses connection when giving a Postback

Asked

Viewed 167 times

2

The idea is to keep the user logged in to Chat as he navigates the pages.


Layout of the components on the page

inserir a descrição da imagem aqui

  • Layout.cshtml

    function iniciarChat() {
    
        var chatHub = $.connection.chat;
        $.connection.hub.start();
    
    }
    
  • Index.cshtml

    iniciarChat();
    

Today the structure is like this. However when the user clicks on some link that makes a Postback, it is disconnected from the Chat. And when it goes back to Index.cshtml, it reconnects to Chat again.

I wonder if there is any way to keep the user connected to Chat even if he makes a Postback.

1 answer

1

What is the structure of your Hub?

I believe you will need to do a check on OnDisconnected and when adding the user to a new session.

When it "disconnect", you will have to keep it still in the mapping of the sessions for X time waiting for it to come back, when it moves to the next page you just check if the user had already created the session and assign it again.

The biggest problem will be in this "management" in time to expire, but how much to disconnect everything depends on your code on .OnDisconnected.

About "Postback" it only exists when the user sends, for example, a submit pro server and it continues on the page. When you browse between pages nay is a Postback. So it’s not that way ;)

  • Hello @Maiconcarraro ! the problem is that when the user changes pages, the javascript code of the Layout and Index pages is executed again and so the hub reconnects.

  • @alexanderPataki There’s no way you can’t run javascript if it won’t receive the calls, what you do is avoid creating a NEW session

  • How can I do that? Because when the start methodChat() is executed, it is as if a bridge was created between the client and the Hub, and if it exits the index.cshtml, it soon falls into the Ondisconnected method of the Hub.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.