0
votes

I am trying to call a console app method from SignalR HUB context which is not working-

 var hubContext = GlobalHost.ConnectionManager.GetHubContext<Hubsfile.MyHub>();
hubContext.Clients.All.SendControl(machine, code);

This (another function below) is working fine as the client is the webpage itself(i think).

hubContext.Clients.All.registerCard(ip, data);

But when I am trying to call a method that is from different client(a console application) , Hub context is not calling it.

Does Hub context doesnt work for clients outside of the Hub Application.

Edit:

Method in SignalR HUB:

public void SendControlKeys(string machine, string code)
    {
        Clients.All.SendControl(machine, code);
    }

Method in Console client:

  proxy.On<string, string>("SendControl", (ip, data) =>
            {
                Console.WriteLine("server called SendControl");
                Console.WriteLine();
                byte[] dataBytes = HexEncoding.GetBytes(data, out int i);
                try
                {
                    lock (Clients)
                    {
                        if (Clients.Count > 0)
                        {
                            foreach (KeyValuePair<string, StateObject> client in Clients)
                            {
                                if (isClientConnected(client.Value.workSocket))
                                {
                                    if (client.Key == ip)
                                    {
                                        Send(client.Value.workSocket, dataBytes);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }catch(Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            });
1
have you connected to the signalr hub from the console client? - reckface
@reckface yes... The console client is sending data to hub. No issues with that.. The issue is only... when I am trying to call that console method from my control.aspx.cs(code behind) file using hub context. And that hub context can call the javascript client function., so I think there is no syntax error also. So I dont know where is the error.. as there is no exception or error shown from signalR or any other code part. - Usha phulwani
what's the name of the method on the console application. can you paste the signature in your question? - reckface
@reckface I have added methods in the question for refrence - Usha phulwani
So your proxy.Start(); // await or .Wait() is called after this call to proxy.On<T>()? - reckface

1 Answers

1
votes

You need to call the hub connection's Start() method after setting up the callback on the proxy.

var proxy = con.CreateHubProxy("name");
proxy.On<T>(...);
con.Start().Wait();

Then, use a single parameter in your callback. Wrap the 2 parameters you have into a single class/object.

Apart from that, ensure the name of the hub in your CreateHubProxy call is valid, that is the name of the SignalR Hub in your ASP.net application (in your case MyHub).