简体   繁体   中英

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#.

I have a winforms app that connects to a few ipcameras and creates the viewing stream. 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.

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. 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. 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.

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. The problem is that I have no idea how to do this. I looked at this question:

How to communicate between ASPX and 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.

On your web form you can use XmlHttpRequest or XDomainRequest or something like that demanding on your browser version. When someone clicks on Play button then it sends a request to your machine which is running Windows form application or Windows service.

Your Windows application catchs that request via HttpListener and then you can do what you want.

Sample:

If you have Internet Explorer 10+ you will use XmlHttpRequest but I have never used it. I have IE 9. You can convert it easily I think. When someone clicked on Play button for camera 1 via Web Form you can write that code in your button click event.

    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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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