Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / ASP

Writing ASP Chat using JavaScript and XML (Part 1)

Rate me:
Please Sign up or sign in to vote.
4.15/5 (14 votes)
21 Jan 20045 min read 91.1K   2.4K   50  
Step-by-step article about writing web-based chat. This article explains the simplest chat.
// Messages class
/**
 *  Chat messages processing and formatting class.
 */
function Messages() {
	this.addMessage = Messages_addMessage;
	this.formatMessage = Messages_formatMessage;
	this.getMessages = Messages_getMessages;
	this.formatAll = Messages_formatAll;
	this.replaceNewLine = Messages_replaceNewLine;
	this.formatSysMessage = Messages_formatSysMessage;
	this.addSysMessage = Messages_addSysMessage;
}

/**
 *  Formats all messages in reversing order as HTML.
 *  @return HTML with messages.
 */
function Messages_formatAll() {
	var strMessages = "";
	var objMessages = this.getMessages();
	var items = objMessages.getItems();
	var i = items.length-1;
	for(;i >= 0; i--) {
		var x = items.item(i);
		strMessages += x.text;
		strMessages += "<BR>\n"
	}
	return strMessages;
}

/**
 *  Adds new message and performs message formatting.
 *  @param clientIP IP address of message sender.
 *  @param strNick nick name of sender.
 *  @param strMessage textual message contents.
 *  @param moodType mood type for current message.
 */
function Messages_addMessage(clientIP, strNick, strMessage, moodType) {
	var filter = new IncorrectInputFilter;
	strMessage = filter.processText(strMessage);
	strNick = Server.HTMLEncode(strNick);
	strMessage = Server.HTMLEncode(strMessage);
	strMessage = this.replaceNewLine(strMessage);
	var strFormatted = this.formatMessage(clientIP, strNick, strMessage, moodType);
	this.getMessages().add(strFormatted);
}

/**
 *  Addition of a new system message for the chat.
 */
function Messages_addSysMessage(strMessage) {
	strMessage = Server.HTMLEncode(strMessage);
	var strFormatted = this.formatSysMessage(strMessage);
	this.getMessages().add(strFormatted);
}

/**
 *  Performs single system message formatting.
 *  @param strMessage textual message contents.
 *  @return Formatted message HTML.
 */
function Messages_formatSysMessage(strMessage) {
	var strFormatted = "<font color=\"red\">System: " + strMessage + "</font>";
	return strFormatted;
}

/**
 *  Performs single message formatting.
 *  @param clientIP IP address of message sender.
 *  @param strNick nick name of sender.
 *  @param strMessage textual message contents.
 *  @param moodType mood type for current message.
 *  @return Formatted message HTML.
 */
function Messages_formatMessage(clientIP, strNick, strMessage, moodType) {
	var strFormatted = "<font color=\"#006666\">" + strNick 
		+ " (" + clientIP + "): " + strMessage + "</font>"; 
	if(moodType > 0 && moodType <= 5) {
		strFormatted += " <img src=\"img/mood" + moodType + ".gif\">";
	}
	return strFormatted;
}

/**
 *  Replaces all new line symbols to be visible in HTML.
 */
function Messages_replaceNewLine(source) {
	var re = /\n/g;
	return source.replace(re, "<BR>");
}

/**
 *  Prepares messages store for easy accessing.
 *  @return LimitedXML object with shared messages.
 */
function Messages_getMessages() {
	var objMessages = new LimitedXML("Messages", getMaxMessages())
	return objMessages;
}
// Messages class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Ukraine Ukraine
Alexander Fedorenko is a professional C++ developer since 1996. He completed many custom and shareware projects. One of the successful projects is DevPlanner - tool for personal planning and time management.

Comments and Discussions