Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Pulled all files related to demo out of chat module.

  • Loading branch information...
commit a67ba34c21389003ac1ef3d118a0fc7a3ac7a417 1 parent 1b4c717
@scottgonzalez authored
Showing with 87 additions and 82 deletions.
  1. +9 −2 demo/chat.js
  2. +78 −80 lib/server.js
View
11 demo/chat.js
@@ -1,3 +1,10 @@
-var chat = require('../lib/server');
+var chat = require('../lib/server'),
+ router = require("../lib/router");
-chat.createServer().listen(8001);
+var chatServer = chat.createServer();
+chatServer.listen(8001);
+httpServer = chatServer.httpServer;
+httpServer.get("/", router.staticHandler("index.html"));
+httpServer.get("/style.css", router.staticHandler("style.css"));
+httpServer.get("/client.js", router.staticHandler("client.js"));
+httpServer.get("/jquery-1.2.6.min.js", router.staticHandler("jquery-1.2.6.min.js"));
View
158 lib/server.js
@@ -1,4 +1,4 @@
-var fu = require("./fu"),
+var router = require("./router"),
sys = require("sys"),
url = require("url"),
qs = require("querystring"),
@@ -7,11 +7,16 @@ var fu = require("./fu"),
// TODO: message backlog limit
// TODO: session timeouts
// TODO: named channels
-// TODO: rewrite fu to have instances
-
var servers = [];
function Server() {
+ var httpServer = router.createServer();
+ handlers.forEach(function(handler) {
+ httpServer.get(handler.path, handler.handler);
+ });
+
+ this.httpServer = httpServer;
+
// TODO: don't auto-add a channel
this.channels = [];
this.addChannel();
@@ -19,7 +24,7 @@ function Server() {
process.mixin(Server.prototype, {
listen: function(port, host) {
- fu.listen(port, host);
+ this.httpServer.listen(port, host);
},
addChannel: function() {
@@ -48,84 +53,77 @@ exports.createServer = function() {
-fu.get("/", fu.staticHandler("index.html"));
-fu.get("/style.css", fu.staticHandler("style.css"));
-fu.get("/client.js", fu.staticHandler("client.js"));
-fu.get("/jquery-1.2.6.min.js", fu.staticHandler("jquery-1.2.6.min.js"));
-
-fu.get("/who", function(request, response) {
- var nicks = [];
- var sessions = servers[0].channels[0].sessions;
- for (var id in sessions) {
- nicks.push(sessions[id].nick);
- }
- response.simpleJSON(200, { nicks: nicks });
-});
-
-fu.get("/join", function(request, response) {
- var nick = qs.parse(url.parse(request.url).query).nick;
- var channel = servers[0].channels[0];
- if (!nick) {
- response.simpleJSON(400, { error: "bad nick." });
- return;
- }
- var session = channel.createSession(nick);
- if (!session) {
- response.simpleJSON(400, { error: "nick in use." });
- return;
- }
+var handlers = [
+ { path: "/who", handler: function(request, response) {
+ var nicks = [];
+ var sessions = servers[0].channels[0].sessions;
+ for (var id in sessions) {
+ nicks.push(sessions[id].nick);
+ }
+ response.simpleJSON(200, { nicks: nicks });
+ } },
+ { path: "/join", handler: function(request, response) {
+ var nick = qs.parse(url.parse(request.url).query).nick;
+ var channel = servers[0].channels[0];
+ if (!nick) {
+ response.simpleJSON(400, { error: "bad nick." });
+ return;
+ }
+ var session = channel.createSession(nick);
+ if (!session) {
+ response.simpleJSON(400, { error: "nick in use." });
+ return;
+ }
+
+ channel.appendMessage(nick, "join");
+ response.simpleJSON(200, { id: session.id, nick: nick });
+ } },
+ { path: "/part", handler: function(request, response) {
+ var id = qs.parse(url.parse(request.url).query).id;
+ var channel = servers[0].channels[0];
+ // TODO: can we remove the check and just always call destroySession()?
+ if (id && channel.sessions[id]) {
+ channel.destroySession(id);
+ }
+ response.simpleJSON(200, {});
+ } },
+ { path: "/recv", handler: function(request, response) {
+ var query = qs.parse(url.parse(request.url).query),
+ since = query.since,
+ id = query.id,
+ session;
+ var channel = servers[0].channels[0];
+ if (!since) {
+ response.simpleJSON(400, { error: "Must supply since parameter." });
+ return;
+ }
- channel.appendMessage(nick, "join");
- response.simpleJSON(200, { id: session.id, nick: nick });
-});
-
-fu.get("/part", function(request, response) {
- var id = qs.parse(url.parse(request.url).query).id;
- var channel = servers[0].channels[0];
- // TODO: can we remove the check and just always call destroySession()?
- if (id && channel.sessions[id]) {
- channel.destroySession(id);
- }
- response.simpleJSON(200, {});
-});
-
-fu.get("/recv", function(request, response) {
- var query = qs.parse(url.parse(request.url).query),
- since = query.since,
- id = query.id,
- session;
- var channel = servers[0].channels[0];
- if (!since) {
- response.simpleJSON(400, { error: "Must supply since parameter." });
- return;
- }
-
- since = parseInt(since, 10);
- session = channel.sessions[id];
- if (session) session.poke();
- channel.query(since, function(messages) {
+ since = parseInt(since, 10);
+ session = channel.sessions[id];
if (session) session.poke();
- response.simpleJSON(200, { messages: messages });
- });
-});
-
-fu.get("/send", function(request, response) {
- var query = qs.parse(url.parse(request.url).query),
- id = query.id,
- text = query.text;
-
- var channel = servers[0].channels[0];
- var session = channel.sessions[id];
- if (!session || !text) {
- response.simpleJSON(400, { error: "No such session id." });
- return;
- }
-
- session.poke();
-
- channel.appendMessage(session.nick, "msg", text);
- response.simpleJSON(200, {});
-});
+ channel.query(since, function(messages) {
+ if (session) session.poke();
+ response.simpleJSON(200, { messages: messages });
+ });
+ } },
+ { path: "/send", handler: function(request, response) {
+ var query = qs.parse(url.parse(request.url).query),
+ id = query.id,
+ text = query.text;
+
+ var channel = servers[0].channels[0];
+ var session = channel.sessions[id];
+ if (!session || !text) {
+ response.simpleJSON(400, { error: "No such session id." });
+ return;
+ }
+
+ session.poke();
+
+ channel.appendMessage(session.nick, "msg", text);
+ response.simpleJSON(200, {});
+ } }
+];
Please sign in to comment.
Something went wrong with that request. Please try again.