Implements the factory to create duplex strongly typed message sender and receiver.
Namespace: Eneter.Messaging.EndPoints.TypedMessagesAssembly: Eneter.Messaging.Framework (in Eneter.Messaging.Framework.dll) Version: 5.0.0.0 (5.0.0.0)
Syntax
| C# |
|---|
public class DuplexTypedMessagesFactory : IDuplexTypedMessagesFactory |
| Visual Basic |
|---|
Public Class DuplexTypedMessagesFactory _ Implements IDuplexTypedMessagesFactory |
| Visual C++ |
|---|
public ref class DuplexTypedMessagesFactory : IDuplexTypedMessagesFactory |
Remarks
Examples
Simple service listening to request messages of type 'RequestMessage' and responding the response message of type 'ResponseMessage'.
using System; using Eneter.Messaging.EndPoints.TypedMessages; using Eneter.Messaging.MessagingSystems.MessagingSystemBase; using Eneter.Messaging.MessagingSystems.WebSocketMessagingSystem; namespace CalculatorService { // Request message. public class RequestMessage { public int Number1 { get; set; } public int Number2 { get; set; } } // Response message. public class ResponseMessage { public int Result { get; set; } } class Program { static void Main(string[] args) { // Create message receiver. IDuplexTypedMessagesFactory aReceiverFactory = new DuplexTypedMessagesFactory(); IDuplexTypedMessageReceiver<ResponseMessage, RequestMessage> aReceiver = aReceiverFactory.CreateDuplexTypedMessageReceiver<ResponseMessage, RequestMessage>(); // Subscribe to process request messages. aReceiver.MessageReceived += OnMessageReceived; // Use WebSocket for the communication. // Note: You can also other messagings. E.g. TcpMessagingSystemFactory IMessagingSystemFactory aMessaging = new WebSocketMessagingSystemFactory(); IDuplexInputChannel anInputChannel = aMessaging.CreateDuplexInputChannel("ws://192.168.1.102:8099/aaa/"); // Attach the input channel to the receiver and start listening. aReceiver.AttachDuplexInputChannel(anInputChannel); Console.WriteLine("The calculator service is running. Press ENTER to stop."); Console.ReadLine(); // Detach the input channel to stop listening. aReceiver.DetachDuplexInputChannel(); } private static void OnMessageReceived(object sender, TypedRequestReceivedEventArgs<RequestMessage> e) { // Calculate numbers. ResponseMessage aResponseMessage = new ResponseMessage(); aResponseMessage.Result = e.RequestMessage.Number1 + e.RequestMessage.Number2; Console.WriteLine("{0} + {1} = {2}", e.RequestMessage.Number1, e.RequestMessage.Number2, aResponseMessage.Result); // Send back the response message. var aReceiver = (IDuplexTypedMessageReceiver<ResponseMessage, RequestMessage>)sender; aReceiver.SendResponseMessage(e.ResponseReceiverId, aResponseMessage); } } } | |
Examples
Simple client sending request messages of type 'RequestMessage' and receiving responses of type 'ResponseMessage'.
The client is synchronous. It sends the request message and waits for the response.
using System; using System.Windows.Forms; using Eneter.Messaging.EndPoints.TypedMessages; using Eneter.Messaging.MessagingSystems.MessagingSystemBase; using Eneter.Messaging.MessagingSystems.WebSocketMessagingSystem; namespace CalculatorClientSync { public partial class Form1 : Form { // Request message. public class RequestMessage { public int Number1 { get; set; } public int Number2 { get; set; } } // Response message. public class ResponseMessage { public int Result { get; set; } } private ISyncDuplexTypedMessageSender<ResponseMessage, RequestMessage> mySender; public Form1() { InitializeComponent(); OpenConnection(); } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { CloseConnection(); } private void OpenConnection() { // Create the message sender. IDuplexTypedMessagesFactory aSenderFactory = new DuplexTypedMessagesFactory(); mySender = aSenderFactory.CreateSyncDuplexTypedMessageSender<ResponseMessage, RequestMessage>(); // Use Websocket for the communication. // If you want to use TCP then use TcpMessagingSystemFactory(). IMessagingSystemFactory aMessaging = new WebSocketMessagingSystemFactory(); IDuplexOutputChannel anOutputChannel = aMessaging.CreateDuplexOutputChannel("ws://192.168.1.102:8099/aaa/"); // Attach the output channel and be able to send messages // and receive response messages. mySender.AttachDuplexOutputChannel(anOutputChannel); } private void CloseConnection() { // Detach input channel and stop listening to response messages. mySender.DetachDuplexOutputChannel(); } private void CalculateBtn_Click(object sender, EventArgs e) { // Create the request message. RequestMessage aRequest = new RequestMessage(); aRequest.Number1 = int.Parse(Number1TextBox.Text); aRequest.Number2 = int.Parse(Number2TextBox.Text); // Send request to the service to calculate 2 numbers. ResponseMessage aResponse = mySender.SendRequestMessage(aRequest); // Display the result. ResultTextBox.Text = aResponse.Result.ToString(); } } } | |
Examples
Simple client sending request messages of type 'RequestMessage' and receiving responses of type 'ResponseMessage'.
The client receives the response asynchronously via the event.
using System; using System.Windows.Forms; using Eneter.Messaging.EndPoints.TypedMessages; using Eneter.Messaging.MessagingSystems.MessagingSystemBase; using Eneter.Messaging.MessagingSystems.WebSocketMessagingSystem; namespace CalculatorClient { public partial class Form1 : Form { // Request message. public class RequestMessage { public int Number1 { get; set; } public int Number2 { get; set; } } // Response message. public class ResponseMessage { public int Result { get; set; } } private IDuplexTypedMessageSender<ResponseMessage, RequestMessage> mySender; public Form1() { InitializeComponent(); OpenConnection(); } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { CloseConnection(); } private void OpenConnection() { // Create the message sender. IDuplexTypedMessagesFactory aSenderFactory = new DuplexTypedMessagesFactory(); mySender = aSenderFactory.CreateDuplexTypedMessageSender<ResponseMessage, RequestMessage>(); // Subscribe to receive response messages. mySender.ResponseReceived += OnResponseReceived; // Use Websocket for the communication. // If you want to use TCP then use TcpMessagingSystemFactory(). IMessagingSystemFactory aMessaging = new WebSocketMessagingSystemFactory(); IDuplexOutputChannel anOutputChannel = aMessaging.CreateDuplexOutputChannel("ws://192.168.1.102:8099/aaa/"); // Attach the output channel and be able to send messages // and receive response messages. mySender.AttachDuplexOutputChannel(anOutputChannel); } private void CloseConnection() { // Detach input channel and stop listening to response messages. mySender.DetachDuplexOutputChannel(); } private void CalculateBtn_Click(object sender, EventArgs e) { // Create the request message. RequestMessage aRequest = new RequestMessage(); aRequest.Number1 = int.Parse(Number1TextBox.Text); aRequest.Number2 = int.Parse(Number2TextBox.Text); // Send request to the service to calculate 2 numbers. mySender.SendRequestMessage(aRequest); } private void OnResponseReceived(object sender, TypedResponseReceivedEventArgs<ResponseMessage> e) { // Display the result using the UI thread. UI(() => ResultTextBox.Text = e.ResponseMessage.Result.ToString()); } // Helper method to invoke a delegate in the UI thread. // Note: You can manipulate UI controls only from the UI tread. private void UI(Action action) { if (InvokeRequired) { Invoke(action); } else { action(); } } } } | |