0
votes

I have self hosted SignalR server using Owin and SignalR.SelfHost in console application, also have (.Net console project) client (using SignalR.Client NuGet package), after server is hosted, I am able to start the connection from the client, I can see same connection ID in both client side and HubCallerContext.ConnectionId in Hub in OnConnected method. But when I try to call 'ReceiveMessage' method on the client using 'GlobalHost.ConnectionManager.GetHubContext' and 'Context.Clients.All.InvokeAsync("ReceiveMessage")', it doesn't reach to the client (no exception as well). I want to have this connection One way for now, that is from server to client(only push notifications from server). Both server and client are in .net framework (not .Net Core). The connection ID I see on Global HubContext is different from client connection ID.

1) Server Side Code,

public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseCors(CorsOptions.AllowAll);           
            app.MapSignalR();           
        }
    }

public static MonitoringContext _monitoringContext;
        static void Main(string[] args)
        {
            // Start OWIN host 
            using (WebApp.Start<Startup>("http://localhost:49632/"))
            {
                Console.WriteLine("Server running on {0}", "http://localhost:49632/");
                _monitoringContext = new MonitoringContext();
                RunLoop();
                //SetTimer();
                Console.ReadLine();
            }
        }
        private static void RunLoop()
        {
            do
            {
                Thread.Sleep(5000);
                _monitoringContext.CallHubClients();

            } while (true);

        }

public class MonitoringContext
    {
        IHubContext _hubContext;
        public MonitoringContext()
        {
            _hubContext = GlobalHost.ConnectionManager.GetHubContext<MonitoringHub>();
        }

        public void CallHubClients()
        {
            Console.WriteLine("Calling client with ID....");
             _hubContext.Clients.All.InvokeAsync("ReceiveMessage");

        }
    }

[HubName("MonitoringHub")]
    public class MonitoringHub : Hub
    {        
        public override Task OnConnected()
        {
            Console.WriteLine("Client connected...." + this.Context.ConnectionId);            
            return base.OnConnected();
        }

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

        public override Task OnDisconnected(bool stopCalled)
        {
            Console.WriteLine("Client disconnected....");
            return base.OnDisconnected(stopCalled);
        }

    }

2) Client side code-

static HubConnection connection;
        static IHubProxy _hubProxy;
        static Action OnReceivedAction;

        public async void RegisterClient()
        {           
            OnReceivedAction = ReceiveMessage;

            connection = new HubConnection("http://localhost:49632/");
            _hubProxy = connection.CreateHubProxy("MonitoringHub");
            _hubProxy.On("ReceiveMessage", OnReceivedAction);
            await connection.Start();

            Console.WriteLine("Hub connection started with ID...." + connection.ConnectionId );
        }

        public void ReceiveMessage()
        {
            Console.WriteLine("Message Received....Hurrey!!");           
        }
1

1 Answers

0
votes

Got the solution, when I started Trace on client side, I was calling wrong method "InvokeAsync", this was the method name searched on the client side by SignalR, when I directly called ยด"ReceiveMessage", then it was resolved to the correct method on client side.

_hubContext.Clients.All.ReceiveMessage("Reached...");

In many of the answers on stackoverflow and other places it is the following way mentioned as calling client methods, but it didn't work for me.

_hubContext.Clients.Group("Test").InvokeAsync("ReceiveMessage")