創建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