You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
176 lines
5.8 KiB
176 lines
5.8 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net.Sockets;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using TouchSocket.Core;
|
|
using TouchSocket.Http;
|
|
using TouchSocket.Http.WebSockets;
|
|
using TouchSocket.Sockets;
|
|
using static SunlightAggregationTerminal.Class.TcpServer;
|
|
|
|
namespace SunlightAggregationTerminal.Class
|
|
{
|
|
public class HttpServer
|
|
{
|
|
public static TouchSocket.Http.HttpClient httpclient = new TouchSocket.Http.HttpClient();
|
|
|
|
public static async void Configuration(string ip, string Name, string Password, string Terminal)
|
|
{
|
|
string[] parts = ip.Split('/');
|
|
|
|
if (parts.Length >= 2)
|
|
{
|
|
DataReceived.ServerIP = parts[0];
|
|
DataReceived.ServerURL = parts[1];
|
|
}
|
|
else
|
|
{
|
|
DataReceived.ServerIP = "";
|
|
DataReceived.ServerURL = "";
|
|
}
|
|
DataReceived.ServerName = Name;
|
|
DataReceived.ServerPassword = Password;
|
|
DataReceived.ServerTerminal = Terminal;
|
|
|
|
if (string.IsNullOrWhiteSpace(ip))
|
|
{
|
|
App.GlobalData.LogNo = false;
|
|
}
|
|
else
|
|
{
|
|
await HttpClient(DataReceived.ServerIP);
|
|
|
|
HttpTransmit("{\"Command\":\"Log\"}");
|
|
}
|
|
}
|
|
|
|
public static async Task HttpClient(string url)
|
|
{
|
|
var config = new TouchSocketConfig();
|
|
#region Http设置远程服务器地址
|
|
config.SetRemoteIPHost("http://" + url);
|
|
#endregion
|
|
|
|
#region Http客户端获取断线通知
|
|
config.ConfigurePlugins(a =>
|
|
{
|
|
//处理连接断开
|
|
a.AddTcpClosedPlugin(async (c, e) =>
|
|
{
|
|
//MainThread.BeginInvokeOnMainThread(() =>
|
|
// {
|
|
// Application.Current!.Windows[0].Page!.DisplayAlertAsync("", "客户端断开连接", "是");
|
|
// });
|
|
// await e.InvokeNext();
|
|
});
|
|
//自动重连
|
|
a.UseReconnection<TouchSocket.Http.HttpClient>(options =>
|
|
{
|
|
options.PollingInterval = TimeSpan.FromSeconds(3);
|
|
});
|
|
//处理连接
|
|
a.AddTcpConnectedPlugin(async (c, e) =>
|
|
{
|
|
await e.InvokeNext();
|
|
});
|
|
});
|
|
#endregion
|
|
|
|
if (httpclient.Online)//是否重置客户端
|
|
{
|
|
await httpclient.CloseAsync();
|
|
}
|
|
|
|
//配置config
|
|
await httpclient.SetupAsync(config);
|
|
//连接
|
|
await httpclient.ConnectAsync();
|
|
}
|
|
|
|
/***发送信息处理***/
|
|
public static async void HttpTransmit(string Dat)
|
|
{
|
|
if (!httpclient.Online) //检查连接
|
|
{
|
|
await httpclient.ConnectAsync();
|
|
}
|
|
Dispose();
|
|
|
|
var dat = new DataReceived.Person()
|
|
{
|
|
User = DataReceived.ServerName,
|
|
Password = DataReceived.ServerPassword,
|
|
IP = DataReceived.GetLocalIpSimple(),
|
|
Terminal = DataReceived.ServerTerminal,
|
|
Dat = Dat
|
|
};
|
|
|
|
var request = new HttpRequest();
|
|
request.SetContent(new StringHttpContent(JsonSerializer.Serialize(dat), Encoding.UTF8, "application/scjson"))
|
|
.InitHeaders()
|
|
.SetUrl(DataReceived.ServerURL)
|
|
.SetHost(httpclient.RemoteIPHost.Host)
|
|
.AsPost();
|
|
|
|
// 创建一个超时操作,15秒后取消
|
|
using var cts = new CancellationTokenSource(1000 * 15);
|
|
try
|
|
{
|
|
using (var responseResult = await httpclient.RequestAsync(request, cts.Token))
|
|
{
|
|
var response = responseResult.Response;
|
|
// GetBodyAsync 同样需要响应取消信号,如果这里超过剩余时间也会抛出异常
|
|
var result = await response.GetBodyAsync();
|
|
DataReceived.Received(result);
|
|
}
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
// 专门处理超时或主动取消的情况
|
|
MainThread.BeginInvokeOnMainThread(() =>
|
|
{
|
|
Application.Current!.Windows[0].Page!.DisplayAlertAsync("网络错误", "网络请求超时,请检查网络网络连接", "是");
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MainThread.BeginInvokeOnMainThread(() =>
|
|
{
|
|
Application.Current!.Windows[0].Page!.DisplayAlertAsync("网络错误", ex.Message.ToString(), "是");
|
|
});
|
|
}
|
|
finally
|
|
{
|
|
Start();
|
|
}
|
|
}
|
|
|
|
//倒计时5秒关闭http客户端
|
|
private static Timer? _timer;
|
|
private static bool _isDisposed = false;
|
|
private static void Start()
|
|
{
|
|
// 延迟30秒后执行一次,不自动循环
|
|
_timer = new Timer(Httpout, null, TimeSpan.FromSeconds(60), Timeout.InfiniteTimeSpan);
|
|
}
|
|
private static async void Dispose()
|
|
{
|
|
if (!_isDisposed)
|
|
{
|
|
_isDisposed = true;
|
|
_timer?.Dispose(); // 释放定时器资源
|
|
_timer = null;
|
|
}
|
|
}
|
|
private static async void Httpout(object? state)
|
|
{
|
|
if (httpclient.Online)
|
|
{
|
|
await httpclient.CloseAsync();
|
|
}
|
|
Dispose();
|
|
}
|
|
|
|
}
|
|
}
|
|
|