创建Windows服务应用程序而不是Windows窗体应用程序,并通过HttpListener监听某些端口。
在您的Web表单上,您可以使用XmlHttpRequest或XDomainRequest或类似于您的浏览器版本的要求。当有人点击“播放”按钮时,它会向运行Windows窗体应用程序或Windows服务的计算机发送请求。
你的Windows应用程序通过HttpListener捕获该请求,然后你可以做你想做的事。
样品:
如果您有Internet Explorer 10+,你会使用XMLHttpRequest,但我从来没有使用过。我有IE 9.你可以很容易地转换它,我认为。当有人通过Web窗体点击相机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!");
}
并在其中被假定为始终运行Windows窗体应用程序:
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 }
}
}
}
}
}
我不知道如果这个代码具有的格式或编译错误。但我相信你可以开发它。
Web应用程序不能直接使用与WinForms应用程序相同的代码与摄像机进行交互吗?什么是相机连接到WinForms应用程序? – David 2015-02-23 16:34:34
我无法理解为什么我必须这样做,但我必须这样做。 – user3634308 2015-02-23 16:56:37