Silverlight 2 Networking Options

image I've been spending my nights working on a new book covering Silverlight 2 and have been focusing on the new networking features that are available in the System.Net and related namespaces.  Silverlight's great at animating objects, performing transformations and collecting data with all of the new controls that are available, but at some point you'll need to retrieve data or send data to a service.  There's great support built-in for calling WCF services, ASMX services as well as other services.  Support for  calling REST APIs is also very good and easy to implement using classes such as WebClient and HttpWebRequest/HttpWebResponse.  Asynchronous requests can be issued and handled quite easily once you figure out the pattern (which is quite consistent throughout the different networking classes).

Serialization and Deserialization Options

Once data returns from a service you can parse it using multiple techniques.  I'll blog about these individually over the next few months but in a nutshell you can map XML data into CLR objects in Silverlight 2 using the XmlReader class, XmlSerializer class or the LINQ to XML technology and related classes such as XDocument.  JSON can be mapped to CLR objects using the DataContractJsonSerializer and other open source options are available at http://www.codeplex.com.  RSS and ATOM feeds can be parsed and items/entries can be accessed using classes such as SyndicationFeed and SyndicationItem.  All of this functionality is straightforward to use especially if you have a background in VB.NET or C#.  The  available networking and parsing classes make working with data (especially XML data) a pleasant experience.

Support for Sockets

In addition to the request/response features built into Silverlight 2 you can also use sockets to communicate and exchange data.  A socket is basically a port and an IP address used to send data between a client and a server.  Currently Silverlight 2 supports port ranges between 4502 and 4532 although it's possible that may change since it's still in the beta 1 stage.  By using sockets a Silverlight client can talk directly with a server that listens for client connections and the server can push data to the client without requiring the client to consistently poll the server for changes...that's cool!  If you've seen some of the sports scoring applications at ESPN.com then you've experienced some of the push technologies that are out there. 

I wanted to play around with the new sockets capabilities so I decided to build a sample application that could push simulated basketball game scores down to a client.  Multiple clients can connect to a socket server and the server can then push the same data out to the clients which is kind of interesting since they stay in sync.  I'm not going to go into details on the implementation of the server or Silverlight client in this post (those attending DevConnections Orlando will get some of this in my Silverlight 2 talk), but I'll be breaking down the different pieces in posts coming in the near future. 

Until then, here's what the application looks like at this point.  Score data is sent as XML from the server to the client along with details about what action occurred in the game.  The game data is randomly generated on the server to simulate turnovers, fouls, 2 point shots, 3 point shots and foul shots.  There's a lot more than can be done to enhance the application, but here's what it looks like to this point. 

SilverlightSockets

I'll be posting details about how to build a server and client that communicate via sockets soon.  In the meantime make sure you check out Tim Heuer's blog on Silverlight 2 topics.  He's a Silverlight Evangelist for Microsoft (and good buddy) and has some awesome posts on Silverlight.  He posts about things that I haven't seen anywhere else.

If you're brand new to Silverlight and not quite ready to get into something like networking features and sockets, check out the following videos I put together that walk through Scott Guthrie's great written tutorials:

Part 1: Creating "Hello World" with Silverlight 2 and VS 2008
Tutorial
Video Tutorial

Part 2: Using Layout Management
Tutorial
Video Tutorial

Part 3: Using Networking to Retrieve Data and Populate a DataGrid
Tutorial
Video Tutorial

Part 4: Using Style Elements to Better Encapsulate Look and Feel
Tutorial
Video Tutorial

Part 5: Using the ListBox and DataBinding to Display List Data
Tutorial
Video Tutorial

Part 6: Using User Controls to Implement Master/Details Scenarios
Tutorial
Video Tutorial

Part 7: Using Templates to Customize Control Look and Feel
Tutorial
Video Tutorial

Part 8: Creating a Digg Desktop Version of our Application using WPF
Tutorial
Video Tutorial

comments powered by Disqus

2 Comments

  • I will buy the book when available. I will order it when Amazon's preorder btn lets me or if the publisher provides an early preview ebook option. Looking forward to it.

  • Could you address the options available for Peer-to-Peer communication between Silverlight apps? If I had 10 workstations in an office all running one Silverlight app, can they communicate directly amongst themselves?

Comments have been disabled for this content.