Question

Signalr is usually used in "server" side asp.net through hubs and connections. What I'm trying to accomplish (if possible) is using signalr as a "client" in a website. I have 2 websites, one as a server and another as a client.

I tried this

public void Start()
    {
        new Task(this.Listen).Start();
        //new Thread(this.Listen).Start();
    }
    private async void Listen()
    {
        try
        {
            using (var hubConnection = new HubConnection("http://localhost:8081"))
            {
                var myHub = hubConnection.CreateHubProxy("mediaHub");

                hubConnection.StateChanged += change => System.Diagnostics.Debug.WriteLine(change.OldState + " => " + change.NewState);

                myHub.On<string>(
                    "log",
                    message =>
                        System.Diagnostics.Debug.WriteLine(message + " Notified !")); 

                await hubConnection.Start();                 

            }
        }
        catch (Exception exc)
        {
             // ... 
        }
    }

The server is calling the client like this:

        var hub = GlobalHost.ConnectionManager.GetHubContext<MediaHub>() ;
        hub.Clients.All.log("Server: " + message);

Nothing reaches the client !

Was it helpful?

Solution

As soon as your connection is ready, you actually dispose it, so it does not stay around. As a quick test, remove the using stuff and make your hubConnection a static member of your class, and check if you get called. If you do, then from there you can refine it, but at least you have clearer what's going on.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top