Skip to content
Browse files

Adding separate configuration options.

  • Loading branch information...
1 parent 13bd575 commit e4b80034c08b9080a7a0b21d8dc6362488c4060d @joshmarshall committed May 15, 2011
Showing with 59 additions and 24 deletions.
  1. +15 −18 app.js
  2. +32 −0 config.js
  3. +1 −0 public/static/css/style.css
  4. +2 −5 public/static/js/talk.js
  5. +9 −1 views/room.ejs
View
33 app.js
@@ -3,12 +3,14 @@ This is just a simple chat program that lets you create rooms
and send out links to your friends.
*/
+var sys = require("sys");
var express = require("express");
var app = express.createServer();
// Configuration
+var config = require("./config.js").configure();
app.use(express.static(__dirname + '/public'));
-app.use(express.cookieParser({"secret": "whatever1234"}));
+app.use(express.cookieParser({"secret": config.cookieSecret}));
app.use(express.bodyParser());
app.set("view engine", "ejs");
app.set("view options", { layout: false });
@@ -17,7 +19,7 @@ app.set("view options", { layout: false });
app.get("/", function(req, res) {
var xsrf = generateId();
res.cookie("_xsrf", xsrf);
- res.render("index.ejs", { xsrf: xsrf });
+ res.render("index.ejs", { xsrf: xsrf, config: config });
});
app.post("/room", function(req, res) {
@@ -42,16 +44,16 @@ app.get("/room/:id", function(req, res) {
if (!room)
// don't know, going home
return res.redirect("/");
- res.render("room.ejs", { room: room });
+ res.render("room.ejs", { room: room, config: config });
});
// Starting HTTP server
-app.listen(3000);
+app.listen(config.port);
+sys.log("Starting app on port "+config.port);
// Setting up Socket.IO
var io = require("socket.io");
-var socket = io.listen(app, { transports: [ 'htmlfile', 'xhr-multipart',
- 'xhr-polling', 'jsonp-polling' ]});
+var socket = io.listen(app, { transports: config.transports });
// Random id generator
var generateId = function() {
@@ -114,8 +116,6 @@ var Room = function(id, name) {
};
}
-var ROOMTIMEOUT = 60000 * 10; // ten minutes (if no users around)
-
// All of the chat rooms associated with this server
var Rooms = {
rooms: {},
@@ -141,15 +141,14 @@ var Rooms = {
// if necessary
checkActivity: function () {
for (roomName in Rooms.rooms) {
- if (!Rooms.rooms.hasOwnProperty(roomName))
- continue;
var room = Rooms.rooms[roomName];
+ if (!room || !room.id)
+ continue
var lastActivity = room.checkActivity();
- var expires = lastActivity + ROOMTIMEOUT;
+ var expires = lastActivity + config.roomTimeout;
var userCount = room.allUsers().length;
if (expires < new Date().getTime() && userCount == 0) {
// deleting room
- var room = Rooms.rooms[roomName];
Rooms.rooms[roomName] = null;
}
}
@@ -181,8 +180,6 @@ var User = function(client) {
};
};
-var AWAYTIMEOUT = 60000 * 5; // five minutes
-
// A list of users, with appropriate methods
var Users = function(room) {
this.room = room;
@@ -208,7 +205,7 @@ var Users = function(room) {
this[user.name] = null;
return true;
} else {
- console.log("sessions don't match -- not deleting.");
+ sys.log("sessions don't match -- not deleting.");
return false;
}
};
@@ -255,7 +252,7 @@ var Users = function(room) {
if (user.activity > mostRecent)
mostRecent = user.activity;
var diff = now - user.activity;
- if (diff > AWAYTIMEOUT) {
+ if (diff > config.awayTimout) {
if (user.status != "away") {
user.status = "away";
@@ -339,7 +336,7 @@ var Commands = {
name: user.name }
var room = user.getRoom();
if (!room) {
- console.log("No room for user?");
+ sys.log("No room for user?");
return;
}
var result = room.removeUser(user);
@@ -431,4 +428,4 @@ socket.on("connection", function(client) {
});
// Set up activity checking interval
-var activityInterval = setInterval(Rooms.checkActivity, 5000);
+var activityInterval = setInterval(Rooms.checkActivity, config.checkInterval);
View
32 config.js
@@ -0,0 +1,32 @@
+/* The Base Configuration file and startup options
+
+ONLY change these if you want them changed across ALL installations.
+(i.e., this file should be in version control). You should put specific
+deploayment options under "development.js" or "production.js", etc under
+the root folder.
+
+Those files should look like:
+
+ exports.update = function(config) {
+ config.port = 1337;
+ };
+
+*/
+
+exports.configure = function() {
+
+ this.port = 3000;
+ this.cookieSecret = "whatever1234";
+ this.awayTimeout = 60000 * 5; // five minutes
+ this.roomTimeout = 60000 * 20; // twenty minutes
+ this.checkInterval = 10000; // ten seconds
+ this.transports = [ 'htmlfile', 'xhr-multipart', 'xhr-polling',
+ 'jsonp-polling' ];
+
+ // Configure deployment settings
+ if (process.env.NODE_ENV)
+ require("./"+process.env.NODE_ENV+".js").update(this);
+
+ return this;
+};
+
View
1 public/static/css/style.css
@@ -162,6 +162,7 @@ input#message {
border: 0;
outline: 0;
background: rgba(255, 255, 255, 0.5);
+ color: #000;
width: 400px;
}
View
7 public/static/js/talk.js
@@ -236,11 +236,8 @@ var Talk = {
},
// Set up the Talk object.
- init: function(room) {
- Talk.room = room;
- var options = { transports: ['htmlfile',
- 'xhr-multipart', 'xhr-polling',
- 'jsonp-polling' ]};
+ init: function(options) {
+ Talk.room = options.room;
Talk.socket = new io.Socket(null, options);
Talk.socket.connect();
Talk.socket.on("connect", Talk.connect);
View
10 views/room.ejs
@@ -23,7 +23,15 @@
<script type="text/javascript">
$(function() {
- Talk.init("<%= room.id %>");
+ Talk.init({
+ room: "<%= room.id %>",
+ port: <%= config.port %>,
+ transports: [
+ <% for (var i=0; i<config.transports.length; i++) { %>
+ "<%= config.transports[i] %>",
+ <% } %>
+ ]
+ });
});
</script>
</body>

0 comments on commit e4b8003

Please sign in to comment.
Something went wrong with that request. Please try again.