Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Removed event prefixes in client code.

Added events to channel objects.
Moved stdout logging out of channels and into the demo.
  • Loading branch information...
commit 00ce2f57ccc711d27569ce55cf7f79f919695b36 1 parent 5da94b4
@scottgonzalez authored
View
14 demo/chat.js
@@ -1,10 +1,20 @@
-var chat = require('../lib/server'),
+var sys = require("sys"),
+ chat = require('../lib/server'),
router = require("../lib/router");
// create chat server and a single channel
var chatServer = chat.createServer();
chatServer.listen(8001);
-chatServer.addChannel({ basePath: "/chat" });
+
+chatServer.addChannel({
+ basePath: "/chat"
+}).addListener("msg", function(msg) {
+ sys.puts("<" + msg.nick + "> " + msg.text);
+}).addListener("join", function(msg) {
+ sys.puts(msg.nick + " join");
+}).addListener("part", function(msg) {
+ sys.puts(msg.nick + " part");
+});
// chat app
chatServer.passThru("/", router.staticHandler("web/index.html"));
View
14 demo/web/js/client.js
@@ -34,7 +34,7 @@ $(function() {
// new message posted to channel
// - add to the chat log
-$(channel).bind("nodechat-msg", function(event, message) {
+$(channel).bind("msg", function(event, message) {
var time = formatTime(message.timestamp),
row = $("<div></div>")
.addClass("chat-msg");
@@ -58,7 +58,7 @@ $(channel).bind("nodechat-msg", function(event, message) {
})
// another user joined the channel
// - add to the chat log
-.bind("nodechat-join", function(event, message) {
+.bind("join", function(event, message) {
var time = formatTime(message.timestamp),
row = $("<div></div>")
.addClass("chat-msg chat-system-msg");
@@ -82,7 +82,7 @@ $(channel).bind("nodechat-msg", function(event, message) {
})
// another user joined the channel
// - add to the user list
-.bind("nodechat-join", function(event, message) {
+.bind("join", function(event, message) {
var added = false,
nick = $("<li></li>", {
"class": colors[0],
@@ -106,7 +106,7 @@ $(channel).bind("nodechat-msg", function(event, message) {
})
// another user left the channel
// - add to the chat log
-.bind("nodechat-part", function(event, message) {
+.bind("part", function(event, message) {
var time = formatTime(message.timestamp),
row = $("<div></div>")
.addClass("chat-msg chat-system-msg");
@@ -130,7 +130,7 @@ $(channel).bind("nodechat-msg", function(event, message) {
})
// another user left the channel
// - remove from the user list
-.bind("nodechat-part", function(event, message) {
+.bind("part", function(event, message) {
$("#users > li").each(function() {
if (this.innerHTML == message.nick) {
$(this).remove();
@@ -140,7 +140,7 @@ $(channel).bind("nodechat-msg", function(event, message) {
})
// Auto scroll list to bottom
-.bind("nodechat-join nodechat-part nodechat-msg", function(){
+.bind("join part msg", function(){
window.setTimeout(function() {
log.scrollTop(log[0].scrollHeight);
}, 10);
@@ -209,7 +209,7 @@ $(function() {
document.title = "node chat";
});
- $(channel).bind("nodechat-msg", function(event, message) {
+ $(channel).bind("msg", function(event, message) {
if (!focused) {
unread++;
document.title = "(" + unread + ") node chat";
View
5 demo/web/js/nodechat.js
@@ -28,7 +28,7 @@ $.extend(Channel.prototype, {
poll: function() {
// TODO: get error handling to work
if (this.pollingErrors > 2) {
- $(this).trigger("nodechat-connectionerror");
+ $(this).triggerHandler("connectionerror");
return;
}
var channel = this;
@@ -51,8 +51,7 @@ $.extend(Channel.prototype, {
if (data && data.messages) {
$.each(data.messages, function(i, message) {
channel.lastMessageTime = Math.max(channel.lastMessageTime, message.timestamp);
- // TODO: don't prefix events if triggering on channel instance
- $(channel).trigger("nodechat-" + message.type, message);
+ $(channel).triggerHandler(message.type, message);
});
}
this.poll();
View
20 lib/channel.js
@@ -1,7 +1,10 @@
-var sys = require("sys"),
+var EventEmitter = require("events").EventEmitter,
+ sys = require("sys"),
Session = require("./session").Session;
function Channel(options) {
+ EventEmitter.call(this);
+
// TODO: handle invalid options
options = options || {};
this.basePath = options.basePath;
@@ -12,6 +15,7 @@ function Channel(options) {
this.callbacks = [];
this.sessions = {};
}
+sys.inherits(Channel, EventEmitter);
extend(Channel.prototype, {
appendMessage: function(nick, type, text) {
@@ -22,25 +26,13 @@ extend(Channel.prototype, {
timestamp: (new Date()).getTime()
};
this.messages.push(message);
-
- switch (type) {
- case "msg":
- sys.puts("<" + nick + "> " + text);
- break;
- case "join":
- sys.puts(nick + " join");
- break;
- case "part":
- sys.puts(nick + " part");
- break;
- }
+ this.emit(type, message);
while (this.callbacks.length > 0) {
this.callbacks.shift().callback([message]);
}
while (this.messages.length > this.messageBacklog) {
- sys.puts("shifing message");
this.messages.shift();
}
},
View
2  lib/server.js
@@ -29,6 +29,8 @@ extend(Server.prototype, {
httpServer.get(channel.basePath + handler.path,
handler.handler.partial(channel));
});
+
+ return channel;
},
flushCallbacks: function() {
Please sign in to comment.
Something went wrong with that request. Please try again.