|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThis article describes how to implement a web-chat with ASP.NET and Ajax. In the first part I introduce the technologies I used for the application. Then I explain the concept of a chat and in the last part I present the main implementations. The goal of this article is to show the interested reader how to develop a chat without browser-plugins or java-applets, just pure HTML and JavaScript. Which Technology is Used?A web-chat is an application for which dynamically generated HTML is needed and client-server interaction. Therefore, ASP.NET 2.0 has been used and of course Ajax for the client-server-intercation without postbacks. We could use Ajax.NET but to keep it simple we decided to use the Ajax-implementation from Mike Schwartz called AjaxPro. To do all serverside calls from JavaScript with AjaxPro you need to do the following:
AjaxPro.Utility.RegisterTypeForAjax(typeof(MyAjaxClass));
That's all. Other details will be explained in the implemenation-part of the article. The Design of a Chat?There are different kind of chats. In this example I designed a chat in which users can login with their username and password. Then they can enter the chat and send messages. The messages are visible for all other chat-users. It is a public chat, where all logged in chatters can communicate together.
The Session holds a (1) AddChatter (store chat-user)Save the curent user after successful login to the Session. (2) AddToMsgQueueWhen sending a chat-message you need to store this in the CMQ. You will have to invoke this action from a JavaScript function. The scriptcode calls a serverside method via Ajax that has the ability to access the ASP.NET-Application. (3) GetMsgsFromQueueHere we need to check in a JavaScript-timer every n seconds, if there are new chat-messages for the client to display. So we poll the CMQ for new messages by an Ajax-method. If we have new messages, these are transformed to a string that can be displayed as a list in a chatwindow. The ImplementationSo far the theory, now the concrete coding. Here are the datatypes we need
ChatterThis class has an Id to identify a chatter internally and a name for the display. The ChatMsgA chat-message has a Key (Guid), the name of the chatter who sent this message and the message itself as string. ChatMgrThe The HTML-components are located on the WebForm Chat.aspx. We have
When the page loads, all logged in chatters and the available messages are loaded and displayed in the corresponding HTML-elements. Here is the JavaScript-code which is called cause we add the calls to it in the onload-handler of the Chat.aspx-page: function fillUsers()
{
ChatMgr.GetChattersHtml( fillUsers_CallBack );
}
function fillUsers_CallBack( res )
{
var userBox = document.getElementById('divUsers');
if( res.value == '' )
{
setTimeout( "fillUsers()", 5000 );
return;
}
userBox.innerHTML = res.value;
setTimeout( "fillUsers()", 5000 );
}
function getMsgs()
{
ChatMgr.GetMsgsFromQueue( getMsgs_CallBack );
}
function getMsgs_CallBack( res )
{
var chatBox = document.getElementById('divChatBox');
chatBox.innerHTML += res.value;
scrollContentDown();
setTimeout( "getMsgs()", 2000 );
}
The [ AjaxMethod( HttpSessionStateRequirement.Read ) ]
public string GetChattersHtml()
{
string result = "";
if( SessionMgr.Instance.OnlineUsers == null )
return result;
foreach (Member member in SessionMgr.Instance.OnlineUsers.Values )
{
result += "<div class='ChatUser' style='width: 280px'>";
result += "<table border=0 cellpadding=0 cellspacing=0><tr>";
result += "<td>" + UserImage.GetHtml( member.ID, "" ) + "</td>";
result += "<td> <b>" + member.Name + "</b></td>";
result += "</tr></table>";
result += "</div>";
}
return result;
}
The method of the For the other code-parts it's just the same: the script calls the serverside method, the method returns the return-values which are read-out in an asynchronous callback-function in script. Here is the logic for sending messages to the queue: function sendMessage()
{
var newMsg = '';
var chatBox = null;
var msg = document.getElementById('txtMsg').value;
if( msg == '' )
return;
document.getElementById('txtMsg').value = '';
newMsg = ChatMgr.AddToMsgQueue( msg ).value;
chatBox = document.getElementById('divChatBox');
chatBox.innerHTML += '' +
newMsg.Name + ': ' + newMsg.Message + '';
}
On pressing return, the function You can see the codegod-chat in action here after logging in. For more information on ASP.NET and Ajax visit our page here
|
|||||||||||||||||||||||||||||||||||||||||||||||||||