0

Background

Following along this question, I am having a problem with trying to "tell" the user that called in the first place and nobody else.


As part of the discovery phase of looking for quick and dirty solutions, I was told that SignalR would be a good choice to aid in discovery and feedback. However, I was told that it would only be for testing and never for production.

Later, management changes their mind.

So now I'm thrust into buttoning up this project and getting it ready for launch. I made this to be quick and dirty, so I have one flaw that I don't know how to fix efficiently. I tell it to broadcast to all, but I need it to broadcast to only one client.

Current Implementation

The code will have lots of things cut out, but it should give the jist of it. First, the controller.

public class HomeController : Controller
{
    HubResponder hub = new HubResponder();
    public String RunProcess(FormCollection ProcessingInfo)
    {
        hub.SignalUser("String");
    }
}

The thing that I see here is that since I construct the hub from the controller instead of the client it isn't receiving client information. I also cannot turn it into a hub since it already inherits Controller. Here's the hub:

public class HubResponder : Hub
{
    public void SignalUser(String Outbound)
    {
        IHubContext context = GlobalHost.ConnectionManager.GetHubContext<HubResponder>();
        context.Clients.All.sendStatus(Outbound);
    }
}

Now this works but it says send to everyone, which will be confusing.

Community
  • 1
  • 1

1 Answers1

-1

Okay, so here is the answer that I came up with. By using a hidden input that carries the connection ID, when the form submits I just assign to that particular location.

<input id="SignalConnectionID" type="hidden" name="SignalConnectionID" value="" />

Elsewhere:

$.connection.hub.start()
.done(function () {
$('#SignalConnectionID').val($.connection.hub.id);
});

Then when the form is submitted I and give the instantiated hub the connection ID. That's basically it.

  • Yep. That works in ideal scenarios. But, according to the [docs](http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-server#onreconnected), SignalR will assign a new connectionId after `a temporary break in connectivity` with OnReconnected. This is not handled in your js workaround and needs to be handled on the server side. – Florin Secal May 23 '16 at 09:01
  • In the case I'm using, if the user is lingering on the page too long something is typically wrong. But I can try to implement something for OnReconnected. – Zero Serenity May 24 '16 at 17:24
  • A reconnect event may happen in the first few seconds of lingering :) – Florin Secal May 24 '16 at 17:27