简体   繁体   English

如何在winforms应用程序和webform页面之间进行通信?

[英]How to Communicate between a winforms app and a webform page?

I just want to start by saying that while researching this I read something like this could be bad programming, but let me explain my situation first and maybe it's not that bad. 我只想首先说,在研究这个时,我读到这样的东西可能是糟糕的编程,但让我先解释一下我的情况,也许它并不是那么糟糕。 The coding is done in C#. 编码在C#中完成。

I have a winforms app that connects to a few ipcameras and creates the viewing stream. 我有一个winforms应用程序连接到几个ipcameras并创建查看流。 It saves each new frame to a filestream. 它将每个新帧保存到文件流中。 The webform has a main page that lets you select which of the cameras you'd like to view, then starts grabbing the new frames from the filestream and allows you to view the cameras. webform有一个主页面,可让您选择要查看的摄像机,然后开始从文件流中抓取新帧并允许您查看摄像机。

Right now, in the winforms app, I have a "play" button that creates the viewing stream, and the webform can only view that camera if the video is "playing" in the winforms app. 现在,在winforms应用程序中,我有一个“播放”按钮,用于创建查看流,如果视频在winforms应用程序中“播放”,则webform只能查看该相机。 So my idea was to have the winforms app be running all the time and have each camera playing, then you could select any camera from the webform and be able to view it. 所以我的想法是让winforms应用程序一直运行并让每个摄像机都在播放,然后你可以从webform中选择任何摄像机并能够查看它。 That works fine, but now I have to change it. 这很好,但现在我必须改变它。 I have to make it so when the camera is selected in the webform to then make the video start "playing" in the winforms app. 我必须这样做才能在webform中选择相机,然后让视频开始在winforms应用程序中“播放”。

So I need some sort of flag that tells the winforms app that that camera is being viewed, and once that camera is not being viewed anymore to tell the winforms app it can stop "playing" that camera. 所以我需要某种标志告诉winforms应用程序正在查看该摄像机,并且一旦不再查看该摄像机告诉winforms应用程序它就可以停止“播放”该摄像机。 The problem is that I have no idea how to do this. 问题是我不知道该怎么做。 I looked at this question: 我看了这个问题:

How to communicate between ASPX and WinForms 如何在ASPX和WinForms之间进行通信

but I didn't really understand the answer. 但我真的不明白答案。 Can anyone help me? 谁能帮我?

I hope this makes sense; 我希望这是有道理的; if not please ask me and I'll try to explain. 如果没有请问我,我会尽力解释。 I am an Electrical Engineering student and am not much of a programmer. 我是一名电气工程专业的学生,​​并不是一名程序员。

Create a Windows service application instead of Windows form application and listen on some ports via HttpListener. 创建Windows服务应用程序而不是Windows窗体应用程序,并通过HttpListener侦听某些端口。

On your web form you can use XmlHttpRequest or XDomainRequest or something like that demanding on your browser version. 在您的Web表单上,您可以使用XmlHttpRequest或XDomainRequest或类似的浏览器版本。 When someone clicks on Play button then it sends a request to your machine which is running Windows form application or Windows service. 当有人点击“播放”按钮时,它会向运行Windows窗体应用程序或Windows服务的计算机发送请求。

Your Windows application catchs that request via HttpListener and then you can do what you want. 您的Windows应用程序通过HttpListener捕获该请求,然后您可以执行您想要的操作。

Sample: 样品:

If you have Internet Explorer 10+ you will use XmlHttpRequest but I have never used it. 如果你有Internet Explorer 10+,你将使用XmlHttpRequest,但我从未使用它。 I have IE 9. You can convert it easily I think. 我有IE 9.我认为你可以轻松转换它。 When someone clicked on Play button for camera 1 via Web Form you can write that code in your button click event. 当有人通过Web Form单击相机1的“播放”按钮时,您可以在按钮单击事件中编写该代码。

    XDomainRequest xDomainRequest = new XDomainRequest();

    if (xDomainRequest ) {
                          xDomainRequest.onerror = xDomainRequestError;
                          xDomainRequest.onprogress = xDomainRequestProgress;
                          xDomainRequest.onload = xDomainRequestOnLoad;
                          xDomainRequest.ontimeout = xDomainRequestTimeOut;
                          xDomainRequest.timeout = 70000;

// Lets say the PC which runs your win.app. has an IP like: 10.10.10.10
// and lets say you will listen on 1234 port via HttpListener
// Tell Windows form application that the camera with number 1 started playing
                          var cameraUrl = "http://10.10.10.10:1234/camera/play/1;" 

                          // send request to Windows form application
                          xDomainRequest.open("POST", cameraUrl);

                          xDomainRequest.send();
                      }
                      else {
                          alert("Error!");
                      }

And in the Windows Form application which is assumed to run always: 在Windows Form应用程序中,假定始终运行:

public void StartNewThread()
        {
                Thread thread = new Thread(StartListening);
                thread.Start();
        }

        public void StartListening()
        {
                    HttpListener listener = new HttpListener();

                    string hostAddress = Dns.GetHostAddresses(Environment.MachineName)[1].ToString();

                    if (hostAddress == "[::1]" || hostAddress == "::1") { hostAddress = "127.0.0.1"; }

                    String[] prefixes = new String[] { 
                             "http://localhost:1234/camera/", 
                             "http://" + hostAddress + ":1234/camera/" ,
                             "http://" + Environment.MachineName + ":1234/camera/" };

                    int ii = 0;

                    foreach (string s in prefixes)
                    {
                        listener.Prefixes.Add(s);
                        ii++;
                    }

                while (true)
                {

            // When button clicked in Web Form for playing camera 1, Windows form application will catch it here
                    HttpListenerContext context = listener.GetContext();
                    HttpListenerRequest request = context.Request;

                    String url = request.RawUrl;
                    String[] subUrlArray = url.Split('/');
                    String queryString = subUrlArray[2];

                    HttpListenerResponse response = context.Response;

                    if (queryString == "play")
                    {
                         if (subUrlArray.Length > 2 && subUrlArray[3] != "")
                         {
                             if(subUrlArray[3] == "1")
                             {
                                 // Code for starting to play Camera 1
                             }
                             else if(subUrlArray[3] == "2")
                             {
                                 // Code for starting to play Camera 2
                             }
                         }                
                    }
                    else if (queryString == "stop")
                    {
                        if (subUrlArray.Length > 2 && subUrlArray[3] != "")
                         {
                             if(subUrlArray[3] == "1")
                             {
                                 // Code for stopping Camera 1
                             }
                             else if(subUrlArray[3] == "2")
                             {
                                 // Code for stopping Camera 2                                }
                           }
                         }     
                    }
                 }
            }

I don't know if this code has formatting or compilation errors. 我不知道这段代码是否有格式化或编译错误。 But I'm sure you can develop it. 但我相信你可以发展它。

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

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