添加ComPort 推送服务

This commit is contained in:
梅浩东
2024-03-29 17:34:14 +08:00
parent 13b1914db8
commit 3c310ebffc
15 changed files with 587 additions and 127 deletions
+90 -32
View File
@@ -6,6 +6,9 @@ using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.IO;
using Jint.Runtime;
using System.Windows.Forms;
namespace BarrageGrab
{
@@ -14,9 +17,14 @@ namespace BarrageGrab
static FormView mainForm = null;
static bool exited = false;
static bool formExited = false;
static WinApi.ControlCtrlDelegate controlCtr = ControlCtrlHandle;
static void Main(string[] args)
{
//JintTest();
//Console.WriteLine("结束");
//Console.ReadKey();
//return;
if (CheckAlreadyRunning())
{
Logger.PrintColor("已经有一个监听程序在运行,按任意键退出...");
@@ -25,19 +33,51 @@ namespace BarrageGrab
}
SetTitle("抖音弹幕监听推送");
WinApi.SetConsoleCtrlHandler(ControlCtrlDelegate, true);//捕获控制台关闭
try
{
Init();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "程序初始化错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
exited = true;
}
while (!exited)
{
Thread.Sleep(500);
}
if (!AppRuntime.WsServer.IsDisposed)
{
AppRuntime.WsServer.Dispose();
}
Logger.PrintColor("服务器已关闭...");
WinApi.SetConsoleCtrlHandler(controlCtr, false);//反注册捕获控制台关闭
}
private static void Init()
{
AppRuntime.Init();
WinApi.SetConsoleCtrlHandler(controlCtr, true);//捕获控制台关闭
WinApi.DisableQuickEditMode();//禁用控制台快速编辑模式
AppRuntime.DisplayConsole(!Appsetting.Current.HideConsole);//控制控制台可见
AppRuntime.WssService.Grab.Proxy.SetUpstreamProxy(Appsetting.Current.UpstreamProxy);//设置上游代理
AppRuntime.WssService.StartListen();//启动WS服务
AppRuntime.DisplayConsole(!AppSetting.Current.HideConsole);//控制控制台可见
AppRuntime.WsServer.Grab.Proxy.SetUpstreamProxy(AppSetting.Current.UpstreamProxy);//设置上游代理
AppRuntime.WsServer.OnClose += (s, e) =>
{
exited = true;
};
Logger.LogSucc($"{AppRuntime.WssService.ServerLocation} 弹幕服务已启动...");
SetTitle($"抖音弹幕监听推送 [{AppRuntime.WssService.ServerLocation}]");
AppRuntime.WssService.OnClose += (s, e) => exited = true;
//串口写入服务
if (!AppSetting.Current.ComPort.IsNullOrWhiteSpace())
{
AppRuntime.ComPortServer.OpenStart();
}
//显示窗体
if (Appsetting.Current.ShowWindow)
if (AppSetting.Current.ShowWindow)
{
var uiThread = new Thread(new ThreadStart(() =>
{
@@ -45,33 +85,16 @@ namespace BarrageGrab
//开启消息循环
System.Windows.Forms.Application.Run(mainForm);
formExited = true;
AppRuntime.WsServer.Dispose();//Close后自动释放资源
}));
uiThread.SetApartmentState(ApartmentState.STA);
uiThread.IsBackground = true;
uiThread.Start();
}
while (!exited)
{
if (formExited && !exited)
{
AppRuntime.WssService.Close();
}
Thread.Sleep(500);
}
Logger.PrintColor("服务器已关闭...");
//if (!mainForm.IsDisposed)
//{
// mainForm.Invoke(new Action(() =>
// {
// mainForm.Close();
// }));
//}
//退出程序,不显示 按任意键退出
//Environment.Exit(0);
AppRuntime.WsServer.StartListen();//启动WS以及代理服务
Logger.LogSucc($"{AppRuntime.WsServer.ServerLocation} 弹幕服务已启动...");
SetTitle($"抖音弹幕监听推送 [{AppRuntime.WsServer.ServerLocation}]");
}
//检测设置控制台标题
@@ -84,7 +107,7 @@ namespace BarrageGrab
}
//监听控制台消息事件
private static bool ControlCtrlDelegate(int CtrlType)
private static bool ControlCtrlHandle(int CtrlType)
{
switch (CtrlType)
{
@@ -94,7 +117,7 @@ namespace BarrageGrab
break;
case 2:
Logger.PrintColor("2工具被强制关闭");//按控制台关闭按钮关闭
AppRuntime.WssService.Close();
AppRuntime.WsServer.Dispose();
break;
}
return false;
@@ -111,5 +134,40 @@ namespace BarrageGrab
return !createdNew;
}
}
private static void JintTest()
{
var jsEng = JsEngine.CreateNewEngine();
var jsFile = File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "Scripts", "engine", "comPortFilter.js"), Encoding.UTF8);
jsEng.Execute(jsFile);
var result = jsEng.Invoke("onPackData", 1, new
{
Aporp = 142,
Bprop = "48777",
Cprop = new string[] { "123" },
Dprop = new Dictionary<string, string>()
{
{ "key1","value1" }
}
});
Console.WriteLine("返回类型:" + result.Type.ToString());
if (result.Type == Types.String)
{
Console.WriteLine("返回String: " + result.ToString());
}
if (result.Type == Types.Object)
{
var obj = result.ToObject();
if (obj is byte[])
{
Console.WriteLine("返回byte[]");
}
}
if (result.Type == Types.Symbol)
{
Console.WriteLine("返回Symbol: " + result.ToString());
}
}
}
}