Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
90 additions
and
40 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,54 @@ | ||
var sys = require("sys"); | ||
var url = require("url"); | ||
var fs = require("fs"); | ||
var fs = require("fs"); | ||
|
||
var bind = require("./libraries/bind-js"); | ||
var srv = require("./libraries/xavlib/simple-router"); | ||
var chn = require("./libraries/xavlib/channel"); | ||
|
||
function renderRoom(channelId, name, callback) { | ||
fs.readFile("./chatroom.html", function(err, data) { | ||
if(err) { throw err; }; | ||
|
||
var channel = chn.channels[channelId] || chn.create(channelId); | ||
var context = { "room-name": channelId, "initial-info-id": channel.lastInfoId }; | ||
context["user-name"] = name; | ||
context["chat-text"] = channel.data.map(function(o) { return o.message.content; }); | ||
context["reply"] = { "send-line": function(def) { return (name ? def : ""); }, | ||
"enter-name": function(def) { return (name ? "" : def); } }; | ||
|
||
bind.to(data, context, callback); | ||
}); | ||
} | ||
|
||
var cookie = { parse: function(data) { | ||
var parsed = {}; | ||
(data || "").replace(/(\S*)=(\S*)(;\s*|$)/g, function(_, key, val) { parsed[key] = val; return _; }); | ||
return parsed; | ||
} }; | ||
|
||
srv.urls["/client.js"] = srv.staticFileHandler("./libraries/xavlib/channel/client.js", "application/x-javascript"); | ||
|
||
srv.urls["/json2.js"] = srv.staticFileHandler("./libraries/json2.js", "application/x-javascript"); | ||
|
||
srv.urls["/"] = srv.urls["/index.html"] = srv.staticFileHandler("./index.html", "text/html"); | ||
|
||
(function() { | ||
var regChannel = new RegExp("^/([a-zA-Z0-9_-]+)$"); | ||
srv.patterns.push({ | ||
test: function(req) { return regChannel.test(url.parse(req.url).pathname); }, | ||
handler: function(req, res) { | ||
var name = cookie.parse(req.headers["cookie"])["name"]; | ||
var channelId = regChannel.exec(url.parse(req.url).pathname)[1]; | ||
renderRoom(channelId, name, function(data) { | ||
res.sendHeader(200, { "Conent-Length": data.length, | ||
"Content-Type": "text/html", | ||
"Set-Cookie": "name=" + name + "; path=/;" }); | ||
res.end(data, "utf8"); | ||
}); | ||
} | ||
}); | ||
})() | ||
|
||
srv.server.listen(8002); | ||
chn.start(srv); |