/// <summary> /// 主函数 /// </summary> /// <param name="args"></param> public static void Main(string[] args) { ///加载插件 Manager.LoadPlugins(); Manager.InitPlugins(); Log.Write("完成插件加载!"); Log.Write("所有初始化后的插件列表!"); MessageRouteInfo.Start(); Log.Write("开启消息路由!"); foreach (var item in Manager.PluginsContainer.Values) { WriteObjectToJson(item); } ConsoleHelper.hideConsole(); //using (FileStream fileStream = File.Open("wav.wav", FileMode.Create)) //{ // byte[] datas = plugin.StartBySecondTime(2); // fileStream.Write(datas, 0, datas.Length); //} //plugin.Play("wav.wav"); //plugin.Play(plugin.StartBySecondTime(8)); //using (FileStream stream = File.Open(@"C:\Users\78633\Desktop\baiduai\test.wav", FileMode.Open)) //{ // byte[] datas = new byte[stream.Length]; // stream.Read(datas, 0, datas.Length); // plugin.Play(datas); //} //plugin.Play(@"C:\Users\78633\Desktop\baiduai\test.wav"); Console.ReadKey(); Manager.Dispose(); }
/// <summary> /// 发送给界面插件显示 /// </summary> /// <param name="msg"></param> /// <param name="id">发送者id</param> public void SendMsgToDispla(string id, string msg) { MessageRouteInfo.AddMessage(new TextMsgInfo() { Text = msg, ReciverId = PluginsURL.FaceInterfaceId, SendId = id, }); }
/// <summary> /// 写入到日志 /// </summary> /// <param name="logMsg"></param> /// <param name="id">发送者id</param> public void WriteLog(string id, string logMsg) { MessageRouteInfo.AddMessage(new TextMsgInfo() { Text = logMsg, ReciverId = PluginsURL.FaceInterfaceId, SendId = id, }); }
/// <summary> /// 发送数据 /// </summary> /// <typeparam name="T">发送数据对象</typeparam> /// <param name="mqInfo">目标通道信息</param> /// <param name="data">数据</param> public void Send <T>(MessageRouteInfo mqInfo, T data) { try { // 创建链接 var connectionFactory = new ConnectionFactory(); connectionFactory.HostName = mqInfo.HostName; connectionFactory.UserName = mqInfo.UserName; connectionFactory.Password = mqInfo.Password; connectionFactory.AutomaticRecoveryEnabled = true; using (var connection = connectionFactory.CreateConnection()) { using (var model = connection.CreateModel()) { // 注册交换机 model.ExchangeDeclare(mqInfo.ExchangeName, ExchangeType.Fanout, true); // 注册队列 model.QueueDeclare(mqInfo.QueueName, mqInfo.QueueDurable, false, false, null); // 对象转换成字节流 var json = JsonConvert.SerializeObject(data, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }); var messageBodyBytes = Encoding.UTF8.GetBytes(json); // 发布数据 model.BasicPublish(mqInfo.ExchangeName, mqInfo.RouteKey, null, messageBodyBytes); } } } catch (Exception ex) { var type = this.GetType(); // 写日志 var info = new NGPExceptionLog { BusinessMethod = string.Format("{0}.{1}", type.FullName, "Excute"), ExceptionContent = string.IsNullOrEmpty(ex.Message) ? (ex.InnerException != null ? ex.InnerException.Message : "Unknow Error") : ex.Message, Exception = ex }; Singleton <IEngine> .Instance.Resolve <ILogPublisher>().RegisterError(info); } }
/// <summary> /// 构造函数 /// </summary> public MainWindow() { InitializeComponent(); this.Closed += MainWindow_Closed; this.Closing += MainWindow_Closing; Log.ErroStringEvent += Log_ErroStringEvent; ///加载插件 Manager.LoadPlugins(); Manager.InitPlugins(); Log.Write("完成插件加载!"); Log.Write("所有初始化后的插件列表!"); MessageRouteInfo.Start(); Log.Write("开启消息路由!"); foreach (var item in Manager.PluginsContainer.Values) { Log.Write(Newtonsoft.Json.JsonConvert.SerializeObject(item)); } this.Loaded += MainWindow_Loaded; }
/// <summary> /// 注册监听 /// </summary> /// <typeparam name="T">监听数据类型</typeparam> /// <param name="mqInfo">监听通道对象</param> /// <param name="action">监听回调</param> public void Monitor <T>(MessageRouteInfo mqInfo, Action <T> action) { // 传输对象 var paramInfo = new MonitorMessageRouteInfo <T> { Action = action, MessageRoute = mqInfo }; // 后台线程 var thr = new Thread((obj) => { var param = obj as MonitorMessageRouteInfo <T>; var mqRouteInfo = param.MessageRoute; var factory = new ConnectionFactory(); factory.HostName = mqRouteInfo.HostName; factory.UserName = mqRouteInfo.UserName; factory.Password = mqRouteInfo.Password; factory.AutomaticRecoveryEnabled = true; using (var connection = factory.CreateConnection()) { using (var model = connection.CreateModel()) { // 注册交换机 model.ExchangeDeclare(mqRouteInfo.ExchangeName, ExchangeType.Fanout, true); // 注册队列 model.QueueDeclare(mqRouteInfo.QueueName, mqRouteInfo.QueueDurable, false, false, null); // 绑定队列 model.QueueBind(mqRouteInfo.QueueName, mqRouteInfo.ExchangeName, mqRouteInfo.RouteKey); var subscription = new Subscription(model, mqRouteInfo.QueueName, false); while (true) { try { BasicDeliverEventArgs basicDeliveryEventArgs = subscription.Next(); var json = Encoding.UTF8.GetString(basicDeliveryEventArgs.Body); var data = JsonConvert.DeserializeObject <T>(json, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All, }); param.Action(data); subscription.Ack(basicDeliveryEventArgs); } catch (Exception ex) { var type = GetType(); // 写日志 var info = new NGPExceptionLog { BusinessMethod = string.Format("{0}.{1}", type.FullName, "Excute"), ExceptionContent = string.IsNullOrEmpty(ex.Message) ? (ex.InnerException != null ? ex.InnerException.Message : "Unknow Error") : ex.Message, Exception = ex }; Singleton <IEngine> .Instance.Resolve <ILogPublisher>().RegisterError(info); } } } } }) { IsBackground = true }; thr.Start(paramInfo); }
/// <summary> /// 路由消息 /// </summary> /// <param name="msgInterface"></param> public void SendToMessage(IMsgInterface msgInterface) { MessageRouteInfo.AddMessage(msgInterface); }