public async Task RunServerAsync() { OnSubMessage?.Invoke("服务启动中......", "重要"); //第一步:创建ServerBootstrap实例 var bootstrap = new ServerBootstrap(); //第二步:绑定事件组 IEventLoopGroup mainGroup = new MultithreadEventLoopGroup(1); //主工作线程组 IEventLoopGroup workGroup = new MultithreadEventLoopGroup(); //工作线程组 bootstrap.Group(mainGroup, workGroup); //第三步:绑定服务端的通道 bootstrap.Channel <TcpServerSocketChannel>();// 设置通道模式为TcpSocket //第四步:配置处理器 bootstrap.Option(ChannelOption.SoBacklog, 8192); bootstrap.ChildHandler(new ActionChannelInitializer <IChannel>(channel => { IChannelPipeline pipeline = channel.Pipeline; //pipeline.AddLast(new HttpServerCodec()); //pipeline.AddLast(new HttpObjectAggregator(65536)); pipeline.AddLast(new TcpServerHandler()); })); //第五步:配置主机和端口号 string hostName = Dns.GetHostName(); IPAddress[] ipAddresses = Dns.GetHostAddresses(hostName); ipAddresses = ipAddresses.Where(m => m.ToString().IsIPv4()).ToArray(); var host = ConfigHelper.Configuration["ServerConfig:Host"]; bool trueAddress = ipAddresses.Any(m => host.Equals(m.ToString())); IPAddress iPAddress = trueAddress ? IPAddress.Parse(host) : ipAddresses[0]; var port = ConfigHelper.Configuration["ServerConfig:Port"]; IChannel bootstrapChannel = await bootstrap.BindAsync(iPAddress, int.Parse(port)); OnSubMessage?.Invoke("服务启动成功", "重要"); OnMessage?.Invoke($"已监听http://{iPAddress}:{int.Parse(port)}"); //第六步:停止服务 OnMessage?.Invoke("输入Stop停止服务"); string inputKey = string.Empty; while (!string.Equals(inputKey, "Stop", StringComparison.Ordinal)) { inputKey = OnGetCommand?.Invoke(); if (!string.Equals(inputKey, "Stop", StringComparison.Ordinal)) { OnException?.Invoke(new Exception("未识别命令请重新输入")); } } OnSubMessage?.Invoke("正在停止服务......", "重要"); await bootstrapChannel.CloseAsync(); OnSubMessage?.Invoke("服务已停止", "重要"); }
/// <summary> /// 等待服务停止 /// </summary> private void WaitServerStop() { OnMessage?.Invoke("输入Stop停止服务"); string inputKey = string.Empty; while (!string.Equals(inputKey, "Stop", StringComparison.Ordinal)) { inputKey = OnGetCommand?.Invoke(); if (!string.Equals(inputKey, "Stop", StringComparison.Ordinal)) { OnException?.Invoke(new Exception("未识别命令请重新输入")); } } }
private async Task TestRun() { await Task.Run(() => { OnSubMessage?.Invoke("服务启动成功", "重要"); OnMessage?.Invoke("输入Stop停止服务"); string inputKey = string.Empty; while (!string.Equals(inputKey, "Stop", StringComparison.Ordinal)) { inputKey = OnGetCommand?.Invoke(); if (!string.Equals(inputKey, "Stop", StringComparison.Ordinal)) { OnException?.Invoke(new Exception("未识别命令请重新输入")); } } OnSubMessage?.Invoke("正在停止服务", "重要"); OnSubMessage?.Invoke("服务已停止", "重要"); }); }
public void ThreadProc() { DateTime beginTime = DateTime.Now; //发送总数 int sendCount = 0; string objectText = SendParam.SendObject == 1 ? "好友" : (SendParam.SendObject == 2 ? "群" : (SendParam.SendObject == 4 ? "群成员" : "")); int friendCount = 0; try { int?commandExce = 1; commandExce = OnGetCommand?.Invoke(); //如果收到停止指令,则直接退出 if (commandExce == 4) { throw new Exception("收到停止指令,任务自动取消!"); } //获取好友或者群数据 DataTable dataTable = null; if (SendParam.SendObject == 1) { OnOutStatus?.Invoke(string.Format("准备获取机器人[{0}]的好友数据!", new object[] { SendParam.RobotQQ })); dataTable = DataHelper.GetFriends(SendParam.RobotQQ); OnOutStatus?.Invoke(string.Format("获取机器人[{0}]的好友数据成功!", new object[] { SendParam.RobotQQ })); } else if (SendParam.SendObject == 2) { OnOutStatus?.Invoke(string.Format("准备获取机器人[{0}]的群组数据!", new object[] { SendParam.RobotQQ })); dataTable = DataHelper.GetGroups(SendParam.RobotQQ); OnOutStatus?.Invoke(string.Format("获取机器人[{0}]的群组数据成功!", new object[] { SendParam.RobotQQ })); } else if (SendParam.SendObject == 4) { } if (dataTable == null) { OnOutStatus?.Invoke(string.Format("机器人[{0}]获取数据失败!", new object[] { SendParam.RobotQQ })); return; } //待输出界面的消息列表 List <string> infos = new List <string>(); friendCount = dataTable.Rows.Count; OnOutStatus?.Invoke(string.Format("机器人[{0}]开始发送数据,{1}总数:{2}", new object[] { SendParam.RobotQQ, objectText, friendCount })); //从第二行开始遍历 for (int i = 0; i <= friendCount; i++) { bool outCommandInfo = true; //获取命令,判断当前是否执行 labelCommand: int?command = 1; command = OnGetCommand?.Invoke(); switch (command) { case 1: break; case 2: if (outCommandInfo) { OnOutStatus?.Invoke(string.Format("机器人[{0}]收到暂停指令,当前任务已暂停!", new object[] { SendParam.RobotQQ })); } outCommandInfo = false; break; case 3: OnOutStatus?.Invoke(string.Format("机器人[{0}]收到恢复指令,当前任务已恢复!", new object[] { SendParam.RobotQQ })); break; case 4: OnOutStatus?.Invoke(string.Format("机器人[{0}]收到停止指令,当前任务已取消!", new object[] { SendParam.RobotQQ })); break; } //如果是暂停指令,则不停检测是否收到恢复指令或者停止指令 if (command == 2) { Thread.Sleep(1000); goto labelCommand; } //如果是停止执行,则停止当前处理 else if (command == 4) { break; } string uin = Convert.ToString(dataTable.Rows[i]["uin"]); string name = Convert.ToString(dataTable.Rows[i]["name"]); //发送消息 SendMsg(uin, uin, name, sendCount, objectText, SendParam); sendCount++; //回传当前状态 OnOutStatus?.Invoke(string.Format("机器人[{0}]成功给第{1}个{2}[{3}-{4}]发送消息!", new object[] { SendParam.RobotQQ, sendCount, objectText, uin, name })); //如果当前已发送完,则停止 if (sendCount == dataTable.Rows.Count) { break; } //当到达每组发送总数时进行暂停 if (sendCount % SendParam.GroupNum == 0) { int groupSleep = PublicUtils.GetRandom(SendParam.GroupSleepDown, SendParam.GroupSleepUp + 1); OnOutStatus?.Invoke(string.Format("机器人[{0}]进入组休眠{1}秒!", new object[] { SendParam.RobotQQ, groupSleep })); Thread.Sleep(groupSleep * 1000); } else { //算出每个休眠时间 int interval = PublicUtils.GetRandom(SendParam.FriendIntervalDown, SendParam.FriendIntervalUp + 1); OnOutStatus?.Invoke(string.Format("机器人[{0}]进入好友休眠{1}秒!", new object[] { SendParam.RobotQQ, interval })); Thread.Sleep(interval * 1000); } } } catch (Exception ex) { OnOutStatus?.Invoke(string.Format("机器人[{0}]执行任务出错了,任务自动取消!成功发送{1},未发送{2},错误详情:{3}", new object[] { SendParam.RobotQQ, sendCount, friendCount - sendCount, ex.Message })); } finally { OnTaskCompleted?.Invoke(string.Format("机器人[{0}]任务已完成!用时:{1:F}秒,共发送{2}个{3}", new object[] { SendParam.RobotQQ, (DateTime.Now - beginTime).TotalSeconds, sendCount, objectText })); } }