Skip to content

Instantly share code, notes, and snippets.

@scottgonzalez
Created May 19, 2010 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save scottgonzalez/406554 to your computer and use it in GitHub Desktop.
Save scottgonzalez/406554 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
var sys = require("sys"),
fs = require("fs"),
chat = require('../lib/server'),
router = require("../lib/router");
// create chat server
var chatServer = chat.createServer();
chatServer.listen(8001);
// create a channel and log all activity to stdout
var channel = chatServer.addChannel({
basePath: "/chat",
sessionTimeout: 4
}).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");
}).addListener("msg", function(msg) {
if (msg.text == "start") {
process.nextTick(proxy);
}
});
function proxy() {
var user = channel.createSession("proxy"),
count = 0,
interval = setInterval(function() {
channel.appendMessage(user.nick, "msg", "hello " + (count++));
if (count == 5) {
process.nextTick(function() {
channel.destroySession(user.id);
clearInterval(interval);
});
}
}, 5000);
Object.defineProperty(user, "timestamp", {
get: function() {
return new Date();
},
set: function() {}
});
}
// server static web files
function serveFiles(localDir, webDir) {
fs.readdirSync(localDir).forEach(function(file) {
var local = localDir + "/" + file,
web = webDir + "/" + file;
if (fs.statSync(local).isDirectory()) {
serveFiles(local, web);
} else {
chatServer.passThru(web, router.staticHandler(local));
}
});
}
serveFiles(__dirname + "/web", "");
chatServer.passThru("/", router.staticHandler(__dirname + "/web/index.html"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment