Tuesday, November 13, 2012

SRChat Plug Play Enjoy

SignalR Chat Plug Play Enjoy

Hey as per my promise and continuation to my previous SignalR To Rescue article here I present the more, in-depth concepts and overview of Chat application build over SignalR and asp.net.
For those who are novice to SignalR or have not read my previous post about basic infrastructure and overview and notification sample of SignalR are advised to go through this post first.
Coming straight to the point below is the overview of the chat module that you will build step by step while progressing this post
- It uses SignalR infrastructure as the base.
Used for coding “SRChatServer” server hub and “SRChatClient” client hub.
- It uses JQuery version 1.8.2
Used for all the power pact code on “SRChatClient” client hub
- It uses JQuery Template based binding mechanism for creating UI on client as part of best practice.
Used for binding the json data coming from “SRChatServer” methods and binding it to templates to generate the html based UI quickly
- It uses JQuery Dialog Extension from crab community at http://code.google.com/p/jquery-dialogextend/. Thanks a ton guys for such a beautiful and sleek master piece.
Used for showing the minimizable and movable dialog boxes for each chat instances with different users.

Brief Architecture Overview



Step 1) Create the asp.net web application project and install the SignalR infrastructure. Please refer SignalR to Rescue for basic configuration

Step 2) Create a serializable class named “MessageRecipient“. This class would act as an object for the users connecting to server hub. The reason for marking it serializable is to make easy transfer across the ajax request easily.

[sourcecode language="csharp"]
[Serializable]
public class MessageRecipient
{
publicMessageRecipient()
{
chatRoomIds = new List();
}
public string messageRecipientId { get; set; }
public string messageRecipientName { get; set; }
public string connectionId { get; set; }
public ListchatRoomIds { get; set; }
}
[/sourcecode]

Step 3) Create a serializable class named “ChatRoom “.This class would act as a bridge between the users who want to chat with each other. The same concept can be enhanced for providing Group Chat feature to this module with few lines of code.

[sourcecode language="csharp"]
[Serializable]
public class ChatRoom
{
public string chatRoomId { get; set; }
public string chatRoomInitiatedBy { get; set; }
public string chatRoomInitiatedTo { get; set; }
public ListmessageRecipients { get; set; }

publicChatRoom()
{
chatRoomId = Guid.NewGuid().ToString();
messageRecipients = new List();
}
}
[/sourcecode]

Step 4) Create a serializable class named “ChatMessage“. This class would act as an object for each chat messages that are exchanged between the users over chat.

[sourcecode language="csharp"]
[Serializable]
public class ChatMessage
{
publicChatMessage()
{
}

public string chatMessageId { get; set; }
public string conversationId { get; set; }
public string senderId { get; set; }
public string senderName { get; set; }
public string messageText { get; set; }
public string displayPrefix { get { return string.Format("[{0}] {1}:", timestamp.ToShortTimeString(), senderName); } }
publicDateTime timestamp { get; set; }
}
[/sourcecode]

Step 5) Create a serializable class named “OnlineContacts“. This class would act as an object for each userconnecting to server hub. It is used here so that a list of Message Recipients can be transferred back to client hub when the user wants to initiate the chat.

[sourcecode language="csharp"]
[Serializable]
public class OnlineContacts
{
public ListmessageRecipients { get; set; }
publicOnlineContacts()
{
messageRecipients = new List();
}
}
[/sourcecode]

Step 6) Finally create a class named “SRChatServer” and inherit from Hub class. This is the server hub that would act as the main heart for building the chat server. Decorate the class with [HubName] attribute. This is the name that Client hub uses to connect to sever hub. We will also add two private static variables for holding our data related to chat rooms and users connected to server hub. As SignalR core is totally thread safe I am using ConcurrentDictionary in order to maintain our collection in thread safe manner.

[sourcecode language="csharp"]
[HubName("sRChatServer")]
public class SRChatServer : Hub
{
#region Private Variables
private static readonly ConcurrentDictionary<string, MessageRecipient> _chatUsers = new ConcurrentDictionary<string, MessageRecipient>(StringComparer.OrdinalIgnoreCase);
private static readonly ConcurrentDictionary<string, ChatRoom> _chatRooms = new ConcurrentDictionary<string, ChatRoom>(StringComparer.OrdinalIgnoreCase);
#endregion
}
[/sourcecode]

Now let’s start adding more ingredients to our server hub.

First we will add connect and disconnect method. Connect method is called whenever the client is loaded for first time and theninitiates the request to connect to server hub. It will simply fetch some data related to connecting client like its connection ID, user IDetc. and store or modify the collection maintained on server hub. Similarly the Disconnect method is called just before the client moves out from the server hub.

[sourcecode language="csharp"]
public bool Connect(string userId, string userName)
{
try
{
if (string.IsNullOrEmpty(userId) | string.IsNullOrEmpty(userName))
{
return false;
}
if (GetChatUserByUserId(userId) == null)
{
AddUser(userId, userName);
}
else
{
ModifyUser(userId, userName);
}
SendOnlineContacts();
return true;
}
catch (Exception ex)
{
throw new InvalidOperationException("Problem in connecting to chat server!");
}
}
public override Task Disconnect()
{
try
{
DeleteUser(Context.ConnectionId);
return null;
}
catch (Exception ex)
{
throw new InvalidOperationException("Problem in disconnecting from chat server!");
}
}
[/sourcecode]

Now let’s add few methods that will help clients to initiate and end chat with other users. InitiateChat method simply takes in required data about the users who want to get connected and start the chat. The code simply builds a technical bridge between the two and initiates a pipeline through ChatRoom using which messages will be exchanged. The EndChat method simply ends the chat and removes the bridge between the two.

[sourcecode language="csharp"]
public bool InitiateChat(string fromUserId, string fromUserName, string toUserId, string toUserName)
{
try
{
if (string.IsNullOrEmpty(fromUserId) || string.IsNullOrEmpty(fromUserName) || string.IsNullOrEmpty(toUserId) || string.IsNullOrEmpty(toUserName))
{
return false;
}

var fromUser = GetChatUserByUserId(fromUserId);
var toUser = GetChatUserByUserId(toUserId);

if (fromUser != null && toUser != null)
{
if (!CheckIfRoomExists(fromUser, toUser))
{
//Create New Chat Room
ChatRoom chatRoom = new ChatRoom();
chatRoom.chatRoomInitiatedBy = fromUser.messageRecipientId;
chatRoom.chatRoomInitiatedTo = toUser.messageRecipientId;

chatRoom.messageRecipients.Add(fromUser);
chatRoom.messageRecipients.Add(toUser);

//create and save blank message to get new conversation id
ChatMessage chatMessage = new ChatMessage();
chatMessage.messageText = "Chat Initiated";
chatMessage.senderId = fromUser.messageRecipientId;
chatMessage.senderName = fromUser.messageRecipientName;

fromUser.chatRoomIds.Add(chatRoom.chatRoomId);
toUser.chatRoomIds.Add(chatRoom.chatRoomId);

//Create SignalR Group for this chat room and add users connection to it
Groups.Add(fromUser.connectionId, chatRoom.chatRoomId);
Groups.Add(toUser.connectionId, chatRoom.chatRoomId);

//Add Chat room object to collection
if (_chatRooms.TryAdd(chatRoom.chatRoomId, chatRoom))
{
//Generate Client UI for this room
Clients[fromUser.connectionId].initiateChatUI(chatRoom);
}
}
}
return true;
}
catch (Exception ex)
{
throw new InvalidOperationException("Problem in starting chat!");
}
}
public bool EndChat(ChatMessage chatMessage)
{
try
{
ChatRoom chatRoom;
if (_chatRooms.TryGetValue(chatMessage.conversationId, out chatRoom))
{
if (_chatRooms[chatRoom.chatRoomId].chatRoomInitiatedBy == chatMessage.senderId)
{
chatMessage.messageText = string.Format("{0} left the chat. Chat Ended!", chatMessage.senderName);
if (_chatRooms.TryRemove(chatRoom.chatRoomId, out chatRoom))
{
Clients[chatRoom.chatRoomId].receiveEndChatMessage(chatMessage);
foreach (MessageRecipient messageReceipient in chatRoom.messageRecipients)
{
if (messageReceipient.chatRoomIds.Contains(chatRoom.chatRoomId))
{
messageReceipient.chatRoomIds.Remove(chatRoom.chatRoomId);
Groups.Remove(messageReceipient.connectionId, chatRoom.chatRoomId);
}
}
}
}
else
{
MessageRecipient messageRecipient = GetChatUserByUserId(chatMessage.senderId);
if (messageRecipient != null && messageRecipient.chatRoomIds.Contains(chatRoom.chatRoomId))
{
chatRoom.messageRecipients.Remove(messageRecipient);
messageRecipient.chatRoomIds.Remove(chatRoom.chatRoomId);
if (chatRoom.messageRecipients.Count < 2)
{
chatMessage.messageText = string.Format("{0} left the chat. Chat Ended!", chatMessage.senderName);
if (_chatRooms.TryRemove(chatRoom.chatRoomId, out chatRoom))
{
Clients[chatRoom.chatRoomId].receiveEndChatMessage(chatMessage);
foreach (MessageRecipient messageReceipient in chatRoom.messageRecipients)
{
if (messageReceipient.chatRoomIds.Contains(chatRoom.chatRoomId))
{
messageReceipient.chatRoomIds.Remove(chatRoom.chatRoomId);
Groups.Remove(messageReceipient.connectionId, chatRoom.chatRoomId);
}
}
}
}
else
{
chatMessage.messageText = string.Format("{0} left the chat.", chatMessage.senderName);
Groups.Remove(messageRecipient.connectionId, chatRoom.chatRoomId);
Clients[messageRecipient.connectionId].receiveEndChatMessage(chatMessage);
Clients[chatRoom.chatRoomId].receiveLeftChatMessage(chatMessage);
Clients[chatRoom.chatRoomId].updateChatUI(chatRoom);
}
}
}
}
else
{
throw new InvalidOperationException("Problem in ending chat!");
}
return true;
}
catch (Exception ex)
{
throw new InvalidOperationException("Problem in ending chat!");
}
}
[/sourcecode]

Finally add below methods that actually send the message to particular client. SendChatMessage sends the message from one user to another and invokes the client hub method to push message to that particular client. SendOnlineContacts is just used for getting the online users list using which the chat can be initiated with anyone of them. I have kept this method private so that I can call it from connect method to broad cast the entire list including new user to all the users. If you want to fetch online contacts on some trigger on client do not forget to make it public.

[sourcecode language="csharp"]
public bool SendChatMessage(ChatMessage chatMessage)
{
try
{
ChatRoom chatRoom;
if (_chatRooms.TryGetValue(chatMessage.conversationId, out chatRoom))
{
chatMessage.chatMessageId = Guid.NewGuid().ToString();
chatMessage.timestamp = DateTime.Now;
Clients[chatMessage.conversationId].receiveChatMessage(chatMessage, chatRoom);
return true;
}
else
{
throw new InvalidOperationException("Problem in sending message!");
}
}
catch (Exception ex)
{
throw new InvalidOperationException("Problem in sending message!");
}
}
private bool SendOnlineContacts()
{
try
{
OnlineContacts onlineContacts = new OnlineContacts();
foreach (var item in _chatUsers)
{
onlineContacts.messageRecipients.Add(item.Value);
}
Clients.onGetOnlineContacts(onlineContacts);
return false;
}
catch (Exception ex)
{
throw new InvalidOperationException("Problem in getting contacts!");
}
}
[/sourcecode]

Apart from the above methods there are few auxiliary methods for supporting the above business logic.

That’s it for server hub. Now let’s plug Client Hub Code step by step

Step 1) Add a new asp.net webform and name it “SRChatClient.aspx”
This webform will be our chat client using which users can chat with each other. This webform when loaded will generate a random number and using this random number it connects with SRChatServer and registers itself. When multiple instances of different browsers are opened we have a group of users getting online who can chat with each other.

[sourcecode language="html"]
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SRChatClient.aspx.cs" Inherits="SRChat.SRChatClient" %></pre>
<style><!--
.chatRooms
{
max-height: 500px;
overflow: auto;
}
.chatRoom
{
width: 100%;
height: 250px;
border: 1px solid #ccc;
}
.chatMessages
{
width: 100%;
height: 200px;
overflow: auto;
margin-left: 0px;
padding-left: 0px;
}
.chatMessages li
{
list-style-type: none;
padding: 1px;
}
.chatNewMessage
{
border: 1px solid #ccc;
width: 200px;
float: left;
height: 18px;
}
.chatMessage
{
}
.chatSend
{
float: left;
}

--></style>
<pre>

</pre>
<form id="form1">
<h3>SRChat - By Dhaval Upadhyaya - <a href="http://dhavalupadhyaya.wordpress.com/about-me/" target="_blank">http://dhavalupadhyaya.wordpress.com/about-me/</a></h3>
<div>
<div id="userNameLabel"></div>
<div id="chatRooms"></div>
<div id="chatOnlineContacts"></div>
</div>
</form>
<pre>

[/sourcecode]

Step 2) Add references to style and js files required to bootstrap this page.

[sourcecode language="html"]
<link href="Styles/jquery-ui.css" rel="stylesheet" /><script type="text/javascript" src="Scripts/jquery-1.8.2.js"></script><script type="text/javascript" src="Scripts/jquery-ui.js"></script>
<script type="text/javascript" src="Scripts/jquery.dialogextend.1_0_1.js"></script><script type="text/javascript" src="Scripts/jquery.signalR.js"></script>
<script type="text/javascript" src="Scripts/jQuery.tmpl.js"></script><script type="text/javascript" src="signalr/hubs"></script>
[/sourcecode]

Step 3) Add templates that can be used to bind the json data and generate the html based UI quickly. Each template as the name justifies is used to generate the html UI when that particular type of triggers occur from the server.

[sourcecode language="html"]
<script id="new-online-contacts" type="text/x-jquery-tmpl">// <![CDATA[
<div>
<ul>
{{each messageRecipients}}

<li id="chatLink${messageRecipientId}"><a href="javascript:;" onclick="javascript:SRChat.initiateChat('${messageRecipientId}','${messageRecipientName}');">${messageRecipientName}</a></li>

{{/each}}</ul>

</div>

// ]]></script>
<script id="new-chatroom-template" type="text/x-jquery-tmpl">// <![CDATA[

<div id="chatRoom${chatRoomId}" class="chatRoom">

<ul id="messages${chatRoomId}" class="chatMessages"></ul>

<form id="sendmessage${chatRoomId}" action="#">
<input type="text" id="newmessage${chatRoomId}" class="chatNewMessage"/>

<div class="clear"></div>

<input type="button" id="chatsend${chatRoomId}" value="Send" class="chatSend" onClick="javascript:SRChat.sendChatMessage('${chatRoomId}')" />
<input type="button" id="chatend${chatRoomId}" value="End Chat" class="chatSend" onClick="javascript:SRChat.endChat('${chatRoomId}')" />
</form>

</div>

// ]]></script>
<script id="new-chat-header" type="text/x-jquery-tmpl">// <![CDATA[

<div id="chatRoomHeader${chatRoomId}">
{{each messageRecipients}}
{{if $index == 0}}
${messageRecipientName}
{{else}}
, ${messageRecipientName}
{{/if}}
{{/each}}
<div>

// ]]></script>
<script id="new-message-template" type="text/x-jquery-tmpl">// <![CDATA[

<li class="message" id="m-${chatMessageId}">
<strong>${displayPrefix}</strong>
{{html messageText}}</li>

// ]]></script>
<script id="new-notify-message-template" type="text/x-jquery-tmpl">// <![CDATA[

<li class="message" id="m-${chatMessageId}">
<strong>{{html messageText}}</strong></li>

// ]]></script>
[/sourcecode]

Step 5) Finally we will plug the magical code that would connect to our SRChatServer and registers all the client methods that would be invoked from server side in order to trigger and inject various messages and chat window instances.

[sourcecode language="html"]
<script type="text/javascript">// <![CDATA[
$(document).ready(function () { SRChat.attachEvents(); }); SRChat = new function () { var chatRooms = 0; var numRand = Math.floor(Math.random() * 1000) var senderId = numRand; var senderName = 'User ' + numRand; var sRChatServer; window.onbeforeunload = function () { if (chatRooms > 0)
return "All chat instances will be ended!";
};

this.attachEvents = function () {
$("#userNameLabel").html(senderName);
if ($.connection != null) {
jQuery.support.cors = true;
$.connection.hub.url = 'signalr/hubs';
sRChatServer = $.connection.sRChatServer;

$.connection.hub.start({ transport: 'auto' }, function () {
sRChatServer.server.connect(senderId, senderName).fail(function (e) {
alert(e);
});
});

sRChatServer.client.initiateChatUI = function (chatRoom) {
var chatRoomDiv = $('#chatRoom' + chatRoom.chatRoomId);
if (($(chatRoomDiv).length > 0)) {
var chatRoomText = $('#newmessage' + chatRoom.chatRoomId);
var chatRoomSend = $('#chatsend' + chatRoom.chatRoomId);
var chatRoomEndChat = $('#chatend' + chatRoom.chatRoomId);

chatRoomText.show();
chatRoomSend.show();
chatRoomEndChat.show();
}
else {
var e = $('#new-chatroom-template').tmpl(chatRoom);
var c = $('#new-chat-header').tmpl(chatRoom);

chatRooms++;

//dialog options
var dialogOptions = {
"id": '#messages' + chatRoom.chatRoomId,
"title": c,
"width": 360,
"height": 365,
"modal": false,
"resizable": false,
"close": function () { javascript: SRChat.endChat('' + chatRoom.chatRoomId + ''); $(this).remove(); }
};

// dialog-extend options
var dialogExtendOptions = {
"close": true,
"maximize": false,
"minimize": true,
"dblclick": 'minimize',
"titlebar": 'transparent'
};

e.dialog(dialogOptions).dialogExtend(dialogExtendOptions);

$('#sendmessage' + chatRoom.chatRoomId).keypress(function (e) {
if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
$('#chatsend' + chatRoom.chatRoomId).click();
return false;
}
});
}
};

sRChatServer.client.updateChatUI = function (chatRoom) {
var chatRoomHeader = $('#chatRoomHeader' + chatRoom.chatRoomId);
var c = $('#new-chat-header').tmpl(chatRoom);
chatRoomHeader.html(c);
};

sRChatServer.client.receiveChatMessage = function (chatMessage, chatRoom) {
sRChatServer.client.initiateChatUI(chatRoom);
var chatRoom = $('#chatRoom' + chatMessage.conversationId);
var chatRoomMessages = $('#messages' + chatMessage.conversationId);
var e = $('#new-message-template').tmpl(chatMessage).appendTo(chatRoomMessages);
e[0].scrollIntoView();
chatRoom.scrollIntoView();
};

sRChatServer.client.receiveLeftChatMessage = function (chatMessage) {
var chatRoom = $('#chatRoom' + chatMessage.conversationId);
var chatRoomMessages = $('#messages' + chatMessage.conversationId);
var e = $('#new-notify-message-template').tmpl(chatMessage).appendTo(chatRoomMessages);
e[0].scrollIntoView();
chatRoom.scrollIntoView();
};

sRChatServer.client.receiveEndChatMessage = function (chatMessage) {
var chatRoom = $('#chatRoom' + chatMessage.conversationId);
var chatRoomMessages = $('#messages' + chatMessage.conversationId);
var chatRoomText = $('#newmessage' + chatMessage.conversationId);
var chatRoomSend = $('#chatsend' + chatMessage.conversationId);
var chatRoomEndChat = $('#chatend' + chatMessage.conversationId);

chatRooms--;

var e = $('#new-notify-message-template').tmpl(chatMessage).appendTo(chatRoomMessages);

chatRoomText.hide();
chatRoomSend.hide();
chatRoomEndChat.hide();

e[0].scrollIntoView();
chatRoom.scrollIntoView();
};

sRChatServer.client.onGetOnlineContacts = function (chatUsers) {
var e = $('#new-online-contacts').tmpl(chatUsers);
var chatLink = $('#chatLink' + senderId);
e.find("#chatLink" + senderId).remove();
$("#chatOnlineContacts").html("");
$("#chatOnlineContacts").html(e);
};
}
};

this.sendChatMessage = function (chatRoomId) {
var chatRoomNewMessage = $('#newmessage' + chatRoomId);

if (chatRoomNewMessage.val() == null || chatRoomNewMessage.val() == "")
return;

var chatMessage = {
senderId: senderId,
senderName: senderName,
conversationId: chatRoomId,
messageText: chatRoomNewMessage.val()
};

chatRoomNewMessage.val('');
chatRoomNewMessage.focus();
sRChatServer.server.sendChatMessage(chatMessage).fail(function (e) {
alert(e);
});

return false;
};

this.endChat = function (chatRoomId) {
var chatRoomNewMessage = $('#newmessage' + chatRoomId);

var chatMessage = {
senderId: senderId,
senderName: senderName,
conversationId: chatRoomId,
messageText: chatRoomNewMessage.val()
};
chatRoomNewMessage.val('');
chatRoomNewMessage.focus();
sRChatServer.server.endChat(chatMessage).fail(function (e) {
//alert(e);
});
};

this.initiateChat = function (toUserId, toUserName) {
if (sRChatServer == null) {
alert("Problem in connecting to Chat Server. Please Contact Administrator!");
return;
}
sRChatServer.server.initiateChat(senderId, senderName, toUserId, toUserName).fail(function (e) {
alert(e);
});
};

};
// ]]></script>
[/sourcecode]

That It run the application and start chatting.

In next version I will add the feature of group chat. Though just few lines of code with the above version will do the job, I leave this to reader as a small exercise.

The full source code of Version 1.0 can be found on my GitHub repo at below link.

https://github.com/upadhyayadhaval/SRChat

Tuesday, October 9, 2012

SignalR To Rescue

SignalR To Rescue

Asynchronous library for .NET to help build real-time, multi-user interactive web applications.

Many asp.net based real time applications uses some traditional techniques to trigger some business on client. Some old techniques include an recursive timely call like Ajax call to server to fetch data at intervals, a trigger from server using duplex channel when using WCF etc… These all traditional techniques has some or more disadvantages like a constant callbacks increasing server load, firewalls restricting duplex protocols hiding clients and resulting in broken links in duplex channels etc.


But now SignalR has emerged to rescue us from these types of architectural bottlenecks. One can easily build notification systems, chat applications, triggers across multiple applications and other myriads of real-time application using this simple yet powerful technology.


In this article I will jot down a very basic sample of broadcasting message to a group or to a specific client using SignalR and asp.net. For further technical documentation please refer https://github.com/SignalR/SignalR/wiki


Prerequisites


In order to take advantage of SignalR a small integration of its infrastructure is required. To keep it simple I will use use Package Manager Console and Nuget package of SignalR later in the project.


Application


Two important parts needs to be coded for creating basic SignalR based applications.


- Server Hub: A simple class containing methods that can be called from client hub. Also it is the main container of logic on server side. It can trigger methods on client hub with specific connection or with a group of connections.


- Client Hub: A piece of code that is used for registering to server hub. It can contain methods that call server hub methods. It can also contain methods that are called from server hub. Client hubs can be coded in native ASP.Net or JavaScript.


Step 1) Create a new Asp.Net Web Application (“SignalRNotification”) in Visual Studio. Remove all the default folders and web forms created by template if any.



Step 2) Install SignalR infrastructure
Go to Tools > Library Package Manager > Package Manager Console



Run below command
Install-Package SignalR



The above steps will install SignalR infrastructure required to make use of this technology

For detail description and latest news for SignalR release please refer http://nuget.org/packages/signalr

Step 3) Create a new class named NotificationHub that inherits from Hub.

[sourcecode language="csharp"]
using System;
using SignalR.Hubs;

namespace SignalRNotification
{
[HubName("notificationHub")]
public class NotificationHub:Hub
{
public void NotifyUsers(string message)
{
//Invokes onReceiveNotification function for all the Clients
Clients.onReceiveNotification(string.Format("{0} {1}", message, DateTime.Now.ToString()));
}
}
}
[/sourcecode]

In the above code one can chose to send message to all clients, a group of clients or to a specific client. To keep things simple the above code simply broadcast the message to all clients.

Step 4) Create a web form Admin.aspx. Add a textbox and a button to it as shown below. It includes reference to magical scripts that is actually responsible for connecting to SignalR hub and creating the proxy class. Internally this script registers the client using a unique GUID by making an ajax call. We will use this form for sending messages.

[sourcecode language="html"]

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Admin.aspx.cs" Inherits="SignalRNotification.Admin" %>

Admin
<script type="text/javascript" src="Scripts/jquery-1.6.4.min.js"></script><script type="text/javascript" src="Scripts/jquery.signalR-0.5.3.min.js"></script>
<script type="text/javascript" src="/signalr/hubs"></script><script type="text/javascript">// <![CDATA[
$(function () {
// the generated client-side hub proxy
var server = $.connection.notificationHub;
// Start the connection
$.connection.hub.start();
$('#xbtnNotify').click(function () {
var message = document.getElementById('txtMessage').value;
//call the method on server using the proxy
server.notifyUsers(message);
});
});

// ]]></script>

<form id="form1">
<h1>Administrator</h1>
<div>Message :
<textarea id="txtMessage" style="width: 400px;" rows="3" cols="1"></textarea>
<input id="xbtnNotify" type="button" value="Send this to All Users" /></div>
</form>

[/sourcecode]

Step 5) Create a web form User.aspx as shown below. We will use this form for receiving messages.

[sourcecode language="html"]

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="User.aspx.cs" Inherits="SignalRNotification.User" %>

User

<script type="text/javascript" src="Scripts/jquery-1.6.4.min.js"></script><script type="text/javascript" src="Scripts/jquery.signalR-0.5.3.min.js"></script>
<script type="text/javascript" src="/signalr/hubs"></script><script type="text/javascript">// <![CDATA[
// the generated client-side hub proxy
$(function () {
var server = $.connection.notificationHub;
//This is the method that will be invoked from the server!!
server.onReceiveNotification = function (message) {
$("#messages").append('
<li>' + message + '</li>

');
};
// Start the connection
$.connection.hub.start();
});
// ]]></script><pre>
<form id="form1">
<div>
<h1>User</h1>
</div>
<ul id="messages"></ul>
</form>
</pre>

[/sourcecode]

That’s it.

A simple way to do complex things.
In my next article I will explain how to create CHAT server using SignalR.

Thursday, January 12, 2012

Screen Readers with Silverlight Applications

Screen Readers with Silverlight Applications.


Web based applications fly across global boundaries due to which accessibility and ease of use becomes one of the most important factor that drives the sales count of the service we provide.


As per 508 Compliance if we require our service served to audience with disabilities it should follow various aspects.


For thorough details please refer


http://en.wikipedia.org/wiki/Section_508_Amendment_to_the_Rehabilitation_Act_of_1973


This article is specifically targeted for users interested to know


How do we provide support for Screen Readers software with the applications developed in one of the most cutting edge technology Silverlight?


Traditional web based applications that emits html to browsers easily comply with Screen readers based on specifications followed for HTML.
With Silverlight based applications the Silverlight plug-in takes care for screen readers. We just need to follow some methodologies provided in form of Automation Properties in Silverlight. Automation Properties provide several methods and attachable properties which help us define access keys, instruction text for screen readers and much more.


For details please refer


http://msdn.microsoft.com/en-us/library/system.windows.automation.automationproperties(v=vs.95).aspx


By following these properties the work is pretty much simple.


Sample 1

[sourcecode language="html"]
<TextBox id=” nameTextBox” AutomationProperties.Name=”Please enter name” />
[/sourcecode]

Screen reader will voice over “Please enter name” when this textbox receives focus.

Sample 2

[sourcecode language="html"]
<TextBlock id=”nameTextBlock” Text=”Please enter name” />
<TextBox id=”nameTextBox” AutomationProperties.LabeledBy="{Binding ElementName= nameTextBlock }" />
[/sourcecode]

Screen reader will voice over “Please enter name” when this textbox receives focus. Here is the case when we want to directly bind the AutomationProperties.Name property of textbox to any control, in this case a label beside text box.


Pretty much simply till here.

But wait!


Silverlight SDK and Controls are very powerful and includes number of controls that can be bound directly to objects. Here is where most of the screen readers fail while generating voice over for such control. I will depict the problem with one of the control “Combo Box”


Problem
If your combo box is bound to any dictionary object including collection than screen readers will work perfectly. But if combo box is bound to any object like “Person” than screen readers will fail.


Sample Code

[sourcecode language="html"]
<ComboBox Name="personsComboBox" ItemsSource="{Binding}" DisplayMemberPath="Name"/>
[/sourcecode]

 

[sourcecode language="csharp"]
public class Person : INotifyPropertyChanged
{
private string name;
public string Name
{
get
{
return name;
}
set
{
if (value != name)
{
name = value;
OnPropertyChnaged("Name");
}
}
}

private string surname;
public string Surname
{
get
{
return surname;
}
set
{
if (value != surname)
{
surname = value;
OnPropertyChnaged("Surname");
}
}
}

private Int32 age;
public Int32 Age
{
get
{
return age;
}
set
{
if (value != age)
{
age = value;
OnPropertyChnaged("Age");
}
}
}

#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;

private void OnPropertyChnaged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

#endregion

}
[/sourcecode]

Solution

Screen Readers works on strings attached to the controls using attachable property AutomationProperties.Name as described in above example code. With the case were we are binding objects directly to control, screen readers fail and voice over the entire path (Namespace.ObjecName) were the object reside. In order to overcome this issue simply override ToString() method for your objects.


Adding this code with the Person object designed above will ask screen readers to voice over “Name” property.



[sourcecode language="csharp"]
public override string ToString()
{
return Name;
}
[/sourcecode]

Tricky but yet simple.

Enjoy Coding.