public string PushHtmlMessage(HtmlMsg msg) { try { bool suc = PushServer.Instance().PushHtml(msg.RegName, msg.Content); if (suc) { #region 添加到已发送成功列表 using (var db = new MPModel()) { MsgSent model = new MsgSent() { Title = msg.Title, Content = msg.Content, RequestTime = msg.RequestTime, ExpriedTime = msg.ExpriedTime, MType = (int)MsgType.Html, RegName = msg.RegName, Phone = "", Address = "", SendTime = DateTime.Now }; db.MsgSents.Add(model); int rows = db.SaveChanges(); return(rows > 0 ? "SUCCESS" : "ERROR"); } #endregion } else { #region 添加到待发送成功列表 using (var db = new MPModel()) { MsgToBeSent model = new MsgToBeSent() { Title = msg.Title, Content = msg.Content, RequestTime = msg.RequestTime, ExpriedTime = msg.ExpriedTime, MType = (int)MsgType.Html, RegName = msg.RegName, Phone = "", Address = "" }; db.MsgToBeSents.Add(model); int rows = db.SaveChanges(); return(rows > 0 ? "SUCCESS" : "ERROR"); } #endregion } } catch (Exception) { throw; } }
private PushServer() { timer = new Timer((obj) => { MPModel db = new MPModel(); db.MsgToBeSents.ToList().ForEach(s => { //判断消息是否已经超时,超时suc=true bool suc = DateTime.Parse(s.ExpriedTime) < DateTime.Now; //如果超时,则不再推送此消息 if (!suc) { //暂定为每条消息只能指定一种推送方式,将来可以改成多种方式一起推送 if (s.MType == (int)MsgType.Html) { suc = PushHtml(s.RegName, s.Content); } else if (s.MType == (int)MsgType.Sms) { suc = PushSms(s.Phone, s.Content); } else if (s.MType == (int)MsgType.Email) { suc = PushEmail(s.Address, s.Content); } } if (suc) { db.MsgSents.Add(new MsgSent() { Title = s.Title, Content = s.Content, RequestTime = s.RequestTime, ExpriedTime = s.ExpriedTime, MType = s.MType, RegName = s.RegName, Phone = s.Phone, Address = s.Address, SendTime = DateTime.Now }); if (db.SaveChanges() > 0) { db.MsgToBeSents.Remove(s); db.SaveChanges(); } } }); }, null, 5000, 1000 * 60 * 5); //5分钟一次轮询 }
public void StartWebSocket(string port, LogLevel level = LogLevel.Debug) { try { FleckLog.Level = level; server = new WebSocketServer("ws://0.0.0.0:" + port); server.Start(socket => { socket.OnOpen = () => { Console.WriteLine("Open!" + socket.ConnectionInfo.Id.ToString()); tmpSocket.Add(new OnLineWebSocket(socket)); }; socket.OnClose = () => { Console.WriteLine("Close!" + socket.ConnectionInfo.Id.ToString()); tmpSocket.RemoveWhere(p => p.Socket == socket); }; socket.OnMessage = name => { Console.WriteLine($"Regist:{name},has connected {tmpSocket.Count}"); var sk = tmpSocket.Where(p => p.Socket == socket).ToList(); sk.ForEach(s => s.Name = name); #region 用户上线后,查询否有待推送消息 MPModel db = new MPModel(); var query = db.MsgToBeSents.Where(p => p.MType == (int)MsgType.Html && p.RegName == name).ToList(); query.ForEach(s => { bool suc = PushHtml(s.RegName, s.Content); if (suc) { db.MsgSents.Add(new MsgSent() { Title = s.Title, Content = s.Content, RequestTime = s.RequestTime, ExpriedTime = s.ExpriedTime, MType = s.MType, RegName = s.RegName, Phone = s.Phone, Address = s.Address, SendTime = DateTime.Now }); if (db.SaveChanges() > 0) { db.MsgToBeSents.Remove(s); db.SaveChanges(); } } }); #endregion }; }); } catch (Exception ex) { Console.WriteLine(ex); } }
static void Main(string[] args) { var db = new MPModel(); var count = db.MsgToBeSents.Count(); Console.WriteLine("待发送消息数量:" + count + " 条"); if (args.Length > 0) { try { ServiceBase[] serviceToRun = new ServiceBase[] { new WinService() }; ServiceBase.Run(serviceToRun); } catch (Exception ex) { File.AppendAllText(@"D:\Log.txt", "\nService Start Error:" + DateTime.Now.ToString() + "\n" + ex.Message); } } else { bool flag = true; do { string name = "MuMessagePusher"; string tip = "请选择你要执行的操作——0:控制台运行,1:自动部署服务,2:安装服务,3:卸载服务,4:查看服务状态,5:退出"; Console.WriteLine(tip); Console.WriteLine("————————————————————"); switch (Console.ReadKey().Key) { case ConsoleKey.NumPad0: case ConsoleKey.D0: StartListen(); break; case ConsoleKey.NumPad1: case ConsoleKey.D1: if (ServiceHelper.IsServiceExisted(name)) { ServiceHelper.ConfigService(name, false); } if (!ServiceHelper.IsServiceExisted(name)) { ServiceHelper.ConfigService(name, true); } ServiceHelper.StartService(name); break; case ConsoleKey.NumPad2: case ConsoleKey.D2: if (ServiceHelper.IsServiceExisted(name)) { Console.WriteLine("\n服务已存在......"); } else { ServiceHelper.ConfigService(name, true); } ServiceHelper.StartService(name); break; case ConsoleKey.NumPad3: case ConsoleKey.D3: if (ServiceHelper.IsServiceExisted(name)) { ServiceHelper.ConfigService(name, false); Console.WriteLine("\n卸载完成......"); } else { Console.WriteLine("\n服务不存在......"); } break; case ConsoleKey.NumPad4: case ConsoleKey.D4: if (ServiceHelper.IsServiceExisted(name)) { Console.WriteLine("\n服务状态:" + ServiceHelper.GetServiceStatus(name)); } else { Console.WriteLine("\n服务不存在......"); } break; case ConsoleKey.NumPad5: case ConsoleKey.D5: flag = false; break; } } while (flag); } }