Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Writing ASP Chat using JavaScript and XML (Part 4)

, 21 Jan 2004
The last part of the ASP chat series. This article explains how to provide a chat with multiple rooms.
// 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;
	this.identifier = "Messages";
}

/**
 *  Formats all messages in reversing order as HTML.
 *  @return HTML with messages.
 */
function Messages_formatAll(roomID) {
	var strMessages = "";
	var objMessages = this.getMessages(roomID);
	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(roomID, 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);
	var messages = this.getMessages(roomID);
	messages.add(strFormatted);
}

/**
 *  Addition of a new system message for the chat.
 */
function Messages_addSysMessage(roomID, strMessage) {
	strMessage = Server.HTMLEncode(strMessage);
	var filter = new IncorrectInputFilter;
	strMessage = filter.processText(strMessage);
	var strFormatted = this.formatSysMessage(strMessage);
	var messages = this.getMessages(roomID)
	messages.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 of specified room for easy accessing.
 *  @return LimitedXML object with shared messages.
 */
function Messages_getMessages(roomID) {
	var allRooms = new SharedXML(this.identifier);
	var roomNode = allRooms.findNode(roomID);
	if(roomNode == null) {
		// Create room if not exists.
		roomNode = allRooms.insertNode(roomID);
	}
	if(roomNode != null) {
		return new LimitedXML(roomNode, getMaxMessages());
	}
}
// 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

About the Author

Alexander Fedorenko
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.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 22 Jan 2004
Article Copyright 2004 by Alexander Fedorenko
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid