简体   繁体   English

从网页发送命令到Winform应用程序

[英]Send commands from a web page to winform application

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). 我已经使用Winform在C#中完成了一个应用程序,现在需要从在lighthttp服务器上托管的网页(作为类包含在我的应用程序解决方案中)远程控制(仅某些功能)。

This is the server code (thx to David's BlogEngine): 这是服务器代码(与David的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: 这是我的winform应用程序内部的代码,用于将其打开:

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

This is the html returned to the server: 这是返回到服务器的html:

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. 当我在浏览器中打开“ localhost:8080 / test”时,它就像一个超级按钮,但是..我不知道如何将信息从网页传递到应用程序以在其上触发事件。

ie If I press a button "close" on the webpage it fires the closing event on the winform application. 即,如果我按下网页上的“关闭”按钮,则会在winform应用程序上触发关闭事件。

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) (我将根据我的未来进展逐步更新此帖子,以使其对所有人有用)

Consider using Grapevine - this is the exact niche it was built for. 考虑使用Grapevine-这是它专门针对的利基市场。

It allows you to embed a simple REST server in your winform application and easily map incoming requests to methods. 它使您可以在Winform应用程序中嵌入一个简单的REST服务器,并轻松将传入的请求映射到方法。 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.) 通过它们,您的webapp可以通过发送简单的请求(通过前端的JavaScript或后端的C#)与之交互。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM