Jalal El-Shaer
Jalal El-Shaer

Reputation: 14710

How to use signalr as a client in asp.net website?

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 !

Upvotes: 2

Views: 753

Answers (1)

Wasp
Wasp

Reputation: 3435

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.

Upvotes: 2

Related Questions