Click here to Skip to main content
15,894,337 members
Articles / Programming Languages / Javascript

Writing ASP Chat using JavaScript and XML (Part 4)

Rate me:
Please Sign up or sign in to vote.
4.00/5 (4 votes)
21 Jan 20045 min read 97.7K   1.8K   32  
The last part of the ASP chat series. This article explains how to provide a chat with multiple rooms.
// Session class.
/**
 *  Manipulations with session, stored in xml.
 *  @param xmlNode XML node with single session information.
 */
function Session(xmlNode, markAccesssing) {
	this.xmlNode = xmlNode;
	this.markAccesssing = markAccesssing;
	this.userExpirationTime = getSessionExpirationTime(); // seconds
	this.getSessionID = Session_getSessionID;
	this.getUserNick = Session_getUserNick;
	this.setUserNick = Session_setUserNick;
	this.getClientIP = Session_getClientIP;
	this.setClientIP = Session_setClientIP;
	this.doesExpired = Session_doesExpired;
	this.markLastAccessTime = Session_markLastAccessTime;
	this.setAttribte = Session_setAttribte;
	this.getAttribte = Session_getAttribte;
}

/**
 *  Extracting session id of owned session.
 *  @return The session id.
 */
function Session_getSessionID() {
	var id = this.getAttribte("ID");
	this.markLastAccessTime();
	return id;
}

/**
 *  Extracting user nick.
 */
function Session_getUserNick() {
	var userNick = this.getAttribte("NAME");
	this.markLastAccessTime();
	return userNick;
}

/**
 *  Assigning new user nick.
 */
function Session_setUserNick(userNick) {
	this.setAttribte("NAME", userNick);
	this.markLastAccessTime();
}

/**
 *  Extracting client of this session IP address.
 */
function Session_getClientIP() {
	var clientIP = this.getAttribte("CLIENTIP");
	this.markLastAccessTime();
	return clientIP;
}

/**
 *  Assigning client IP address.
 */
function Session_setClientIP(clientIP) {
	this.setAttribte("CLIENTIP", clientIP);
	this.markLastAccessTime();
}

/**
 *  Check record for not accessed time-out.
 */
function Session_doesExpired() {
	var accessedValue = this.getAttribte("ACCESSED");
	if(accessedValue == null) {
		// Maybe the first time accessing.
		return false;
	}
	var current = new Date();
	var p = Number(accessedValue);
	var expirationDiff = p + this.userExpirationTime*1000;
	return (expirationDiff < current.valueOf());
}

/**
 *  Continue accessing this session.
 */
function Session_markLastAccessTime() {
	if(this.markAccesssing) {
		var currentTime = new Date();
		var lastAccessTime = currentTime.valueOf();
		this.setAttribte("ACCESSED", lastAccessTime);
	}
}

/**
 *  Create or rename name-value pair of attribute.
 *  @param name the attribute name.
 *  @param value attribute value.
 */
function Session_setAttribte(name, value) {
	var attributeNode = this.xmlNode.attributes.getNamedItem(name);
	if(attributeNode != null) {
		attributeNode.nodeValue = value;
	}
	else {
		var xmlDoc = this.xmlNode.ownerDocument;
		attributeNode = xmlDoc.createAttribute(name);
		attributeNode.nodeValue = value;
		this.xmlNode.attributes.setNamedItem(attributeNode);
	}
}

/**
 *  Retraiving specified attribute value.
 *  @param name the attribute name.
 *  @return Attribute value.
 */
function Session_getAttribte(name) {
	var xmlNode = this.xmlNode.attributes.getNamedItem(name);
	if(xmlNode != null) {
		return xmlNode.nodeValue;
	}
	else {
		return null;
	}
}
// Session 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