alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Jetty example source code file (chat.js)

This example Jetty source code file (chat.js) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Jetty tags/keywords

off, off, server, server

The Jetty chat.js source code

dojo.require("dojox.cometd");
dojo.require("dojox.cometd.timestamp");

var room = {
	_last: "",
	_username: null,
	_connected: true,
	groupName: "whimsical",

	join: function(name){

		if(name == null || name.length==0 ){
			alert('Please enter a username!');
		}else{

			dojox.cometd.init(new String(document.location).replace(/http:\/\/[^\/]*/,'').replace(/\/examples\/.*$/,'')+"/cometd");
			// dojox.cometd.init("http://127.0.0.2:8080/cometd");
			this._connected = true;

			this._username = name;
			dojo.byId('join').className='hidden';
			dojo.byId('joined').className='';
			dojo.byId('phrase').focus();

			// subscribe and join
			dojox.cometd.startBatch();
			dojox.cometd.subscribe("/chat/demo", room, "_chat", { groupName: this.groupName});
			dojox.cometd.publish("/chat/demo", { 
				user: room._username,
				join: true,
				chat : room._username+" has joined"
			}, { groupName: this.groupName });
			dojox.cometd.endBatch();

			// handle cometd failures while in the room
			room._meta = dojo.subscribe("/cometd/meta", this, function(event){
				console.debug(event);   
				if(event.action == "handshake"){
					room._chat({ data: {
						join: true,
						user:"SERVER",
						chat:"reinitialized"
					} });
					dojox.cometd.subscribe("/chat/demo", room, "_chat", { groupName: this.groupName });
				}else if(event.action == "connect"){
					if(event.successful && !this._connected){
						room._chat({ data: {
							leave: true,
							user: "SERVER",
							chat: "reconnected!"
						} });
					}
					if(!event.successful && this._connected){
						room._chat({ data: {
							leave: true,
							user: "SERVER",
							chat: "disconnected!"
						} });
					}
					this._connected = event.successful;
				}
			}, {groupName: this.groupName });
		}
	},

	leave: function(){
		if(!room._username){
			return;
		}

		if(room._meta){
			dojo.unsubscribe(room._meta, null, null, { groupName: this.groupName });
		}
		room._meta=null;

		dojox.cometd.startBatch();
		dojox.cometd.unsubscribe("/chat/demo", room, "_chat", { groupName: this.groupName });
		dojox.cometd.publish("/chat/demo", { 
			user: room._username,
			leave: true,
			chat : room._username+" has left"
		}, { groupName: this.groupName });
		dojox.cometd.endBatch();

		// switch the input form
		dojo.byId('join').className='';
		dojo.byId('joined').className='hidden';
		dojo.byId('username').focus();
		room._username = null;
		dojox.cometd.disconnect();
	},

	chat: function(text){
		if(!text || !text.length){
			return false;
		}
		dojox.cometd.publish("/chat/demo", { user: room._username, chat: text}, { groupName: this.groupName });
	},

	_chat: function(message){
		var chat=dojo.byId('chat');
		if(!message.data){
			console.debug("bad message format "+message);
			return;
		}
		var from=message.data.user;
		var special=message.data.join || message.data.leave;
		var text=message.data.chat;
		if(!text){ return; }

		if( !special && from == room._last ){
			from="...";
		}else{
			room._last=from;
			from+=":";
		}

		if(special){
			chat.innerHTML += "<span class=\"alert\">"+from+" "+text+"
"; room._last=""; }else{ chat.innerHTML += "<span class=\"from\">"+from+" "+text+"
"; } chat.scrollTop = chat.scrollHeight - chat.clientHeight; }, _init: function(){ dojo.byId('join').className=''; dojo.byId('joined').className='hidden'; dojo.byId('username').focus(); var element=dojo.byId('username'); element.setAttribute("autocomplete","OFF"); dojo.connect(element, "onkeyup", function(e){ if(e.keyCode == dojo.keys.ENTER){ room.join(dojo.byId('username').value); return false; } return true; }); dojo.connect(dojo.byId('joinB'), "onclick", function(e){ room.join(dojo.byId('username').value); e.preventDefault(); }); element=dojo.byId('phrase'); element.setAttribute("autocomplete","OFF"); dojo.connect(element, "onkeyup", function(e){ if(e.keyCode == dojo.keys.ENTER){ room.chat(dojo.byId('phrase').value); dojo.byId('phrase').value=''; e.preventDefault(); } }); dojo.connect(dojo.byId('sendB'), "onclick", function(e){ room.chat(dojo.byId('phrase').value); dojo.byId('phrase').value=''; }); dojo.connect(dojo.byId('leaveB'), "onclick", room, "leave"); } }; dojo.addOnLoad(room, "_init"); dojo.addOnUnload(room,"leave"); //vim:ts=4:noet:

Other Jetty examples (source code examples)

Here is a short list of links related to this Jetty chat.js source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.