Click here to Skip to main content
11,410,118 members (60,688 online)
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.
<script language="JavaScript" runat="server" src="Settings.js"></script>
<script language="JavaScript" runat="server" src="MessagesClass.js"></script>
<script language="JavaScript" runat="server" src="LimitedXmlClass.js"></script>
<script language="JavaScript" runat="server" src="IncorrectInputFilter.js"></script>
<script language="JavaScript" runat="server" src="SessionClass.js"></script>
<script language="JavaScript" runat="server" src="SharedXMLClass.js"></script>
<script language="JavaScript" runat="server" src="SingleLevelXMLClass.js"></script>
<script language="JavaScript" runat="server" src="UserManagerClass.js"></script>
<script language="JavaScript" runat="server" src="UsersDataXMLClass.js"></script>
<script language="JavaScript" runat="server" src="UserInfoClass.js"></script>
<script language="JavaScript" runat="server" src="RoomsDataXMLClass.js"></script>
<script language="JavaScript" runat="server" src="RoomInfoClass.js"></script>
<script language="JavaScript" runat="server">
/**
 *  Addition of a new message for the chat.
 */
function addMessage(roomID, sessionID, strMessage, moodType) {
	var clientIP = Request.ServerVariables("REMOTE_ADDR");
	var objMessages = new Messages;
	var strNick = getUserNick(roomID, sessionID);
	if(typeof(strNick) != "undefined") {
		objMessages.addMessage(roomID, clientIP, strNick, strMessage, moodType)
	}
}

/**
 *  Addition of a new system message for the chat.
 */
function addSysMessage(roomID, strMessage) {
	var objMessages = new Messages;
	objMessages.addSysMessage(roomID, strMessage)
}

/**
 *  Formats all messages as HTML for user friendly representation.
 */
function formatAll(roomID) {
	var objMessages = new Messages;
	var strMessages = objMessages.formatAll(roomID);
	return strMessages;
}

/**
 *  Adds user to active users list.
 *  @return Session unique id or -1 if fails.
 */
function loginUser(strUserName, password) {
	var sessionID = -1;
	strUserName = Server.HTMLEncode(strUserName);
	password = Server.HTMLEncode(password);
	var isValidUser = authenticateUser(strUserName, password);
	if(isValidUser) {
		var userManager = new UserManager;
		var clientIP = Request.ServerVariables("REMOTE_ADDR");
		sessionID = userManager.loginUser(strUserName, clientIP);
	}
	return sessionID;
}

/**
 *  Cheks user name and password combination.
 *  @return true if valid user, overvise false.
 */
function authenticateUser(userName, password) {
	var usersData = new UsersDataXML;
	return usersData.authenticateUser(userName, password);
}

/**
 *  Registers new user.
 *  @return New user id or -1 if already exists.
 */
function registerUser(nick, password, firstName, 
					  lastName, email) {
	var usersData = new UsersDataXML;
	nick = Server.HTMLEncode(nick);
	password = Server.HTMLEncode(password);
	firstName = Server.HTMLEncode(firstName);
	lastName = Server.HTMLEncode(lastName);
	email = Server.HTMLEncode(email);
	return usersData.registerUser(nick, password, firstName, 
		lastName, email);
}

/**
 *  Removes user from active users list.
 *  @param session unique id.
 */
function logoutUser(roomID, sessionID) {
	var userManager = new UserManager;
	if(sessionID > -1) {
		var strUserName = userManager.getUserNick(roomID, sessionID);
		if(typeof(strUserName) != "undefined") {
			userManager.removeSession(roomID, sessionID);
			var strMessage = strUserName + " went from here!"
			addSysMessage(roomID, strMessage);
		}
	}
}

/**
 *  Extracts user name by session id.
 */
function getUserNick(roomID, sessionID) {
	var userManager = new UserManager;
	return userManager.getUserNick(roomID, sessionID);
}

/**
 *  Formats users list for specified session.
 */
function formatUsers(roomID, sessionID) {
	var userManager = new UserManager;
	return userManager.formatUsers(roomID, sessionID);
}

/**
 *  Retrieves quantity of currently logged users in the specified room.
 */
function getActiveUsersCount(roomID) {
	var userManager = new UserManager;
	return userManager.getUsersCount(roomID);
}

/**
 *  Formats html representation of available rooms with quantity 
 * of active users for each.
 */
function formatAllRooms() {
	var allRoomsFormat = "";
	var roomsData = new RoomsDataXML;
	var roomsList = roomsData.getRoomsList();
	var i = 0;
	for(i = 0; i < roomsList.length; i++) {
		var x = roomsList.item(i);
		var info = new RoomInfo(x);
		var roomID = info.getRoomID();
		var nUsers = getActiveUsersCount(roomID);
		var roomFormat = "<tr><td><a href=\"javascript:goToRoom("
			+ roomID + ")\">" + info.getRoomName()
			+ "</a></td><td>" + nUsers
			+ "</td></tr>\n";
		allRoomsFormat += roomFormat;
	}
	return allRoomsFormat;
}

/**
 *  Checks for session in specified room presence.
 *  @return true if session exists, overwise false.
 */
function isValidSession(roomID, sessionID) {
	var userManager = new UserManager;
	return userManager.isValidSession(roomID, sessionID);
}

/**
 *  Create a new chat room.
 */
function createRoom(sessionID, roomName) {
	var isValid = isValidSession(0, sessionID);
	if(isValid) {
		var roomsData = new RoomsDataXML;
		return roomsData.createRoom(roomName);
	}
}

/**
 *  Enter just logged user to the room.
 */
function enterToTheRoom(roomID, sessionID) {
	return switchRoom(0, roomID, sessionID);
}

/**
 *  Move user to another room.
 */
function switchRoom(roomIDOld, roomIDNew, sessionID) {
	var strUserName = getUserNick(roomIDOld, sessionID);
	var userManager = new UserManager;
	var alreadyExist = userManager.getSessionID(roomIDNew, strUserName);
	if(alreadyExist == -1 && sessionID > -1) {
		userManager.switchRoom(roomIDOld, roomIDNew, sessionID);
		strMessage = "Welcome " + strUserName + "!"
		addSysMessage(roomIDNew, strMessage);
		return sessionID;
	}
	else {
		// User already in target room. Required only removing from previous.
		userManager.removeSession(roomIDOld, sessionID);
		// Provide existent session no.
		return alreadyExist;
	}
}

/**
 *  Clone session for another room.
 */
function copySessionToRoom(roomIDOld, roomIDNew, sessionID) {
	var userManager = new UserManager;
	var strUserName = getUserNick(roomIDOld, sessionID);
	var alreadyExist = userManager.getSessionID(roomIDNew, strUserName);
	if(alreadyExist == -1) {
		userManager.copySessionToRoom(roomIDOld, roomIDNew, sessionID);
		return sessionID;
	}
	else {
		return alreadyExist;
	}
}
</script>

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

Share

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 | Terms of Use | Mobile
Web02 | 2.8.150414.5 | Last Updated 22 Jan 2004
Article Copyright 2004 by Alexander Fedorenko
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid