Skip to content

Commit

Permalink
修复控制台点击阻塞问题;
Browse files Browse the repository at this point in the history
添加程序关闭指令入口;
  • Loading branch information
ape-byte committed Jun 19, 2023
1 parent 6f5ae16 commit e3a73c0
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 17 deletions.
60 changes: 46 additions & 14 deletions BarrageGrab/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace BarrageGrab
Expand All @@ -14,24 +15,26 @@ public class Program
static WsBarrageService server = null;
static void Main(string[] args)
{
SetConsoleCtrlHandler(cancelHandler, true);//捕获控制台关闭
Console.Title = "抖音弹幕监听推送";
SetConsoleCtrlHandler(cancelHandler, true);//捕获控制台关闭
Console.Title = "抖音弹幕监听推送";
DisableQuickEditMode();
bool exited = false;
server = new WsBarrageService();
server.StartListen();

while (true)
server.StartListen();
server.OnClose += (s, e) =>
{
var input = Console.ReadKey();
switch (input.Key)
{
case ConsoleKey.Escape: goto end; break;
}
//退出程序
exited = true;
};

while (!exited)
{
Thread.Sleep(500);
}
Console.WriteLine("服务器已关闭...");

end:
server.Close();
Console.WriteLine("服务器已正常关闭,按任意键结束...");
Console.ReadKey();
//退出程序,不显示 按任意键退出
Environment.Exit(0);
}


Expand All @@ -54,5 +57,34 @@ public static bool HandlerRoutine(int CtrlType)
}
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);
}

}
}
25 changes: 23 additions & 2 deletions BarrageGrab/WsBarrageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ internal class WsBarrageService
WssBarrageGrab grab = new WssBarrageGrab();
Appsetting Appsetting = Appsetting.Current;
bool debug = false;

/// <summary>
/// 服务关闭后触发
/// </summary>
public event EventHandler OnClose;

public WsBarrageService()
{
Expand Down Expand Up @@ -399,6 +404,22 @@ private void Listen(IWebSocketConnection socket)
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);
Expand Down Expand Up @@ -440,7 +461,7 @@ public void StartListen()
}

/// <summary>
/// 关闭服务器连接
/// 关闭服务器连接,并关闭系统代理
/// </summary>
public void Close()
{
Expand All @@ -449,7 +470,7 @@ public void Close()
socketServer.Dispose();
grab.Dispose();

console.WriteLine("服务器已关闭...");
this.OnClose?.Invoke(this, EventArgs.Empty);
}

class UserState
Expand Down
2 changes: 1 addition & 1 deletion BarrageGrab/WssBarrageGrab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void Start()

public void Dispose()
{
proxy.Dispose();
proxy.Dispose();
}

//域名过滤器
Expand Down
1 change: 1 addition & 0 deletions BarrageGrab/WssBarrageService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down

0 comments on commit e3a73c0

Please sign in to comment.