Search code examples
c#signalrsignalr-hubsignalr.client

SignalR self-host windows form - UI Interaction Between Hub and GUI


Hi I have created 2 winform apps, one to act as a server (self host) and one to act as a client. My server app has a button to stop and start the server / hub and a text box to display logging information.

I can successfully send messages between the 2 applications, that is to say client receiving messages from the server and vice versa, this works great but my only query is what is the preferred way of allowing the hub, when a message is sent or received, to display that in a text box, for debug / information purposes.

How do I push the text generated from the hub's methods into to the GUI's text box control?

My Code looks like this:

Winform GUI code

public partial class Form2 : Form
{
    private IDisposable _SignalR;

    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this._SignalR = WebApp.Start<Startup>("http://localhost:8080");
    }
}

Startup is the name of my class for initializing the Hub

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        try
        {
            app.Map("/signalr", map =>
            {
                map.UseCors(CorsOptions.AllowAll);

                HubConfiguration hubConfiguration = new HubConfiguration
                {
                    EnableDetailedErrors = true,
                    EnableJSONP = true
                };

                map.RunSignalR(hubConfiguration);
            });
        }
        catch(Exception)
        {
            throw;
        }

    }
}

My Hub class (TestHub) looks like this, I have indicated where I would like to pipe the string to the GUI:

public class TestHub : Hub
{
    public override Task OnConnected()
    {
        string message = string.Format("Client connected: {0}", Context.ConnectionId);
        // Want to send details of connected user to GUI

        return base.OnConnected();
    }

    public override Task OnDisconnected(bool graceFull)
    {
        string message = string.Format("Client disconnected: {0}", Context.ConnectionId);
        // Want to send details of disconnected user to GUI

        return base.OnDisconnected(graceFull);
    }


    public void SendAll(string message)
    {
        // Want to send details of actionto GUI

        Clients.All.addMessage(message);
    }
}

Solution

  • I've just started playing around with SignalR myself, so I understand your confusion. I put links to the resources that were most helpful to me as well as an answer for your specific example.

    http://www.asp.net/signalr/overview/guide-to-the-api

    Working example: https://code.msdn.microsoft.com/Using-SignalR-in-WinForms-f1ec847b/file/119892/19/Using%20SignalR%20in%20WinForms%20and%20WPF.zip

    public partial class Form2 : Form
    {
        private IDisposable _SignalR;
    
        public Form2()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            this._SignalR = new HubConnection("http://localhost:8080").CreateHubProxy("TestHub");
            this._SignalR.On<string>("SendAll", message => { textbox1.Text = message;}
        }
    }