Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR - How can I call Hub Method on server from server

I have SignalR working between an ASP.NET (formerly MVC) server and a Windows Service client in the sense that the client can call methods on the Server Hub and then display to Browsers. The Hub code is:

  public class AlphaHub : Hub
        {
            public void Hello(string message)
            {
                // We got the string from the Windows Service 
                // using SignalR. Now need to send to the clients
                Clients.All.addNewMessageToPage(message);

                // Call Windows Service
                string message1 = System.Environment.MachineName;
                Clients.All.Notify(message1);


            }
     public void CallForReport(string reportName)
     {
          Clients.All.CallForReport(reportName);
     }

On the client (Windows Service) I have been calling methods on the Hub:

var hubConnection = new HubConnection("http://localhost/AlphaFrontEndService/signalr",
                    useDefaultUrl: false);
                IHubProxy alphaProxy = hubConnection.CreateHubProxy("AlphaHub");

                await hubConnection.Start();
                string cid = hubConnection.ConnectionId.ToString();
                eventLog1.WriteEntry("ConnectionID: " + cid);
                // Invoke method on hub

                await alphaProxy.Invoke("Hello", "Message from Service - ConnectionID: " + cid + " - " + System.Environment.MachineName.ToString() + " " + DateTime.Now.ToString());

Now, suppose this scenario: The user wll go a particular ASP.NET form like Insured.aspx on the server. In that I want to call CallForReport and then call this method on the client:

 public void CallFromReport(string reportName)
        {
            eventLog1.WriteEntry(reportName);
        }

How do I get a connection to my own Hub on the server and call the method. I tried the following things from Insured.aspx:

 protected void Page_Load(object sender, EventArgs e)
        {
            // Hubs.AlphaHub.CallForReport("Insured Report");
            // IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<AlphaHub>();
            // hubContext.Clients.All.CallForReport("Insured Report");
        }
like image 683
user2471435 Avatar asked Feb 14 '14 19:02

user2471435


People also ask

How many SignalR connections can a server handle?

IIS on client operating systems has a limit of 10 concurrent connections. SignalR's connections are: Transient and frequently re-established. Not disposed immediately when no longer used.

How do I send client specific messages using SignalR?

We change the BroadcastChartData() method to accept connectionId as an additional parameter. This way, we can find the client using the connectionId and send a message just to that client. Additionally, we add a new GetConnectionId() method, which returns the connectionId of the client.

How does SignalR hub work?

The SignalR Hubs API enables you to call methods on connected clients from the server. In the server code, you define methods that are called by client. In the client code, you define methods that are called from the server.


1 Answers

I don't see any calls to IHubProxy.On. That is the method you need to hook up your CallFromReport method to your AlphaHub IHubProxy on the client.

 var hubConnection = new HubConnection("http://localhost/AlphaFrontEndService/");
 IHubProxy alphaProxy = hubConnection.CreateHubProxy("AlphaHub");

 alphaProxy.On<string>("CallForReport", CallFromReport);

 await hubConnection.Start();

 // ...

Once you have that, the last two lines you have commented in Page_Load should work.

protected void Page_Load(object sender, EventArgs e)
{
    IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<AlphaHub>();
    hubContext.Clients.All.CallForReport("Insured Report");
}
like image 172
halter73 Avatar answered Sep 20 '22 11:09

halter73