修复控制台点击阻塞问题;
添加程序关闭指令入口;
This commit is contained in:
+44
-12
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BarrageGrab
|
||||
@@ -16,22 +17,24 @@ namespace BarrageGrab
|
||||
{
|
||||
SetConsoleCtrlHandler(cancelHandler, true);//捕获控制台关闭
|
||||
Console.Title = "抖音弹幕监听推送";
|
||||
DisableQuickEditMode();
|
||||
bool exited = false;
|
||||
server = new WsBarrageService();
|
||||
server.StartListen();
|
||||
|
||||
while (true)
|
||||
server.OnClose += (s, e) =>
|
||||
{
|
||||
var input = Console.ReadKey();
|
||||
switch (input.Key)
|
||||
{
|
||||
case ConsoleKey.Escape: goto end; break;
|
||||
}
|
||||
}
|
||||
//退出程序
|
||||
exited = true;
|
||||
};
|
||||
|
||||
end:
|
||||
server.Close();
|
||||
Console.WriteLine("服务器已正常关闭,按任意键结束...");
|
||||
Console.ReadKey();
|
||||
while (!exited)
|
||||
{
|
||||
Thread.Sleep(500);
|
||||
}
|
||||
Console.WriteLine("服务器已关闭...");
|
||||
|
||||
//退出程序,不显示 按任意键退出
|
||||
Environment.Exit(0);
|
||||
}
|
||||
|
||||
|
||||
@@ -54,5 +57,34 @@ namespace BarrageGrab
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
const int STD_INPUT_HANDLE = -10;
|
||||
const uint ENABLE_QUICK_EDIT_MODE = 0x0040;
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
internal static extern IntPtr GetStdHandle(int hConsoleHandle);
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
internal static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint mode);
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
internal static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint mode);
|
||||
|
||||
public static void EnableQuickEditMode()
|
||||
{
|
||||
IntPtr hStdin = GetStdHandle(STD_INPUT_HANDLE);
|
||||
uint mode;
|
||||
GetConsoleMode(hStdin, out mode);
|
||||
mode |= ENABLE_QUICK_EDIT_MODE;
|
||||
SetConsoleMode(hStdin, mode);
|
||||
}
|
||||
|
||||
public static void DisableQuickEditMode()
|
||||
{
|
||||
IntPtr hStdin = GetStdHandle(STD_INPUT_HANDLE);
|
||||
uint mode;
|
||||
GetConsoleMode(hStdin, out mode);
|
||||
mode &= ~ENABLE_QUICK_EDIT_MODE;
|
||||
SetConsoleMode(hStdin, mode);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,11 @@ namespace BarrageGrab
|
||||
Appsetting Appsetting = Appsetting.Current;
|
||||
bool debug = false;
|
||||
|
||||
/// <summary>
|
||||
/// 服务关闭后触发
|
||||
/// </summary>
|
||||
public event EventHandler OnClose;
|
||||
|
||||
public WsBarrageService()
|
||||
{
|
||||
#if DEBUG
|
||||
@@ -399,6 +404,22 @@ namespace BarrageGrab
|
||||
socketList[clientUrl].Socket = socket;
|
||||
}
|
||||
|
||||
//接收指令
|
||||
socket.OnMessage = (message) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var cmdPack = JsonConvert.DeserializeObject<Command>(message);
|
||||
if (cmdPack == null) return;
|
||||
|
||||
if (cmdPack.Cmd == CommandCode.Close)
|
||||
{
|
||||
this.Close();
|
||||
}
|
||||
}
|
||||
catch (Exception) {}
|
||||
};
|
||||
|
||||
socket.OnClose = () =>
|
||||
{
|
||||
socketList.Remove(clientUrl);
|
||||
@@ -440,7 +461,7 @@ namespace BarrageGrab
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 关闭服务器连接
|
||||
/// 关闭服务器连接,并关闭系统代理
|
||||
/// </summary>
|
||||
public void Close()
|
||||
{
|
||||
@@ -449,7 +470,7 @@ namespace BarrageGrab
|
||||
socketServer.Dispose();
|
||||
grab.Dispose();
|
||||
|
||||
console.WriteLine("服务器已关闭...");
|
||||
this.OnClose?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
class UserState
|
||||
|
||||
@@ -108,6 +108,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Appsetting.cs" />
|
||||
<Compile Include="JsonEntity\Command.cs" />
|
||||
<Compile Include="Proxy\EventArgs\HttpResponseEventArgs.cs" />
|
||||
<Compile Include="Proxy\EventArgs\WsMessageEventArgs.cs" />
|
||||
<Compile Include="Proxy\ISystemProxy.cs" />
|
||||
|
||||
Reference in New Issue
Block a user