Send commands from a web page to winform application

1.9k Views Asked by At

I've done an application in C#, using winform, that now is required to be controlled remotely (only some functions) from a webpage self-hosted on a lighthttp server (included as a class in my application solution).

This is the server code (thx to David's BlogEngine):

public class WebServer
{
    private readonly HttpListener _listener = new HttpListener();
    private readonly Func<HttpListenerRequest, string> _responderMethod;

    public WebServer(string[] prefixes, Func<HttpListenerRequest, string> method)
    {
        if (!HttpListener.IsSupported)
            throw new NotSupportedException(
                "Needs Windows XP SP2, Server 2003 or later.");

        // URI prefixes are required, for example 
        // "http://localhost:8080/index/".
        if (prefixes == null || prefixes.Length == 0)
            throw new ArgumentException("prefixes");

        // A responder method is required
        if (method == null)
            throw new ArgumentException("method");

        foreach (string s in prefixes)
            _listener.Prefixes.Add(s);

        _responderMethod = method;
        _listener.Start();
    }

    public WebServer(Func<HttpListenerRequest, string> method, params string[] prefixes)
        : this(prefixes, method) { }

    public void Run()
    {
        ThreadPool.QueueUserWorkItem((o) =>
        {
            //Console.WriteLine("Webserver running...");
            try
            {
                while (_listener.IsListening)
                {
                    ThreadPool.QueueUserWorkItem((c) =>
                    {
                        var ctx = c as HttpListenerContext;
                        try
                        {
                            string rstr = _responderMethod(ctx.Request);
                            byte[] buf = Encoding.UTF8.GetBytes(rstr);
                            ctx.Response.ContentLength64 = buf.Length;
                            ctx.Response.OutputStream.Write(buf, 0, buf.Length);
                        }
                        catch { } // suppress any exceptions
                        finally
                        {
                            // always close the stream
                            ctx.Response.OutputStream.Close();
                        }
                    }, _listener.GetContext());
                }
            }
            catch { } // suppress any exceptions
        });
    }

    public void Stop()
    {
        _listener.Stop();
        _listener.Close();
    }
}

and this is the code, inside my winform application, used to turn it on:

    WebServer ws = new WebServer(SendResponse, "http://localhost:8080/test/");
    ws.Run();

This is the html returned to the server:

public static string SendResponse(HttpListenerRequest request)
    {
        return string.Format("<HTML><BODY>My web page.<br>{0}</BODY></HTML>", DateTime.Now);
    }

When I open "localhost:8080/test" on my browser it works like a charm, but.. I don't know how to pass info from the webpage to the application to fire an event on it.

i.e. If I press a button "close" on the webpage it fires the closing event on the winform application.

What I need to implement to achieve this objective?

(I will update this post step by step with my future progress to keep it useful to everybody)

1

There are 1 best solutions below

1
Scott Offen On

Consider using Grapevine - this is the exact niche it was built for.

It allows you to embed a simple REST server in your winform application and easily map incoming requests to methods. From their, your webapp can interact with it by sending simple requests (either via JavaScript on the front end or more C# on the backend.)

https://sukona.github.io/Grapevine/en/getting-started.html