Monday, March 30, 2009

Silverlight 3 Beta Local Messaging among Silverlight islands

If you have read my previous post about inter silverlight communication design you might seen how difficult it is to accomplish communication between 2 silverlight applications hosted in same page.But in Silverlight 3 Microsoft has introduced a new technique to do the communication.It is nothing but 2 classes.LocalMessageSender & LocalMessageReceiver.Now it is very easy to accomplish communication between islands of Silverlight contents.

Implementing Local messaging between silverlight applications
As like in other communication scenarios here also there should be a source and destination which sends and receives respectively.LocalMessageSender class sends a string message and LocalMessageReceiver receives the same.At the time of sending we should specify the name of the receiver.Also at the time of listening for message we should give the name of receiver.ie it denotes which messages it should capture based on the receiver name.
LocalMessageSender : The constructor of this class accepts the receiver name.To send the message we have to use the method SendAsync.The SendCompleted event is fired after completion of sending process.
LocalMessageReceiver : The constructor of this class also got a parameter to specify the receiver name.MessageReceived is the event which will fire when a message comes with the corresponding receiver name.After subscribing into this event the Listen Method need to be called in order to start listening.

Creating Visual Studio Solution structure
Here I have used 3 projects 2 Silverlight projects one is source and next is destination,and one asp.net application which hosts these 2 Silverlight applications.

Default.aspx

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div  style="height:100%;">
<asp:Silverlight ID="Silverlight1" runat="server" 
Source="~/ClientBin/LocalMessagingSource.xap" MinimumVersion="3.0.40307.0" 
Width="400" Height="100"/>
</div>
<div>
Your html contents goes here..
<h1>Heading</h1>
<h3>Sub heading</h3>
<p>contents</p>
</div>
<div  style="height:100%;">
<asp:Silverlight ID="Silverlight2" runat="server" 
Source="~/ClientBin/LocalMessagingDestination.xap" MinimumVersion="3.0.40307.0" 
Width="400" Height="100" />
</div>
</form>


 MainPage.xaml in project LocalMessageSource


<UserControl x:Class="LocalMessagingSource.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid x:Name="LayoutRoot"
Background="AliceBlue"
Height="100"
Width="400">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.ColumnSpan="2"
HorizontalAlignment="Center">I am sender</TextBlock>
<TextBlock Grid.Row="1">Message</TextBlock>
<TextBox Grid.Row="1"
x:Name="txtMsg"
Grid.Column="1"
Text="" />
<Button Grid.Row="2"
Content="Send"
Click="Button_Click" />
<TextBlock x:Name="txtStatus"
Grid.Row="2"
Grid.Column="1" />
</Grid>
</UserControl>


MainPage.xaml.cs in project LocalMessageSource


private void Button_Click(object sender, RoutedEventArgs e)
{
LocalMessageSender msgSender = new LocalMessageSender("receiver");
msgSender.SendCompleted += new EventHandler<SendCompletedEventArgs>(msgSender_SendCompleted);
msgSender.SendAsync(txtMsg.Text); 
}

void msgSender_SendCompleted(object sender, SendCompletedEventArgs e)
{
txtStatus.Text = "Send";
}


MainPage.xaml.cs in project LocalMessageDestination


<UserControl x:Class="LocalMessagingDestination.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400"
Height="100">
<Grid x:Name="LayoutRoot"
Background="LightGoldenrodYellow">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.ColumnSpan="2"
HorizontalAlignment="Center">I am receiver</TextBlock>
<TextBlock Text="Received messag"
Grid.Row="1" />
<TextBox x:Name="txtMessage"
Grid.Row="1"
Grid.Column="1" />
</Grid>
</UserControl>


MainPage.xaml.cs in project LocalMessageDestination

public MainPage()
{
InitializeComponent();
LocalMessageReceiver receiver = new LocalMessageReceiver("receiver");

receiver.MessageReceived += new EventHandler<MessageReceivedEventArgs>(receiver_MessageReceived);
receiver.Listen();
}

void receiver_MessageReceived(object sender, MessageReceivedEventArgs e)
{
txtMessage.Text = e.Message;
}



Sample can be downloaded from here.

There are so many options with this messaging APIs such as domain blocking etc.You can send to some specific domains by using these options.More details later…

NB:This post is written using Silverlight 3 Beta.Things may change in Actual release.

No comments: