Chat Demo - Silverlight Client
Silverlight
Silverlight is Microsoft's solution for creating rich web applications using .NET technology. A Silverlight program consists of .NET code compiled into a DLL assembly together with Extended Application Markup Language (XAML) for describing the user interface. The application is downloaded on demand by the web browser and executed by a Silverlight plug-in.
Ice for Silverlight
ZeroC introduced Ice for Silverlight as the first experimental project of ZeroC Labs. The initial public beta release of Silverlight enforced restrictions that would have prevented the Ice for Silverlight run time from establishing a network connection to any host except the web server from which the Silverlight application was downloaded. To allow Ice programs to communicate with arbitrary hosts, Ice for Silverlight uses a bridge that runs on the web server and transparently forwards Ice requests to their intended destinations. For security reasons, the chat application uses a modified version of the bridge that will only forward requests to the chat server.
Asynchronous Pull Client
Our Silverlight chat client must operate as a pull client because Ice for Silverlight does not offer a server-side run time. Furthermore, the program has the same considerations as a graphical client in that it must maintain a responsive user interface. Unlike the C# and Java language mappings, Ice for Silverlight raises an exception if a program attempts to issue a synchronous invocation from the UI thread because doing so could cause the program to hang. Consequently, our Silverlight client polls for chat updates at regular intervals using Ice's asynchronous programming model, called Asynchronous Method Invocation (AMI). To invoke an operation using AMI, the operation's Slice definition must be annotated with metadata, as shown in the following example:
interface PollingChatSession {
["ami"] ChatRoomEventSeq getUpdates();
// ...
};
User Interface
The user interface of the chat client is constructed primarily using HTML and Cascading Style Sheets (CSS), similar to that of the PHP client. JavaScript code invokes directly into the C# application to relay commands, and C# code calls back into JavaScript to update the user interface. The diagram below shows the interactions of the various components when the user submits a new chat message:
Now let's examine the code that implements these interactions, starting with the JavaScript event handler that is invoked when the user presses the Enter key:
function processInputEvent(event)
{
if(event.keyCode == 13)
{
var sl = document.getElementById("SilverlightControl");
sl.Content.ChatCoordinator.send($('txtMessage').value);
}
}
The script obtains a reference to an object that is exported by our Silverlight code and supports a method named send. The event handler passes the contents of a text input field to send, whose C# definition is shown below:
[ScriptableMember]
public void send(string message)
{
PollingChat.PollingChatSessionPrx session = ...
session.send_async(sendMessageResponse, sendMessageException, message);
}
The attribute ScriptableMember makes it possible for JavaScript code to invoke this method. The implementation of the method invokes an asynchronous Ice operation on its session proxy to publish a new chat message. The first two arguments to send_async are delegates that handle the success or failure of the invocation when it eventually completes. For example, the sendMessageResponse method schedules a delegate that displays a new chat message. It accomplishes this with the help of a ScriptObject, which allows Silverlight code to call back into JavaScript to invoke the function userSay:
public void sendMessageResponse(long timestamp)
{
Dispatcher.BeginInvoke(delegate()
{
ScriptObject chatView = ...
chatView.Invoke("userSay", formatTimestamp(timestamp), _username, _message);
});
}
Finally, userSay updates the user interface:
userSay:function(timestamp, name, message)
{
this.addMessage("<div>" + timestamp + " - <" + name + "> " + message+ "</div>");
}
If you would like to see the Silverlight client in action, you can use ZeroC's live chat server.
| Previous: PHP Client |