/// <summary> /// 发送消息 /// </summary> /// <param name="MQContent">消息内容</param> /// <param name="ServerName">接收端程序名,必须在服务端注册</param> public void SendMQ(string MQContent) { client.Connect(); IOutgoingMessage message = client.CreateMessage(); message.MessageData = Encoding.UTF8.GetBytes(MQContent); message.Send(); client.Disconnect(); }
/// <summary> /// invio messaggio di plctag changed value a tutti i susbscibers /// </summary> /// <param name="sender"></param> /// <param name="e">dati plctag</param> private void plc_TagChangedValue(object sender, TagChangedValueEventArgs e) { var tag = new PLCTag(); /* cerco il subscriber associato al tag */ foreach (var subscriber in _Subs.ToList()) { /* lista di subscrictions del subscriber */ var list = subscriber.Value; if (list.Contains(new Subscription(e.PLCName, e.Tag.ItemName))) { // trovato il subscriber, invio messaggio di tag changed tag.PLCName = e.PLCName; tag.Address = e.Tag.ItemName; tag.Value = e.Tag.ItemValue; //Create a DotNetMQ Message to send var message = mdsClient.CreateMessage(); //Set destination application name message.DestinationApplicationName = subscriber.Key; //Create a message var MsgData = new PLCTagData { MsgCode = MsgCodes.PLCTagChanged, Tag = tag }; //Set message data message.MessageData = GeneralHelper.SerializeObject(MsgData); message.TransmitRule = MessageTransmitRules.NonPersistent; try { //Send message message.Send(); Logger.InfoFormat("{1}/{2}-{3} : Inviato a {0}", message.DestinationApplicationName, tag.PLCName, tag.Address, tag.Value.ToString()); } catch (Exception exc) { // non sono riuscito a inviare il messaggio Logger.WarnFormat("Messaggio non inviato - {0}", exc.Message); } } } }
static void Main() { //Create MDSClient object to connect to DotNetMQ //Name of this application: Application1 var mdsClient = new MDSClient("Application1"); //Connect to DotNetMQ server mdsClient.Connect(); Console.WriteLine("Write a text and press enter to send to Application2. Write 'exit' to stop application."); while (true) { //Get a message from user var messageText = Console.ReadLine(); if (string.IsNullOrEmpty(messageText) || messageText == "exit") { break; } //Create a DotNetMQ Message to send to Application2 var message = mdsClient.CreateMessage(); //Set destination application name message.DestinationApplicationName = "Application2"; //message.DestinationServerName = "this_server2"; //Set message data message.MessageData = Encoding.UTF8.GetBytes(messageText); //Send message message.Send(); } //Disconnect from DotNetMQ server mdsClient.Disconnect(); }
static void Send(DateTime time, int structid, string filepath, int sensorid) { var tsk = new Task(() => { var message = mdsClient.CreateMessage(); message.DestinationApplicationName = "DataCalc"; message.MessageData = GeneralHelper.SerializeObject(new RequestDataCalcMessage() { StructId = structid, DateTime = time, FilePath = filepath, SensorID = sensorid }); message.TransmitRule = MDS.Communication.Messages.MessageTransmitRules.NonPersistent; message.Send(); Console.WriteLine("发送消息:结构物编号{0},采集时间:{1}", structid, DateTime.Now); }); tsk.Start(); tsk.ContinueWith(t => { if (t.Exception != null) { Console.WriteLine(t.Exception.Message); } else { Console.WriteLine("done"); } }); }
public bool Connection(string sender, string destination) { bool RetValue = true; //Create a DotNetMQ Message to send var message = MDSClientInstance.CreateMessage(); //Set destination application name message.DestinationApplicationName = destination; //Create a message var MsgData = new PLCConnectionData { MsgCode = MsgCodes.ConnectPLC, PLCName = this.Name, IpAddress = this.IPAddress, Rack = this.Rack, Slot = this.Slot, Delay = this.Delay }; //Set message data message.MessageData = MDS.GeneralHelper.SerializeObject(MsgData); // message.MessageData = Encoding.UTF8.GetBytes(messageText); message.TransmitRule = MessageTransmitRules.NonPersistent; try { //Send message message.Send(); Logger.InfoFormat("Inviato Messaggio a {0}", message.DestinationApplicationName); } catch (Exception exc) { // non sono riuscito a inviare il messaggio Logger.WarnFormat("Messaggio non inviato : {0}", exc.Message); RetValue = false; } return(RetValue); }
private static void SendMessage(MDSClient mdsClient, string DestinationApplicationName, string newMessage, MDS.Communication.Messages.MessageTransmitRules rules) { //Create a DotNetMQ Message to send to Application2 var message = mdsClient.CreateMessage(); //Set destination application name message.DestinationApplicationName = DestinationApplicationName; message.TransmitRule = rules; //Set message data message.MessageData = Encoding.UTF8.GetBytes(newMessage); //Send message message.Send(); }
public void SendWarningProcessMessage(RequestDataCalcMessage request) { if (!isConnect) { this.Start(); } var reqWarnMsg = mdsClient.CreateMessage(); reqWarnMsg.MessageData = GeneralHelper.SerializeObject(request); reqWarnMsg.TransmitRule = MDS.Communication.Messages.MessageTransmitRules.NonPersistent; reqWarnMsg.DestinationApplicationName = WarningAppName; reqWarnMsg.Send(); log.Info("成功发送告警"); }
public void Send(object message) { var outgoingMessage = client.CreateMessage(); outgoingMessage.MessageData = GeneralHelper.SerializeObject(message); try { outgoingMessage.Send(); } finally { client.Connect(); outgoingMessage.Send(); } }
/// <summary> /// /// </summary> /// <param name="args"></param> static void Main(string[] args) { while (true) { //实例化客户端 var mdsClient = new MDSClient("SyncService", "127.0.0.1", 10905); //链接SERVER mdsClient.Connect(); var message = mdsClient.CreateMessage(); message.MessageData = System.Text.Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()); message.Send(); Console.WriteLine("00"); mdsClient.Disconnect(); System.Threading.Thread.Sleep(1000); } }
/// <summary> /// Connette a plcserver /// </summary> /// <returns></returns> private bool PLCServerConnect() { bool RetValue = true; //Create a DotNetMQ Message to send var message = mdsClient.CreateMessage(); //Set destination application name message.DestinationApplicationName = PLCServerApplicationName; //Create a message var MsgData = new MsgData { MsgCode = MsgCodes.ConnectSubscriber, }; //Set message data message.MessageData = GeneralHelper.SerializeObject(MsgData); // message.MessageData = Encoding.UTF8.GetBytes(messageText); message.TransmitRule = MessageTransmitRules.NonPersistent; try { //Send message message.Send(); } catch { // non sono riuscito a inviare il messaggio Logger.InfoFormat("Messaggio non inviato"); RetValue = false; } if (RetValue) { Logger.InfoFormat("Connesso"); } return(RetValue); }
static void Main(string[] args) { Console.WriteLine("Press enter to query a stock status"); Console.ReadLine(); //Connect to DotNetMQ var mdsClient = new MDSClient("StockClient"); mdsClient.MessageReceived += mdsClient_MessageReceived; mdsClient.Connect(); //Create a stock request message var stockQueryMessage = new StockQueryMessage { StockCode = "S01" }; //Create a MDS message var requestMessage = mdsClient.CreateMessage(); requestMessage.DestinationApplicationName = "StockServer"; requestMessage.TransmitRule = MessageTransmitRules.NonPersistent; requestMessage.MessageData = GeneralHelper.SerializeObject(stockQueryMessage); //Send message and get response var responseMessage = requestMessage.SendAndGetResponse(); //Get stock query result message from response message var stockResult = (StockQueryResultMessage)GeneralHelper.DeserializeObject(responseMessage.MessageData); //Write stock query result Console.WriteLine("StockCode = " + stockResult.StockCode); Console.WriteLine("ReservedStockCount = " + stockResult.ReservedStockCount); Console.WriteLine("TotalStockCount = " + stockResult.TotalStockCount); //Acknowledge received message responseMessage.Acknowledge(); Console.ReadLine(); //Disconnect from DotNetMQ server. mdsClient.Disconnect(); }
private static void Main(string[] args) { Ts(); return; var mdsClient = new MDSClient("ET"); mdsClient.Connect(); bool isAuto = false; int autoCount = 100; Console.WriteLine("输入(auto),自动执行100次"); isAuto = Console.ReadLine() == "auto"; while (true) { if (!isAuto) { string str = Console.ReadLine(); if (str == "end") { break; } } else { if (autoCount-- <= 0) { break; } } Task[] ts = new Task[100]; for (int i = 0; i < 100; i++) { ts[i] = new Task(() => { Random ran = new Random(); int number = ran.Next(0, 10); var message = mdsClient.CreateMessage(); message.DestinationApplicationName = "DataCalc"; message.MessageData = GeneralHelper.SerializeObject(new RequestDataCalcMessage() { StructId = number, DateTime = DateTime.Now }); message.TransmitRule = MDS.Communication.Messages.MessageTransmitRules.NonPersistent; message.Send(); Console.WriteLine("发送消息:结构物编号{0},采集时间:{1}", number, DateTime.Now); }); } foreach (Task task in ts) { task.Start(); } Thread.Sleep(200); } mdsClient.Disconnect(); Console.ReadLine(); }