A ChatBox in JavaScript
This is an extendable chatbox in javascript. It looks like this:This code has been tested on Mozilla Firefox 1.4 and Internet Explorer 6 only. It should work on most browsers that support XMLHttpRequest. If it doesn't, please contact the author.
You can download the zip with all the code.
Source code
Here is some sample code that demonstrates how to set up the above chatbox.
The call
var chat = new nl.zeekat.chat.SimpleChat("http://zeekat.nl/downloads/chatbox/","chatbox");
chat.start();
simplechat.js
This defines the nl.zeekat.chat.SimpleChat class. This is just about the simplest working chatbox class you can build on top of the ChatBase class.
/*
Copyright (c) 2005 Joost Diepenmaat, Zeekat Softwareontwikkeling,
all rights reserved.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
simplechat.js $Revision: 1.10 $
A javascript chatbox using XMLHttpRequest.
The webpage for this code is at http://zeekat.nl/downloads/chatbox/
*/
(function(){
var xjs = nl.zeekat.xjs;
var chat = nl.zeekat.chat;
chat.SimpleChat = xjs.constructor({
prototype: new chat.ChatBase(),
init: function ( baseUrl, elementId ) {
if (arguments.length > 0) {
this.messagesUrl = baseUrl + "/messages.xml";
}
},
send: function(message) {
var chatbox = this;
chatbox.inputBox.value="";
chatbox.inputBox.focus();
this.getRequest(
this.baseUrl+"/chatbox.cgi?message="+escape(message)+"&author="+escape(chatbox.authorBox.value),
function (resp) {
chatbox.updateStatus("Message posted");
chatbox.secondsLeft = 3;
chatbox.countDown();
},
null
);
},
parseMessage: function(node) {
var index = 0;
var nindex = 0;
var html = "";
var text = node.firstChild.data;
this.updateStatus("msg "+text);
var author = node.getAttribute("author");
var emote = (text.indexOf("/me") == 0 || text.indexOf("/me") == 1);
if (emote) {
text = text.substring(4,text.length);
}
html = this.htmlEscape(text);
html = html.replace(/(http:\/\/\S*)/g,'<a href="$1">$1</a>');
var r = {
emote: emote,
html: html,
author: author
};
return r;
},
createInterface: function() {
chat.SimpleChat.superclass.createInterface.apply(this);
this.authorBox = document.createElement("input");
this.authorBox.type = 'text';
this.authorBox.value = "anonymous";
this.form.insertBefore(this.authorBox,this.form.firstChild);
}
});
})();
chatbase.js
This is the base class for the chatbox: nl.zeekat.chat.ChatBase. It is intended to be flexible enough to allow for subclasses that use different message formats and functionality. I'm working on a subclass for the perlmonks chatterbox, too. All code is available the zipfile above.
Depends on the xjs library.
chatbox.cgi
This is the server-side component. It stores the last 10 messages in an XML file.
messages.xml
This is were the messages are stored for this demo. See for yourself.