public void InitMessageRegister() { string strRabbitMQconfig = _dbOption.RabbitMQconfig; MQHelper tMQHelper = MQHelperFactory.CreateBus(strRabbitMQconfig); #region 模拟第三方消息发送 //Task.Factory.StartNew(() => //{ // while (true) // { // chatHub.Clients.All.SendAsync("Notify", $" {DateTime.Now}:->{new Random().Next(1, 10000)}"); // Task.Delay(5000); // } //}); int i = 0; string strErrorMsg = ""; Task.Factory.StartNew(() => { while (true) { i++; var bodyMsg = new SystemMessage() { Content = $"定时第{i}条信息", DateTime = DateTime.Now.ToString(), Title = "四方博瑞安防平台信息", Type = "log".ObjectToJSON() }; var message = new RabbitMQMsg { Body = bodyMsg.ObjectToJSON(), Category = CategoryMessage.System, Dst = "预留字段", SendTime = DateTime.Now.ToString(), Src = "消息来源" }; tMQHelper.TopicPublish(message.Category.ToString() + ".xA", message, ref strErrorMsg); Task.Delay(5000); } ; }); #endregion #region 通过signar发送到客户端 tMQHelper.TopicSubscribe(Guid.NewGuid().ToString(), s => { Console.WriteLine("当前收到信息:" + s.Body.FromJson <SystemMessage>().Content); foreach (var connection in ChatHub.UserList) { if (_chatHub != null && connection.Key != null) { _chatHub.Clients.Client(connection.Key).SendAsync("Recv", "当前收到信息:" + s.Body.FromJson <SystemMessage>().Content); } } // chatHub.Clients.All.SendAsync("Notify", $" {DateTime.Now}:->{new Random().Next(1, 10000)}"); }, true, CategoryMessage.System.ToString() + ".*", CategoryMessage.Alarm.ToString() + ".*"); #endregion }
public static void Main(string[] args) { string input = ""; string strErrorMsg = ""; Console.WriteLine("请输入你要选择的发送端和接收端==1为发,2为收"); string type = Console.ReadLine(); string connStr = System.Configuration.ConfigurationManager.AppSettings["connStr"] ?? "host=192.168.137.112:5672;virtualHost=/;username=lxsh;password=123456"; Console.WriteLine(connStr); MQHelper tMQHelper = MQHelperFactory.CreateBus(connStr); if (type == "1") { Console.WriteLine("================================开始发送消息================================"); Console.WriteLine("Please enter a message. 'Quit' to quit."); int i = 0; //while ((input = Console.ReadLine()) != "Quit") while (true) { Thread.Sleep(1000); i++; var bodyMsg = new SystemMessage() { // Content = $"第{i}条信息:{ input}", Content = $"定时第{i}条信息", DateTime = DateTime.Now.ToString(), Title = "四方博瑞安防平台信息", Type = "log".ToJson() }; var message = new RabbitMQMsg { Body = bodyMsg.ToJson(), EventCategory = CategoryMessage.System, EventDst = "预留字段", EventSendTime = DateTime.Now.ToString(), SourceName = "消息来源" }; tMQHelper.TopicPublish(message.EventCategory.ToString() + ".xA", message, ref strErrorMsg); Console.WriteLine(message.Body); Console.WriteLine("================================发送消息完成================================"); } } else { tMQHelper.TopicSubscribe(Guid.NewGuid().ToString(), s => Console.WriteLine("当前收到信息:" + s.Body.FromJson <SystemMessage>().Content), true, CategoryMessage.System.ToString() + ".*", CategoryMessage.Alarm.ToString() + ".*"); Console.WriteLine("Please enter a message. 'Quit' to quit."); } }