Introduction
The example bellow shows how to use Eneter Messaging Framework to send text messages from Silverlight application to standalone desktop application with using Tcp connection.
(Full, not limited and for non-commercial usage free version of the framework can be downloaded from http://www.eneter.net/. The online help for developers can be found at http://www.eneter.net/OnlineHelp/EneterMessagingFramework/Index.html)
When we want to use the Tcp conection in Silverlight, we must be aware of following specifics:
- The Silverlight framework requires the policy server.
- The Silverlight framework allows only ports of range 4502 - 4532.
The Policy Server is a special service listening on the port 943. The service receives '<policy-file-request>' and responses the policy file that says who is allowed to communicate.
Silverlight automatically uses this service when creates the Tcp connection. It sends the request on the port 943 and expects the policy file. If the policy server is not there or the content of the policy file does not allow the communication, the Tcp connection is not created.
Eneter Messaging Framework wraps the low-level socket communication and provides convenient API for the Tcp communication. It also provides the Policy Server for you. Therefore the implementation is very simple. See the code bellow.
Desktop Application
The desktop application is responsible for starting the policy server and receiving messages.
The whole implementation is here:
using System; using Eneter.Messaging.EndPoints.StringMessages; using Eneter.Messaging.MessagingSystems.MessagingSystemBase; using Eneter.Messaging.MessagingSystems.TcpMessagingSystem; namespace Receiver { class Program { static void Main(string[] args) { // Start policy server enabling silverlight security to communicate via Tcp. TcpPolicyServer aPolicyServer = new TcpPolicyServer(); aPolicyServer.StartPolicyServer(); // Create receiver of string messages. IStringMessagesFactory aStringMessageReceiverFactory = new StringMessagesFactory(); IStringMessageReceiver aStringMessageReceiver = aStringMessageReceiverFactory.CreateStringMessageReceiver(); aStringMessageReceiver.MessageReceived += StringMessageReceived; // Create Tcp listrning channel. // Note: Silverlight supports only ports of range 4502 - 4532 IMessagingSystemFactory aTcpMessagingFactory = new TcpMessagingSystemFactory(); IInputChannel anInputChannel = aTcpMessagingFactory.CreateInputChannel("127.0.0.1:4502"); // Attach the input channel to the string message receiver and start listening. Console.WriteLine("Receiver is listening."); aStringMessageReceiver.AttachInputChannel(anInputChannel); } static void StringMessageReceived(object sender, StringMessageEventArgs e) { // Process incoming message. Console.WriteLine(e.Message); } } }
Silverlight Application
The Silverlight application is responsible for sending text messages. (The communication with the policy server is invoked automatically by Silverlight before the connection is established.)
The whole implementation is very simple:
using System.Windows; using System.Windows.Controls; using Eneter.Messaging.EndPoints.StringMessages; using Eneter.Messaging.MessagingSystems.MessagingSystemBase; using Eneter.Messaging.MessagingSystems.TcpMessagingSystem; namespace SilverlightApplication { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); // Create sender of string messages. IStringMessagesFactory aStringMessageReceiverFactory = new StringMessagesFactory(); myStringMessageSender = aStringMessageReceiverFactory.CreateStringMessageSender(); // Create output channel sending via Tcp. // Note1: Silverlight supports only ports of range 4502 - 4532 IMessagingSystemFactory aTcpMessagingFactory = new TcpMessagingSystemFactory(); IOutputChannel anOutputChannel = aTcpMessagingFactory.CreateOutputChannel("127.0.0.1:4502"); // Attach the output channel to the string message sender // to be able to send messages via Tcp. myStringMessageSender.AttachOutputChannel(anOutputChannel); } // Send message when the button is clicked. private void button1_Click(object sender, RoutedEventArgs e) { string aMessage = textBox1.Text; myStringMessageSender.SendMessage(aMessage); } private IStringMessageSender myStringMessageSender; } }
I hope you found the article useful and if you have any comments or questions feel free to ask.
No comments:
Post a Comment