带有 JS 的动态 HTTP 侦听器
Dynamic HTTP listener with JS
我正在开发一个 winform 应用程序,我想使用 HTTP 侦听器实现(使用 CSS 和 JS)来动态通知用户 winform 应用程序的状态。我所说的动态是指网页的内容会在不需要重新加载网页本身(创建新请求)的情况下更新。我的想法是使用 javascript ,例如每秒检查一些 c# 变量的状态,但我不知道 out/google 怎么做。
这样的事情有可能吗?我对全新的解决方案持开放态度,例如添加一个新的 Web 项目(.net 核心下的 asp 等),该项目将被编译为 dll 和来自 winform 应用程序的 运行 但我想避免部署我的带有独立服务器的应用程序。
下面的侦听器示例:
// This example requires the System and System.Net namespaces.
public static void SimpleListenerExample(string[] prefixes)
{
if (!HttpListener.IsSupported)
{
Console.WriteLine ("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
return;
}
// URI prefixes are required,
// for example "http://contoso.com:8080/index/".
if (prefixes == null || prefixes.Length == 0)
throw new ArgumentException("prefixes");
// Create a listener.
HttpListener listener = new HttpListener();
// Add the prefixes.
foreach (string s in prefixes)
{
listener.Prefixes.Add(s);
}
listener.Start();
Console.WriteLine("Listening...");
// Note: The GetContext method blocks while waiting for a request.
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
// Obtain a response object.
HttpListenerResponse response = context.Response;
// Construct a response.
string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
output.Write(buffer,0,buffer.Length);
// You must close the output stream.
output.Close();
listener.Stop();
}
经过一些研究,我按照评论中的建议使用了 ajax。基本上我的 javascript 使用 setInterval() 函数每秒进行一次查询(例如 get_content),并且我在我的 c# 代码中定义了一个响应。
我正在开发一个 winform 应用程序,我想使用 HTTP 侦听器实现(使用 CSS 和 JS)来动态通知用户 winform 应用程序的状态。我所说的动态是指网页的内容会在不需要重新加载网页本身(创建新请求)的情况下更新。我的想法是使用 javascript ,例如每秒检查一些 c# 变量的状态,但我不知道 out/google 怎么做。
这样的事情有可能吗?我对全新的解决方案持开放态度,例如添加一个新的 Web 项目(.net 核心下的 asp 等),该项目将被编译为 dll 和来自 winform 应用程序的 运行 但我想避免部署我的带有独立服务器的应用程序。
下面的侦听器示例:
// This example requires the System and System.Net namespaces.
public static void SimpleListenerExample(string[] prefixes)
{
if (!HttpListener.IsSupported)
{
Console.WriteLine ("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
return;
}
// URI prefixes are required,
// for example "http://contoso.com:8080/index/".
if (prefixes == null || prefixes.Length == 0)
throw new ArgumentException("prefixes");
// Create a listener.
HttpListener listener = new HttpListener();
// Add the prefixes.
foreach (string s in prefixes)
{
listener.Prefixes.Add(s);
}
listener.Start();
Console.WriteLine("Listening...");
// Note: The GetContext method blocks while waiting for a request.
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
// Obtain a response object.
HttpListenerResponse response = context.Response;
// Construct a response.
string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
output.Write(buffer,0,buffer.Length);
// You must close the output stream.
output.Close();
listener.Stop();
}
经过一些研究,我按照评论中的建议使用了 ajax。基本上我的 javascript 使用 setInterval() 函数每秒进行一次查询(例如 get_content),并且我在我的 c# 代码中定义了一个响应。