Created
December 11, 2009 01:30
-
-
Save jazzychad/253902 to your computer and use it in GitHub Desktop.
This file contains 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
*** simplechat_orig.js 2009-12-10 17:20:47.000000000 -0500 | |
--- chatevents.js 2009-12-10 17:20:26.000000000 -0500 | |
*************** | |
*** 1,6 **** | |
--- 1,8 ---- | |
var tcp = require("tcp"); | |
var sys = require("sys"); | |
+ var chatEmitter = new process.EventEmitter(); | |
+ | |
Array.prototype.remove = function(e) { | |
for(var i = 0; i < this.length; i++) | |
if(e == this[i]) this.splice(i, 1); | |
*************** Array.prototype.each = function(fn) { | |
*** 12,25 **** | |
function Client(connection) { | |
this.name = null; | |
this.connection = connection; | |
} | |
- var clients = []; | |
- | |
var server = tcp.createServer(function (socket) { | |
var client = new Client(socket); | |
! clients.push(client); | |
socket.setTimeout(0); | |
socket.setEncoding("utf8"); | |
--- 14,35 ---- | |
function Client(connection) { | |
this.name = null; | |
+ this.listener = null; | |
this.connection = connection; | |
} | |
var server = tcp.createServer(function (socket) { | |
var client = new Client(socket); | |
! | |
! var chatListener = function(c, msg) { | |
! if (c != client) { | |
! client.connection.send(msg); | |
! } | |
! } | |
! | |
! chatEmitter.addListener("chat", chatListener); | |
! client.listener = chatListener; | |
! | |
socket.setTimeout(0); | |
socket.setEncoding("utf8"); | |
*************** var server = tcp.createServer(function ( | |
*** 32,50 **** | |
if (client.name == null) { | |
client.name = data.match(/\S+/); | |
socket.send("===========\n"); | |
! clients.each(function(c) { | |
! if (c != client) | |
! c.connection.send(client.name + " has joined.\n"); | |
! }); | |
return; | |
} | |
var command = data.match(/^\/(.*)/); | |
if (command) { | |
if (command[1] == 'users') { | |
! clients.each(function(c) { | |
! socket.send("- " + c.name + "\n"); | |
! }); | |
} | |
else if (command[1] == 'quit') { | |
socket.close(); | |
--- 42,55 ---- | |
if (client.name == null) { | |
client.name = data.match(/\S+/); | |
socket.send("===========\n"); | |
! chatEmitter.emit("chat", client, client.name + " has joined.\n"); | |
return; | |
} | |
var command = data.match(/^\/(.*)/); | |
if (command) { | |
if (command[1] == 'users') { | |
! chatEmitter.emit("chat", client, "- " + c.name + "\n"); | |
} | |
else if (command[1] == 'quit') { | |
socket.close(); | |
*************** var server = tcp.createServer(function ( | |
*** 52,61 **** | |
return; | |
} | |
! clients.each(function(c) { | |
! if (c != client) | |
! c.connection.send(client.name + ": " + data); | |
! }); | |
}); | |
socket.addListener("eof", function() { | |
--- 57,63 ---- | |
return; | |
} | |
! chatEmitter.emit("chat", client, client.name + ": " + data); | |
}); | |
socket.addListener("eof", function() { | |
*************** var server = tcp.createServer(function ( | |
*** 63,73 **** | |
}); | |
socket.addListener("close", function() { | |
! clients.remove(client); | |
! | |
! clients.each(function(c) { | |
! c.connection.send(client.name + " has left.\n"); | |
! }); | |
}); | |
}); | |
--- 65,73 ---- | |
}); | |
socket.addListener("close", function() { | |
! chatEmitter.removeListener("chat", client.listener); | |
! chatEmitter.emit("chat", client, client.name + " has left.\n"); | |
! client = null; | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment