A:调整http监听及websockt状态管理
This commit is contained in:
1 parent
38d9378e0f
commit
26182b28d4
13 files changed
+201
-428
No files matched your search
@@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<TargetFramework>net8.0-windows</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
@@ -14,4 +14,8 @@
|
||||
<PackageReference Include="Fleck" Version="1.2.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Utils.ETW\Utils.ETW.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,41 +0,0 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Application;
|
||||
|
||||
|
||||
// 7. 示例消息处理器
|
||||
public class ChatMessageHandler : IMessageHandler
|
||||
{
|
||||
public string MessageType => "chat";
|
||||
|
||||
public async Task<object?> HandleAsync(string connectionId, string data)
|
||||
{
|
||||
try
|
||||
{
|
||||
var chatData = JsonSerializer.Deserialize<ChatData>(data);
|
||||
if (chatData == null) return new { status = "error", message = "无效的聊天数据" };
|
||||
|
||||
// 处理聊天逻辑
|
||||
return new
|
||||
{
|
||||
status = "success",
|
||||
message = "聊天消息已接收",
|
||||
echo = new
|
||||
{
|
||||
from = connectionId,
|
||||
content = chatData.Content,
|
||||
timestamp = DateTime.UtcNow
|
||||
}
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new { status = "error", message = ex.Message };
|
||||
}
|
||||
}
|
||||
|
||||
public class ChatData
|
||||
{
|
||||
public string Content { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
|
||||
namespace Application;
|
||||
|
||||
public class Class1
|
||||
{
|
||||
void main()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
namespace Application;
|
||||
|
||||
|
||||
// 4. 实现连接管理器
|
||||
using System.Collections.Concurrent;
|
||||
using System.Net.WebSockets;
|
||||
|
||||
public class ConnectionManager : IConnectionManager
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, WebSocket> _connections = new();
|
||||
|
||||
public void AddConnection(string connectionId, WebSocket webSocket)
|
||||
{
|
||||
_connections.TryAdd(connectionId, webSocket);
|
||||
}
|
||||
|
||||
public void RemoveConnection(string connectionId)
|
||||
{
|
||||
if (_connections.TryRemove(connectionId, out var webSocket))
|
||||
{
|
||||
if (webSocket.State == WebSocketState.Open)
|
||||
{
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Connection removed", CancellationToken.None);
|
||||
}
|
||||
catch { /* 忽略关闭时的异常 */ }
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public WebSocket? GetConnection(string connectionId)
|
||||
{
|
||||
_connections.TryGetValue(connectionId, out var webSocket);
|
||||
return webSocket?.State == WebSocketState.Open ? webSocket : null;
|
||||
}
|
||||
|
||||
public IEnumerable<string> GetAllConnectionIds()
|
||||
{
|
||||
return _connections.Where(c => c.Value.State == WebSocketState.Open).Select(c => c.Key);
|
||||
}
|
||||
|
||||
public int Count => _connections.Count(c => c.Value.State == WebSocketState.Open);
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Common.Net.WebSocketConn;
|
||||
using Utils.ETW.Etw;
|
||||
|
||||
namespace Shell;
|
||||
|
||||
public class DispatchEngine
|
||||
{
|
||||
private static readonly Lazy<DispatchEngine> _instance = new(() => new DispatchEngine());
|
||||
public static DispatchEngine Instance => _instance.Value;
|
||||
|
||||
private DispatchEngine()
|
||||
{
|
||||
}
|
||||
// region 软件信息订阅
|
||||
|
||||
/// <summary>
|
||||
/// 软件信息订阅
|
||||
/// </summary>
|
||||
private ConcurrentDictionary<string, DispatchModel> ApplicationInfoDispatch = [];
|
||||
|
||||
/// <summary>
|
||||
/// 软件信息上次发送时间
|
||||
/// </summary>
|
||||
private ConcurrentDictionary<string, DateTime> AppInfoLastSendTime = [];
|
||||
|
||||
/// <summary>
|
||||
/// 添加订阅
|
||||
/// </summary>
|
||||
public void AddApplicationInfo(string clientId, DispatchModel model)
|
||||
{
|
||||
ApplicationInfoDispatch.TryAdd(clientId, model);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新订阅
|
||||
/// </summary>
|
||||
public void UpdateApplicationInfo(string clientId, DispatchModel model)
|
||||
{
|
||||
ApplicationInfoDispatch[clientId] = model;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除订阅
|
||||
/// </summary>
|
||||
public void DeleteApplicationInfo(string clientId)
|
||||
{
|
||||
ApplicationInfoDispatch.TryRemove(clientId, out _);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 软件信息分发
|
||||
/// </summary>
|
||||
public void ApplicationInfoDistribute()
|
||||
{
|
||||
var timer = new Timer(state =>
|
||||
{
|
||||
if (ApplicationInfoDispatch.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取信息
|
||||
var programInfos = GlobalNetworkMonitor.Instance.GetAllPrograms();
|
||||
var serialize = JsonSerializer.Serialize(programInfos);
|
||||
|
||||
foreach (var keyValuePair in ApplicationInfoDispatch)
|
||||
{
|
||||
// 间隔时间
|
||||
var interval = keyValuePair.Value.Interval;
|
||||
// 上一次是否发送
|
||||
var isHasLast = AppInfoLastSendTime.TryGetValue(keyValuePair.Key, out DateTime lastSendTime);
|
||||
|
||||
// 本次是否应该发送
|
||||
var isShouldSend = false;
|
||||
// 具有上一次,检查时间
|
||||
if (isHasLast)
|
||||
{
|
||||
var intervalTime = (DateTime.Now - lastSendTime).TotalMilliseconds;
|
||||
|
||||
isShouldSend = intervalTime >= interval;
|
||||
}
|
||||
// 没有上一次,直接发送
|
||||
else
|
||||
{
|
||||
isShouldSend = true;
|
||||
}
|
||||
|
||||
// 是否应该发送
|
||||
if (isShouldSend)
|
||||
{
|
||||
AppInfoLastSendTime[keyValuePair.Key] = DateTime.Now;
|
||||
var rm = new ResponseMessage
|
||||
{
|
||||
Success = true,
|
||||
Data = serialize,
|
||||
Message = "数据发送",
|
||||
Timestamp = AppInfoLastSendTime[keyValuePair.Key]
|
||||
};
|
||||
WebSocketManager.Instance.SendToClient(keyValuePair.Key, rm);
|
||||
}
|
||||
}
|
||||
}, null, TimeSpan.Zero, TimeSpan.FromSeconds(1));
|
||||
}
|
||||
|
||||
// endregion
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 调度参数模型
|
||||
/// </summary>
|
||||
public class DispatchModel
|
||||
{
|
||||
/// <summary>
|
||||
/// 信息间隔
|
||||
/// </summary>
|
||||
public int Interval { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 订阅信息内容
|
||||
/// </summary>
|
||||
public class SubscriptionInfo
|
||||
{
|
||||
[JsonPropertyName("subscriptionType")] public string SubscriptionType { get; set; }
|
||||
[JsonPropertyName("interval")] public int Interval { get; set; }
|
||||
}
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
using System.Net.WebSockets;
|
||||
|
||||
namespace Application;
|
||||
|
||||
// 1. 服务接口定义 - 便于单元测试和依赖注入
|
||||
public interface IWebSocketServerService
|
||||
{
|
||||
event Action<string, string>? MessageReceived;
|
||||
event Action<string>? ClientConnected;
|
||||
event Action<string>? ClientDisconnected;
|
||||
event Action<string>? ServerMessage;
|
||||
|
||||
Task<bool> StartAsync(string url);
|
||||
Task StopAsync();
|
||||
Task SendMessageAsync(string connectionId, object message);
|
||||
Task BroadcastMessageAsync(object message);
|
||||
bool IsRunning { get; }
|
||||
int ConnectionCount { get; }
|
||||
IEnumerable<string> GetConnectedClients();
|
||||
}
|
||||
|
||||
// 2. 消息处理策略接口
|
||||
public interface IMessageHandler
|
||||
{
|
||||
string MessageType { get; }
|
||||
Task<object?> HandleAsync(string connectionId, string data);
|
||||
}
|
||||
|
||||
// 3. 连接管理接口
|
||||
public interface IConnectionManager
|
||||
{
|
||||
void AddConnection(string connectionId, WebSocket webSocket);
|
||||
void RemoveConnection(string connectionId);
|
||||
WebSocket? GetConnection(string connectionId);
|
||||
IEnumerable<string> GetAllConnectionIds();
|
||||
int Count { get; }
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
namespace Application;
|
||||
|
||||
|
||||
// 5. 消息模型
|
||||
public class WebSocketMessage
|
||||
{
|
||||
public string Type { get; set; } = string.Empty;
|
||||
public string Data { get; set; } = string.Empty;
|
||||
public DateTime Timestamp { get; set; } = DateTime.UtcNow;
|
||||
public string? ConnectionId { get; set; }
|
||||
}
|
||||
@@ -1,276 +0,0 @@
|
||||
namespace Application;
|
||||
|
||||
|
||||
// 6. 主要的WebSocket服务实现
|
||||
using System.Net;
|
||||
using System.Net.WebSockets;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
public class WebSocketServerService : IWebSocketServerService, IDisposable
|
||||
{
|
||||
private readonly IConnectionManager _connectionManager;
|
||||
private readonly Dictionary<string, IMessageHandler> _messageHandlers;
|
||||
private HttpListener? _httpListener;
|
||||
private CancellationTokenSource? _cancellationTokenSource;
|
||||
private readonly SemaphoreSlim _startStopSemaphore = new(1, 1);
|
||||
|
||||
public event Action<string, string>? MessageReceived;
|
||||
public event Action<string>? ClientConnected;
|
||||
public event Action<string>? ClientDisconnected;
|
||||
public event Action<string>? ServerMessage;
|
||||
|
||||
public bool IsRunning { get; private set; }
|
||||
public int ConnectionCount => _connectionManager.Count;
|
||||
|
||||
public WebSocketServerService(IConnectionManager connectionManager)
|
||||
{
|
||||
_connectionManager = connectionManager;
|
||||
_messageHandlers = new Dictionary<string, IMessageHandler>();
|
||||
}
|
||||
|
||||
// 注册消息处理器
|
||||
public void RegisterMessageHandler(IMessageHandler handler)
|
||||
{
|
||||
_messageHandlers[handler.MessageType.ToLower()] = handler;
|
||||
}
|
||||
|
||||
public async Task<bool> StartAsync(string url)
|
||||
{
|
||||
await _startStopSemaphore.WaitAsync();
|
||||
try
|
||||
{
|
||||
if (IsRunning) return true;
|
||||
|
||||
_httpListener = new HttpListener();
|
||||
_httpListener.Prefixes.Add(url);
|
||||
_cancellationTokenSource = new CancellationTokenSource();
|
||||
|
||||
try
|
||||
{
|
||||
_httpListener.Start();
|
||||
IsRunning = true;
|
||||
ServerMessage?.Invoke($"WebSocket服务器已启动: {url}");
|
||||
|
||||
// 开始监听连接
|
||||
_ = Task.Run(() => ListenForConnectionsAsync(_cancellationTokenSource.Token));
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ServerMessage?.Invoke($"启动服务器失败: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_startStopSemaphore.Release();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task StopAsync()
|
||||
{
|
||||
await _startStopSemaphore.WaitAsync();
|
||||
try
|
||||
{
|
||||
if (!IsRunning) return;
|
||||
|
||||
_cancellationTokenSource?.Cancel();
|
||||
_httpListener?.Stop();
|
||||
_httpListener?.Close();
|
||||
|
||||
// 关闭所有连接
|
||||
foreach (var connectionId in _connectionManager.GetAllConnectionIds().ToList())
|
||||
{
|
||||
_connectionManager.RemoveConnection(connectionId);
|
||||
}
|
||||
|
||||
IsRunning = false;
|
||||
ServerMessage?.Invoke("WebSocket服务器已停止");
|
||||
}
|
||||
finally
|
||||
{
|
||||
_startStopSemaphore.Release();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ListenForConnectionsAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
while (!cancellationToken.IsCancellationRequested && _httpListener!.IsListening)
|
||||
{
|
||||
try
|
||||
{
|
||||
var context = await _httpListener.GetContextAsync();
|
||||
|
||||
if (context.Request.IsWebSocketRequest)
|
||||
{
|
||||
// 不等待,让连接处理在后台进行
|
||||
_ = Task.Run(() => ProcessWebSocketRequestAsync(context, cancellationToken), cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 对非WebSocket请求返回404
|
||||
context.Response.StatusCode = 404;
|
||||
context.Response.Close();
|
||||
}
|
||||
}
|
||||
catch (HttpListenerException) when (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
break;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ServerMessage?.Invoke($"监听连接时出错: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ProcessWebSocketRequestAsync(HttpListenerContext context, CancellationToken cancellationToken)
|
||||
{
|
||||
WebSocketContext? webSocketContext = null;
|
||||
string connectionId = Guid.NewGuid().ToString();
|
||||
|
||||
try
|
||||
{
|
||||
webSocketContext = await context.AcceptWebSocketAsync(null);
|
||||
var webSocket = webSocketContext.WebSocket;
|
||||
|
||||
_connectionManager.AddConnection(connectionId, webSocket);
|
||||
ClientConnected?.Invoke(connectionId);
|
||||
ServerMessage?.Invoke($"客户端连接: {connectionId}");
|
||||
|
||||
await HandleWebSocketConnectionAsync(webSocket, connectionId, cancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ServerMessage?.Invoke($"处理WebSocket请求时出错 [{connectionId}]: {ex.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
_connectionManager.RemoveConnection(connectionId);
|
||||
ClientDisconnected?.Invoke(connectionId);
|
||||
ServerMessage?.Invoke($"客户端断开: {connectionId}");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleWebSocketConnectionAsync(WebSocket webSocket, string connectionId, CancellationToken cancellationToken)
|
||||
{
|
||||
var buffer = new byte[1024 * 4];
|
||||
|
||||
try
|
||||
{
|
||||
while (webSocket.State == WebSocketState.Open && !cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), cancellationToken);
|
||||
|
||||
if (result.MessageType == WebSocketMessageType.Text)
|
||||
{
|
||||
var message = Encoding.UTF8.GetString(buffer, 0, result.Count);
|
||||
MessageReceived?.Invoke(connectionId, message);
|
||||
|
||||
// 处理消息但不等待响应,避免阻塞接收循环
|
||||
_ = Task.Run(() => ProcessMessageAsync(message, connectionId), cancellationToken);
|
||||
}
|
||||
else if (result.MessageType == WebSocketMessageType.Close)
|
||||
{
|
||||
await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "", cancellationToken);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
// 正常取消操作
|
||||
}
|
||||
catch (WebSocketException ex) when (ex.WebSocketErrorCode == WebSocketError.ConnectionClosedPrematurely)
|
||||
{
|
||||
// 客户端异常断开连接
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ServerMessage?.Invoke($"处理WebSocket连接时出错 [{connectionId}]: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ProcessMessageAsync(string message, string connectionId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var messageObj = JsonSerializer.Deserialize<WebSocketMessage>(message);
|
||||
if (messageObj == null) return;
|
||||
|
||||
messageObj.ConnectionId = connectionId;
|
||||
messageObj.Timestamp = DateTime.UtcNow;
|
||||
|
||||
// 查找对应的消息处理器
|
||||
if (_messageHandlers.TryGetValue(messageObj.Type.ToLower(), out var handler))
|
||||
{
|
||||
var response = await handler.HandleAsync(connectionId, messageObj.Data);
|
||||
if (response != null)
|
||||
{
|
||||
await SendMessageAsync(connectionId, response);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var errorResponse = new { status = "error", message = $"未知消息类型: {messageObj.Type}" };
|
||||
await SendMessageAsync(connectionId, errorResponse);
|
||||
}
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
var errorResponse = new { status = "error", message = "无效的JSON格式" };
|
||||
await SendMessageAsync(connectionId, errorResponse);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ServerMessage?.Invoke($"处理消息时出错 [{connectionId}]: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SendMessageAsync(string connectionId, object message)
|
||||
{
|
||||
var webSocket = _connectionManager.GetConnection(connectionId);
|
||||
if (webSocket != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var json = JsonSerializer.Serialize(message);
|
||||
var buffer = Encoding.UTF8.GetBytes(json);
|
||||
await webSocket.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Text, true, CancellationToken.None);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ServerMessage?.Invoke($"发送消息失败 [{connectionId}]: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task BroadcastMessageAsync(object message)
|
||||
{
|
||||
var json = JsonSerializer.Serialize(message);
|
||||
var tasks = new List<Task>();
|
||||
|
||||
foreach (var connectionId in _connectionManager.GetAllConnectionIds())
|
||||
{
|
||||
tasks.Add(SendMessageAsync(connectionId, message));
|
||||
}
|
||||
|
||||
if (tasks.Count > 0)
|
||||
{
|
||||
await Task.WhenAll(tasks);
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<string> GetConnectedClients()
|
||||
{
|
||||
return _connectionManager.GetAllConnectionIds();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_ = Task.Run(async () => await StopAsync());
|
||||
_startStopSemaphore?.Dispose();
|
||||
_cancellationTokenSource?.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -317,9 +317,26 @@ public static class Middlewares
|
||||
// CORS 中间件
|
||||
public static async Task<bool> Cors(HttpContext context)
|
||||
{
|
||||
context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
|
||||
context.Response.Headers.Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
|
||||
context.Response.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Authorization");
|
||||
|
||||
// 允许所有来源(生产环境应该指定具体域名)
|
||||
string? origin = context.Request.Headers["Origin"];
|
||||
context.Response.Headers.Add("Access-Control-Allow-Origin", !string.IsNullOrEmpty(origin) ? origin : "*");
|
||||
|
||||
// 允许的HTTP方法
|
||||
context.Response.Headers.Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, PATCH");
|
||||
|
||||
// 关键:允许的请求头(包括你的uuid头)
|
||||
context.Response.Headers.Add("Access-Control-Allow-Headers",
|
||||
"Content-Type, Authorization, X-Requested-With, uuid, Accept, Origin");
|
||||
|
||||
// 允许携带凭据(如果需要)
|
||||
context.Response.Headers.Add("Access-Control-Allow-Credentials", "true");
|
||||
|
||||
// 预检请求缓存时间(秒)
|
||||
context.Response.Headers.Add("Access-Control-Max-Age", "86400");
|
||||
|
||||
// 允许前端访问的响应头
|
||||
context.Response.Headers.Add("Access-Control-Expose-Headers", "Content-Length, Content-Type");
|
||||
|
||||
if (context.Request.HttpMethod == "OPTIONS")
|
||||
{
|
||||
|
||||
@@ -154,7 +154,13 @@ public class WebSocketManager
|
||||
{
|
||||
_connections.TryRemove(socket.ConnectionInfo.Id, out var conn);
|
||||
if (conn?.Uuid != null)
|
||||
{
|
||||
_uuidToSocketId.TryRemove(conn.Uuid, out _);
|
||||
// 清空订阅
|
||||
// conn.Uuid;
|
||||
|
||||
}
|
||||
|
||||
Log.Information($"客户端断开: {socket.ConnectionInfo.Id}");
|
||||
}
|
||||
|
||||
|
||||
@@ -31,4 +31,9 @@ public sealed class AppConfig
|
||||
/// WebSocket 连接地址
|
||||
/// </summary>
|
||||
public string? WebSocketPath { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 订阅字符串
|
||||
/// </summary>
|
||||
public static readonly string ApplicationInfoSubscribe= "ApplicationInfo";
|
||||
}
|
||||
@@ -2,7 +2,8 @@
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:Shell"
|
||||
StartupUri="MainWindow.xaml">
|
||||
>
|
||||
<!-- StartupUri="MainWindow.xaml" -->
|
||||
<Application.Resources>
|
||||
|
||||
</Application.Resources>
|
||||
|
||||
@@ -5,6 +5,7 @@ using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Web;
|
||||
using System.Windows;
|
||||
using System.Windows.Threading;
|
||||
using Application.Utils;
|
||||
@@ -130,6 +131,9 @@ public partial class App : System.Windows.Application
|
||||
// 启动WebSocket服务器
|
||||
WebSocketManager.Instance.Start(AppConfig.Instance.WebSocketPath);
|
||||
|
||||
// 开启 WebSocket 定时发送服务
|
||||
DispatchEngine.Instance.ApplicationInfoDistribute();
|
||||
|
||||
// 启动 http 服务
|
||||
Task.Run(() => { _ = StartHttpServer(); });
|
||||
}
|
||||
@@ -164,8 +168,35 @@ public partial class App : System.Windows.Application
|
||||
// 解析请求数据
|
||||
try
|
||||
{
|
||||
var request = context.GetRequestBody<string>();
|
||||
Log.Warning(request);
|
||||
// 直接使用已经读取的数据
|
||||
string requestData = context.RequestBody;
|
||||
|
||||
// 打印原始数据用于调试
|
||||
string? uuid = context.Request.Headers["uuid"];
|
||||
Log.Warning($"接收到的数据: {requestData}, 用户 Id: {uuid}");
|
||||
|
||||
var subscriptionInfo = JsonSerializer.Deserialize<SubscriptionInfo>(requestData);
|
||||
if (subscriptionInfo != null && !string.IsNullOrWhiteSpace(uuid))
|
||||
{
|
||||
if (AppConfig.ApplicationInfoSubscribe.Equals(subscriptionInfo.SubscriptionType))
|
||||
{
|
||||
DispatchEngine.Instance.AddApplicationInfo(uuid, new DispatchModel
|
||||
{
|
||||
Interval = subscriptionInfo.Interval
|
||||
});
|
||||
}
|
||||
|
||||
Log.Warning($"名称: {subscriptionInfo.SubscriptionType}");
|
||||
Log.Warning($"时间: {subscriptionInfo.Interval}");
|
||||
}
|
||||
|
||||
// 打印字节数据用于调试编码问题
|
||||
// var bytes = Encoding.UTF8.GetBytes(requestData);
|
||||
// Log.Warning($"字节数据: {BitConverter.ToString(bytes)}");
|
||||
//
|
||||
// // 如果需要解析 URL 编码的数据
|
||||
// var decodedData = HttpUtility.UrlDecode(requestData);
|
||||
// Log.Warning($"解码后的数据: {decodedData}");
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user