Samuel
Samuel

Reputation: 303

SignalR connection closed while refreshing the page

Im using SignalR for real time notification. My problem is that, after initiating connection to the server HUB, the connection gets disconnected if I refresh the page. Can anybody give me an idea why the connection gets disconnected on every page refreshes.

public class TaskHub : Hub 
{
   public void AddSession(string sessionId) 
   {
       Groups.Add(Context.ConnectionId, sessionId); 
   } 
}

Upvotes: 2

Views: 2389

Answers (1)

DI container : Hub must work singleton

builder.RegisterType<FeedHub>().ExternallyOwned().SingleInstance();

You manage OnReconnected event in hub.

public override Task OnConnected()
{
      ...
      return base.OnConnected();
}

public override Task OnReconnected()
{
     ...
     return base.OnReconnected();
}

public override Task OnDisconnected(bool stopCalled)
{
     ....
     return base.OnDisconnected(stopCalled);
}

Upvotes: 1

Related Questions