Created
February 28, 2011 16:55
-
-
Save aelaguiz/847609 to your computer and use it in GitHub Desktop.
Communications for chat client using message.socket.io
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ChatRoom = function ChatRoom(chatList, inputArea, list) { | |
var _chatList = chatList, | |
_inputText = inputArea, | |
_userList = list, | |
_nickList = []; | |
/* | |
* Handle three events from the communications layer | |
*/ | |
this.expose = { | |
'joined': { | |
'eventHandler': '__api__joined' | |
}, | |
'left': { | |
'eventHandler': '__api__left' | |
}, | |
'said': { | |
'eventHandler': '__api__said' | |
} | |
}; | |
this.__api__joined = function __api__joined(comInstance, args) { | |
this._addMetaLine(args['nick'] + " joined"); | |
this.addUser(args['nick']); | |
} | |
this.__api__left = function __api__left(comInstance, args) { | |
this._addMetaLine(args['nick'] + " left"); | |
this.delUser(args['nick']); | |
} | |
this.__api__said = function __api__said(comInstance, args) { | |
this._addChat(args['nick'], args['text']); | |
} | |
this.addUser = function addUser(nick) { | |
_nickList.push(nick); | |
this._addUserHTML(nick); | |
} | |
this.delUser = function delUser(nick) { | |
/* | |
* Remove the user from our user list | |
*/ | |
for(var i = 0, max = _nickList.length; i < max; i++) { | |
if(nick === _nickList[i]) { | |
arrayRemove(_nickList, i); | |
break; | |
} | |
} | |
/* | |
* Rebuild the user list html entity | |
*/ | |
_userList.innerHTML = ''; | |
for(var i = 0, max = _nickList.length; i < max; i++) { | |
this._addUserHTML(_nickList[i]); | |
} | |
} | |
this._addChat = function addChat(nick, text) { | |
this._addLine(nick + ": " + text); | |
} | |
this._addLine = function addLine(chatText) { | |
_chatList.innerHTML += "<li class='chat'>" + chatText + "</li>"; | |
_chatList.scrollTop = _chatList.scrollHeight; | |
} | |
this._addMetaLine = function addMetaLine(text) { | |
_chatList.innerHTML += "<li class='meta'>" + text + "</li>"; | |
_chatList.scrollTop = _chatList.scrollHeight; | |
} | |
this._addUserHTML = function addUserHTML(nick) { | |
_userList.innerHTML += "<li>" + nick + "</li>"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ChatRoom = function ChatRoom() { | |
/* | |
* Expose two functions to the communications layer, joining and talking | |
*/ | |
this.expose = { | |
'say': { | |
'function': '__api__say' | |
}, | |
'join': { | |
'function': '__api__join' | |
} | |
}; | |
this.nickList = []; | |
this.clients = []; | |
this.chatLines = []; | |
} | |
ChatRoom.prototype.__proto__ = EventEmitter.prototype; | |
ChatRoom.prototype.__api__say = function __api__say(comInstance, args, callback) { | |
var text = args.text, | |
nick, | |
lineObj; | |
if(undefined === text) { | |
callback(new Error("Client did not say shit")); | |
} | |
else { | |
nick = this.clients[comInstance.getInstanceId()]; | |
lineObj = {'nick': nick, 'text': text}; | |
this.chatLines.push(lineObj); | |
callback(null, {}); | |
this.emit('said', 'room', lineObj); | |
} | |
} | |
ChatRoom.prototype.__api__join = function __api__join(comInstance, args, callback) { | |
var nick = args.nick; | |
if(undefined === nick) { | |
callback(new Error("Client did not specify nickname")); | |
} | |
else if(!this.isNickAvailable(nick)) { | |
callback(new Error("Nickname is already in use")); | |
} | |
else { | |
this.clients[comInstance.getInstanceId()] = nick; | |
comInstance.subscribeObject(this, 'joined'); | |
comInstance.subscribeObject(this, 'left'); | |
comInstance.subscribeObject(this, 'said'); | |
callback(null, {'users': this.nickList, history: this.chatLines.slice(-10)}); | |
this.emit('joined', 'room', {'nick': nick}); | |
this.nickList.push(nick); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment