A:主页面调整为WebSocket连接设置

This commit is contained in:
jimmy committed 2025-06-01 00:48:31 +08:00
1 parent fe4c71f392
commit 61ab65ee81
8 files changed
+87 -39

No files matched your search

+21 -13
View File
@@ -4,8 +4,11 @@ using System.Net.Http;
using System.Windows;
using System.Windows.Threading;
using Application.Utils;
using Common;
using Common.Logger;
using Common.Net.WebSocketConn;
using Microsoft.Extensions.Logging;
using Serilog.Events;
namespace Shell;
@@ -18,11 +21,15 @@ public partial class App : System.Windows.Application
{
// 异常处理
ExceptionCatch();
// 初始化日志
var configModelLogging = AppConfig.Instance.ConfigModel.Logging;
Log.Initialize(configModelLogging);
base.OnStartup(e);
// ✅ 启动你的服务(如 WebSocket、HTTP、端口监听等)
StartMyServer();
}
/// <summary>
/// 异常捕捉
/// </summary>
@@ -33,7 +40,7 @@ public partial class App : System.Windows.Application
// 注册全局异常处理
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
}
/// <summary>
/// 处理异常
/// </summary>
@@ -48,6 +55,7 @@ public partial class App : System.Windows.Application
MessageBox.Show($"恭喜你发现了BUG,请联系管理员解决:{exception.Message}");
}
}
private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
Log.Error(e.Exception, "UI线程异常");
@@ -64,13 +72,14 @@ public partial class App : System.Windows.Application
// 让应用正常关闭而不是崩溃
}
}
private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
foreach (var ex in e.Exception.InnerExceptions)
{
Log.Error(e.Exception, "UI线程异常");
}
e.SetObserved(); // 标记异常已被观察,防止应用终止
}
@@ -78,7 +87,7 @@ public partial class App : System.Windows.Application
{
var exception = e.ExceptionObject as Exception;
Log.Error(exception, "非UI线程异常");
if (e.IsTerminating)
{
// 应用即将终止,进行紧急清理
@@ -86,6 +95,7 @@ public partial class App : System.Windows.Application
Log.Fatal("应用程序即将因未处理异常而终止");
}
}
private bool CanRecover(Exception exception)
{
return exception switch
@@ -104,17 +114,15 @@ public partial class App : System.Windows.Application
private void StartMyServer()
{
int port = SysHelper.GetAvailablePort();
Console.WriteLine($"服务启动在端口: {port}");
// 例如开启线程或任务监听 WS:
Task.Run(() =>
{
StartWebSocketServer(port);
});
}
AppConfig.Instance.WebSocketPort = port;
private void StartWebSocketServer(int port)
{
// 示例服务逻辑,如上文 WebSocket 实现
AppConfig.Instance.WebSocketPath = $"ws://127.0.0.1:{port}";
Log.Information4Ctx($"服务启动在端口: {AppConfig.Instance.WebSocketPort}");
Log.Information($"服务完整地址: {AppConfig.Instance.WebSocketPath}");
// TODO 2025年5月28日 22点25分 :开启 Http 网络本地服务,提供网页访问
// 启动WebSocket服务器
WebSocketManager.Instance.Start(AppConfig.Instance.WebSocketPath);
}
}
+2 -7
View File
@@ -8,13 +8,6 @@
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<Content Include="WinDivert.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="WinDivert64.sys" />
</ItemGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
@@ -25,8 +18,10 @@
<ItemGroup>
<ProjectReference Include="..\Application\Application.csproj" />
<ProjectReference Include="..\Common.Net\Common.Net.csproj" />
<ProjectReference Include="..\Data\Data.csproj" />
<ProjectReference Include="..\Infrastructure\Infrastructure.csproj" />
<ProjectReference Include="..\Utils.ETW\Utils.ETW.csproj" />
</ItemGroup>
<ItemGroup>
+48 -4
View File
@@ -2,7 +2,13 @@ using System.Windows.Controls;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Windows;
using Application.Utils;
using Common;
using Common.Logger;
using Common.Net;
using Common.Net.WebSocketConn;
using Infrastructure.utils;
using Utils.ETW;
using WinDivertNet.WinDivertWrapper;
namespace Shell.Views;
@@ -12,6 +18,9 @@ public partial class WebPanel : UserControl
public WebPanel()
{
InitializeComponent();
Class1.Main2();
InitWebView();
}
@@ -35,8 +44,8 @@ public partial class WebPanel : UserControl
// 反序列化为 C# 对象
var msg = JsonSerializer.Deserialize<JsMessage>(json);
Console.WriteLine(json);
Console.WriteLine(msg?.Channel);
Log.Information(json);
Log.Information(msg?.Channel);
switch (msg?.Channel)
{
@@ -49,7 +58,7 @@ public partial class WebPanel : UserControl
break;
case "GetProgramDiagnostics":
var programDiagnostics = SysInfoUtils.GetProgramDiagnostics();
Console.WriteLine(programDiagnostics);
Log.Information(programDiagnostics);
break;
case "InspectProcess":
SysInfoUtils.InspectProcess(23192);
@@ -60,19 +69,54 @@ public partial class WebPanel : UserControl
case "PacketSnifferStart":
PacketSniffer.Start();
break;
case "GetWebSocketPath":
WebSocketPath();
break;
case "CloseWebSocket":
CloseWebSocket();
break;
case "OpenWebSocket":
OpenWebSocket();
break;
default:
Console.WriteLine("没有任何函数被触发...");
Log.Information("没有任何函数被触发...");
break;
}
};
}
private void OpenWebSocket()
{
int port = SysHelper.GetAvailablePort();
AppConfig.Instance.WebSocketPort = port;
AppConfig.Instance.WebSocketPath = $"ws://127.0.0.1:{port}";
WebSocketManager.Instance.Start(AppConfig.Instance.WebSocketPath);
}
private void CloseWebSocket()
{
WebSocketManager.Instance.Stop();
}
private async void InitWebView1()
{
await webView.ExecuteScriptAsync("""
window.externalFunctions.__BRIDGE_LISTEN__('showMessage','来自 C# 的问候');
""");
}
private async void WebSocketPath()
{
var instanceWebSocketPath = AppConfig.Instance.WebSocketPath;
Log.Information($"instanceWebSocketPath -> :{instanceWebSocketPath}");
string result = await webView.ExecuteScriptAsync($"""
window.externalFunctions.__BRIDGE_LISTEN__('ListenWebSocketPath','{instanceWebSocketPath}');
""");
string result1 = await webView.ExecuteScriptAsync($"""
window.externalFunctions.__BRIDGE_LISTEN__('GetWebSocketPath','{instanceWebSocketPath}');
""");
}
}
public class JsMessage
Binary file not shown.
Binary file not shown.
+1 -1
View File
@@ -1,5 +1,5 @@
{
"AppSettings": {
"MaxConnection": 99
"MaxConnection": 321
}
}
+1 -1
View File
@@ -1,5 +1,5 @@
{
"AppSettings": {
"MaxConnection": 10
"MaxConnection": 123
}
}
+14 -13
View File
@@ -1,18 +1,19 @@
{
"AppSettings": {
"AppName": "MyApp",
"MaxConnection": 10
},
"Logging": {
"EnableConsole": true,
"EnableFile": true,
"LogPath": "logs",
"MaxFileSizeMB": 1,
"RetentionDays": 30,
"CompressDaysThreshold": 3,
"MinimumLevel": "Information",
"OutputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
"UseJsonFormat": false,
"CompressCheckIntervalHours": 24
"MaxConnection": 444,
"Version": "0.0.1",
"Logging": {
"EnableConsole": true,
"EnableFile": false,
"LogPath": "logs",
"MaxFileSizeMB": 1,
"RetentionDays": 30,
"CompressDaysThreshold": 3,
"MinimumLevel": "Information",
"OutputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] [{ThreadId}] {Message:lj}{NewLine}{Exception}",
"UseJsonFormat": false,
"CompressCheckIntervalHours": 24
}
}
}