添加刷新逻辑
This commit is contained in:
1 parent
f40e9db080
commit
dcfa96824f
13 files changed
+875
No files matched your search
@@ -0,0 +1,157 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Windows;
|
||||
using System.Xml.Linq;
|
||||
using TouchSocket.Core;
|
||||
using TouchSocket.Http;
|
||||
using TouchSocket.Http.WebSockets;
|
||||
using TouchSocket.Sockets;
|
||||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||
|
||||
|
||||
namespace ForwardingServerUI.Class
|
||||
{
|
||||
public class AsyncHttpServer
|
||||
{
|
||||
public static HttpService httpservice = new HttpService();
|
||||
|
||||
public static async Task HttpMain()
|
||||
{
|
||||
await httpservice.SetupAsync(new TouchSocketConfig()//加载配置
|
||||
.SetListenIPHosts(7791)
|
||||
.ConfigureContainer(a =>
|
||||
{
|
||||
a.AddConsoleLogger();
|
||||
})
|
||||
.ConfigurePlugins(a =>
|
||||
{
|
||||
a.Add<MyHttpPlug1>();
|
||||
//default插件应该最后添加,其作用是
|
||||
//1、为找不到的路由返回404
|
||||
//2、处理 header 为Option的探视跨域请求。
|
||||
a.UseDefaultHttpServicePlugin();
|
||||
}));
|
||||
await httpservice.StartAsync();
|
||||
|
||||
Console.WriteLine("HttpService:Run");
|
||||
}
|
||||
|
||||
public class MyHttpPlug1 : PluginBase, IHttpPlugin
|
||||
{
|
||||
public async Task OnHttpRequest(IHttpSessionClient client, HttpContextEventArgs e)
|
||||
{
|
||||
#region HttpContext生命周期
|
||||
//var request = e.Context.Request;//http请求体
|
||||
var response = e.Context.Response;//http响应体
|
||||
#endregion
|
||||
|
||||
var bodyString = await e.Context.Request.GetBodyAsync();
|
||||
|
||||
//获取路径
|
||||
var URL = e.Context.Request.URL;
|
||||
|
||||
try
|
||||
{
|
||||
string dat;
|
||||
DataRow[] rows = MainWindowViewModel._machineTable.Select("Enterprise='/" + URL + "'");
|
||||
if (rows.Length > 0)
|
||||
{
|
||||
string? s_ = rows.First().Field<string?>("ID")!;
|
||||
|
||||
if (s_ != null)
|
||||
{
|
||||
// var deserialized = JsonSerializer.Deserialize<Dictionary<string, object>>(bodyString)!;
|
||||
|
||||
// if (deserialized.TryGetValue("Terminal", out var val) && val != null) { }
|
||||
// using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
|
||||
|
||||
await AsyncTcpServer.service.SendAsync(s_, bodyString);
|
||||
|
||||
MainWindowViewModel.Person deserializedPerson = JsonSerializer.Deserialize<MainWindowViewModel.Person>(bodyString)!;
|
||||
|
||||
Application.Current.Dispatcher.Invoke(() => {
|
||||
MainWindowViewModel._userTable.Rows.Add(client.Id, client.IP, deserializedPerson.User,
|
||||
"", deserializedPerson.Terminal, "", "");
|
||||
});
|
||||
// 创建一个 秒的延时任务
|
||||
Task delayTask = Task.Delay(15000);
|
||||
// 创建一个监测任务,不断检查变量
|
||||
Task monitorTask = Task.Run(async () =>
|
||||
{
|
||||
while (!(MainWindowViewModel._userTable.Select("Terminal='" + deserializedPerson.Terminal + "'").
|
||||
First().Field<string>("RDAT")!.Length > 1))
|
||||
{
|
||||
await Task.Delay(500);
|
||||
}
|
||||
});
|
||||
Task winner = await Task.WhenAny(delayTask, monitorTask);
|
||||
|
||||
DataRow[] dataRows = MainWindowViewModel._userTable.Select("Terminal='" + deserializedPerson.Terminal + "'");
|
||||
if (winner == monitorTask)
|
||||
{
|
||||
dat = dataRows.First().Field<string>("RDAT")!;
|
||||
}
|
||||
else { dat = "970"; }
|
||||
foreach (DataRow row in dataRows)
|
||||
{
|
||||
Application.Current.Dispatcher.Invoke(() => {
|
||||
MainWindowViewModel._userTable.Rows.Remove(row);
|
||||
});
|
||||
}
|
||||
}
|
||||
else { dat = "999"; }
|
||||
}
|
||||
else
|
||||
{
|
||||
dat = "980";
|
||||
}
|
||||
|
||||
e.Context.Response.Headers.Add("SUNLIGHT", "SUNLIGHT");
|
||||
e.Context.Response.SetStatus(200, "success");
|
||||
e.Context.Response.SetContent(Encoding.UTF8.GetBytes(dat.ToCharArray()));
|
||||
await e.Context.Response.AnswerAsync();
|
||||
}
|
||||
catch (TimeoutException)
|
||||
{
|
||||
Console.WriteLine("等待TCP服务端响应超时");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{//错误
|
||||
Console.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-DD HH:mm:ss:fff") + "] Http:" + ex.ToString());
|
||||
LogGing.ERRDATA(ex);
|
||||
//client.SendAsync("Target instruction parsing error manager will close connection");
|
||||
}
|
||||
|
||||
/* if (request.IsGet() && request.UrlEquals("/success"))
|
||||
{
|
||||
//直接响应文字
|
||||
await response
|
||||
.SetStatus(200, "success")
|
||||
.FromText("Success")
|
||||
.AnswerAsync();//直接回应
|
||||
Console.WriteLine("处理/success");
|
||||
return;
|
||||
}
|
||||
|
||||
//无法处理,调用下一个插件
|
||||
await e.InvokeNext();*/
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user