20240126
This commit is contained in:
parent
b3f5b23a49
commit
eb9ab319e7
|
@ -45,6 +45,7 @@ public class AppInfo
|
|||
public class MonitorServer
|
||||
{
|
||||
private static UdpClient UdpClient;
|
||||
private static HttpUtils HttpServer = new();
|
||||
|
||||
private static readonly ConcurrentDictionary<string, AppInfo> AppInfos = new();
|
||||
|
||||
|
@ -66,9 +67,33 @@ public class MonitorServer
|
|||
/// </summary>
|
||||
public static void Startup()
|
||||
{
|
||||
HttpServer.CreateHttp("http://+:5002/", HttpListenerEvent);
|
||||
|
||||
TaskUtils.Run(AppMonitorProcess, 2000, Guid.NewGuid());
|
||||
}
|
||||
|
||||
private static void HttpListenerEvent(object sender, HttpListenerContext e)
|
||||
{
|
||||
e.Response.AddHeader("Content-Type", "application/json");
|
||||
|
||||
if (e.Request.HttpMethod == "GET")
|
||||
{
|
||||
e.Response.OutputStream.Write("OK");
|
||||
}
|
||||
else if (e.Request.HttpMethod == "POST")
|
||||
{
|
||||
try
|
||||
{
|
||||
string json = string.Empty;
|
||||
using (StreamReader reader = new StreamReader(e.Request.InputStream, Encoding.UTF8))
|
||||
json = reader.ReadToEnd();
|
||||
|
||||
Debug.WriteLine("POST" + json);
|
||||
}
|
||||
catch { e.Response.OutputStream.Write(JsonUtils.ToJson(new { status = false, picdata = "" })); }
|
||||
}
|
||||
}
|
||||
|
||||
private static void RequestCallback(IAsyncResult ar)
|
||||
{
|
||||
var point = new IPEndPoint(IPAddress.Any, 0);
|
||||
|
@ -157,7 +182,7 @@ public class MonitorServer
|
|||
{
|
||||
if (name == null) return;
|
||||
|
||||
if (AppInfos.TryGetValue(name, out AppInfo? app))
|
||||
if (AppInfos.TryGetValue(name, out AppInfo app))
|
||||
{
|
||||
if (app == null) return;
|
||||
|
||||
|
@ -184,7 +209,8 @@ public class MonitorServer
|
|||
/* 若进程不存在, 启动进程 */
|
||||
if (process.Length == 0)
|
||||
{
|
||||
Process.Start(app.Path);
|
||||
if (File.Exists(app.Path))
|
||||
Process.Start(app.Path);
|
||||
}
|
||||
//else
|
||||
//{
|
||||
|
|
|
@ -11,7 +11,7 @@ public class FileUtils
|
|||
public static long Size(string fullpath) { try { return new FileInfo(fullpath).Length; } catch { return 0; } }
|
||||
public static string GetName(string fullpath) { try { return new FileInfo(fullpath).Name; } catch { return ""; } }
|
||||
public static string ReadString(string fullpath) { try { return File.ReadAllText(fullpath); } catch { return ""; } }
|
||||
public static byte[]? ReadBytes(string fullpath) { try { return File.ReadAllBytes(fullpath); } catch { return null; } }
|
||||
public static byte[] ReadBytes(string fullpath) { try { return File.ReadAllBytes(fullpath); } catch { return null; } }
|
||||
|
||||
public static void Append(string fullpath, string contents)
|
||||
{
|
||||
|
|
111
HxServer/HxServer/Utils/HttpUtils.cs
Normal file
111
HxServer/HxServer/Utils/HttpUtils.cs
Normal file
|
@ -0,0 +1,111 @@
|
|||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
|
||||
namespace HxServer.Utils;
|
||||
|
||||
/// <summary>
|
||||
/// HTTP扩展库
|
||||
/// </summary>
|
||||
public class HttpUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// HTTP GET/POST数据接收回调函数
|
||||
/// </summary>
|
||||
public EventHandler<HttpListenerContext> ListenCallBack;
|
||||
|
||||
/// <summary>
|
||||
/// 创建HTTP服务端
|
||||
/// </summary>
|
||||
/// <param name="url">监听的地址</param>
|
||||
/// <param name="callback">回调函数</param>
|
||||
/// <returns>true 创建成功 false 失败</returns>
|
||||
public bool CreateHttp(string url, EventHandler<HttpListenerContext> callback)
|
||||
{
|
||||
ServicePointManager.DefaultConnectionLimit = 1023;
|
||||
|
||||
ListenCallBack = callback;
|
||||
|
||||
HttpListener DataSever = new();
|
||||
|
||||
try
|
||||
{
|
||||
DataSever.Prefixes.Add(url);
|
||||
DataSever.Start();
|
||||
DataSever.BeginGetContext(RequestCallBack, DataSever);
|
||||
}
|
||||
catch (Exception ex) { Console.WriteLine($"Http Create {ex}"); }
|
||||
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine($"Http Create Success");
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
|
||||
return DataSever.IsListening;
|
||||
}
|
||||
|
||||
private void RequestCallBack(IAsyncResult ar)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine($"Http RequestCallBack Receive data");
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
|
||||
HttpListener DataSever = ar.AsyncState as HttpListener;
|
||||
HttpListenerContext Context = DataSever.EndGetContext(ar);
|
||||
DataSever.BeginGetContext(RequestCallBack, DataSever);
|
||||
|
||||
/* 设置返回数据为JSON类 */
|
||||
Context.Response.AddHeader("Content-Type", "application/json");
|
||||
|
||||
System.Threading.Tasks.Task.Factory.StartNew(() => ListenCallBack?.Invoke(null, Context));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送Get请求
|
||||
/// </summary>
|
||||
/// <param name="Url">地址</param>
|
||||
/// <param name="TimeOut">超时时间(秒)</param>
|
||||
/// <param name="result">结果</param>
|
||||
/// <returns>发送状态</returns>
|
||||
public static bool Get(string url, int seconds, out string result)
|
||||
{
|
||||
result = string.Empty;
|
||||
|
||||
using var client = new HttpClient() { Timeout = TimeSpan.FromSeconds(seconds) };
|
||||
var response = client.GetAsync(url).Result;
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
result = response.Content.ReadAsStringAsync().Result;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送Post请求
|
||||
/// </summary>
|
||||
/// <param name="Url">地址</param>
|
||||
/// <param name="Data">数据</param>
|
||||
/// <param name="TimeOut">超时时间(秒)</param>
|
||||
/// <param name="result">结果</param>
|
||||
/// <returns></returns>
|
||||
public static bool Post(string url, object data, int seconds, out string result)
|
||||
{
|
||||
result = string.Empty;
|
||||
|
||||
HttpContent content = new StringContent(data.ToString());
|
||||
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/josn");
|
||||
using HttpClient client = new();
|
||||
HttpResponseMessage res = client.PostAsync(url, content).Result;
|
||||
if (res.IsSuccessStatusCode)
|
||||
{
|
||||
result = res.Content.ReadAsStringAsync().Result;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -9,7 +9,7 @@ namespace HxServer.Utils;
|
|||
|
||||
public class JsonUtils
|
||||
{
|
||||
public static string ToJson(object? value)
|
||||
public static string ToJson(object value)
|
||||
{
|
||||
if (value == null)
|
||||
return "";
|
||||
|
@ -17,7 +17,7 @@ public class JsonUtils
|
|||
return JsonConvert.SerializeObject(value);
|
||||
}
|
||||
|
||||
public static Dictionary<string, object> ToDictionary(object? message)
|
||||
public static Dictionary<string, object> ToDictionary(object message)
|
||||
{
|
||||
var json = message?.ToString();
|
||||
|
||||
|
|
21
HxServer/HxServer/Utils/StreamUtils.cs
Normal file
21
HxServer/HxServer/Utils/StreamUtils.cs
Normal file
|
@ -0,0 +1,21 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HxServer.Utils
|
||||
{
|
||||
public static class StreamUtils
|
||||
{
|
||||
public static void Write(this Stream stream, object data)
|
||||
{
|
||||
try
|
||||
{
|
||||
using StreamWriter writer = new(stream);
|
||||
writer.Write(data.ToString());
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
}
|
|
@ -21,7 +21,7 @@ namespace HxServer.Utils
|
|||
client.Send(buffer);
|
||||
}
|
||||
|
||||
public static void Send(this UdpClient client, IPEndPoint? point, object obj)
|
||||
public static void Send(this UdpClient client, IPEndPoint point, object obj)
|
||||
{
|
||||
var data = obj.ToString();
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user