Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add RSS and uptime display

  • Loading branch information...
commit 8ec8bb2bad9823ad865acd769db070b2b3f52cd8 1 parent f2d65d9
@ry ry authored
Showing with 155 additions and 11 deletions.
  1. +131 −4 client.js
  2. +2 −1  index.html
  3. +22 −6 server.js
View
135 client.js
@@ -8,6 +8,105 @@ var CONFIG = { debug: false
var nicks = [];
+// CUT ///////////////////////////////////////////////////////////////////
+/* This license and copyright apply to all code until the next "CUT"
+http://github.com/jherdman/javascript-relative-time-helpers/
+
+The MIT License
+
+Copyright (c) 2009 James F. Herdman
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
+
+ * Returns a description of this past date in relative terms.
+ * Takes an optional parameter (default: 0) setting the threshold in ms which
+ * is considered "Just now".
+ *
+ * Examples, where new Date().toString() == "Mon Nov 23 2009 17:36:51 GMT-0500 (EST)":
+ *
+ * new Date().toRelativeTime()
+ * --> 'Just now'
+ *
+ * new Date("Nov 21, 2009").toRelativeTime()
+ * --> '2 days ago'
+ *
+ * // One second ago
+ * new Date("Nov 23 2009 17:36:50 GMT-0500 (EST)").toRelativeTime()
+ * --> '1 second ago'
+ *
+ * // One second ago, now setting a now_threshold to 5 seconds
+ * new Date("Nov 23 2009 17:36:50 GMT-0500 (EST)").toRelativeTime(5000)
+ * --> 'Just now'
+ *
+ */
+Date.prototype.toRelativeTime = function(now_threshold) {
+ var delta = new Date() - this;
+
+ now_threshold = parseInt(now_threshold, 10);
+
+ if (isNaN(now_threshold)) {
+ now_threshold = 0;
+ }
+
+ if (delta <= now_threshold) {
+ return 'Just now';
+ }
+
+ var units = null;
+ var conversions = {
+ millisecond: 1, // ms -> ms
+ second: 1000, // ms -> sec
+ minute: 60, // sec -> min
+ hour: 60, // min -> hour
+ day: 24, // hour -> day
+ month: 30, // day -> month (roughly)
+ year: 12 // month -> year
+ };
+
+ for (var key in conversions) {
+ if (delta < conversions[key]) {
+ break;
+ } else {
+ units = key; // keeps track of the selected key over the iteration
+ delta = delta / conversions[key];
+ }
+ }
+
+ // pluralize a unit when the difference is greater than 1.
+ delta = Math.floor(delta);
+ if (delta !== 1) { units += "s"; }
+ return [delta, units].join(" ");
+};
+
+/*
+ * Wraps up a common pattern used with this plugin whereby you take a String
+ * representation of a Date, and want back a date object.
+ */
+Date.fromString = function(str) {
+ return new Date(Date.parse(str));
+};
+
+// CUT ///////////////////////////////////////////////////////////////////
+
+
+
//updates the users link to reflect the number of active users
function updateUsersLink ( ) {
var t = nicks.length.toString() + " user";
@@ -140,6 +239,21 @@ function addMessage (from, text, time, _class) {
scrollDown();
}
+function updateRSS () {
+ var bytes = parseInt(rss);
+ if (bytes) {
+ var megabytes = bytes / (1024*1024);
+ megabytes = Math.round(megabytes*10)/10;
+ $("#rss").text(megabytes.toString());
+ }
+}
+
+function updateUptime () {
+ if (starttime) {
+ $("#uptime").text(starttime.toRelativeTime());
+ }
+}
+
var transmission_errors = 0;
var first_poll = true;
@@ -154,6 +268,11 @@ function longPoll (data) {
return;
}
+ if (data && data.rss) {
+ rss = data.rss;
+ updateRSS();
+ }
+
//process any updates we may have
//data will be null on the first call of longPoll
if (data && data.messages) {
@@ -260,6 +379,11 @@ function updateTitle(){
}
}
+// daemon start time
+var starttime;
+// daemon memory usage
+var rss;
+
//handle the server's response to our nickname and join request
function onConnect (session) {
if (session.error) {
@@ -270,6 +394,10 @@ function onConnect (session) {
CONFIG.nick = session.nick;
CONFIG.id = session.id;
+ starttime = new Date(session.starttime);
+ rss = session.rss;
+ updateRSS();
+ updateUptime();
//update the UI to show the chat
showChat(CONFIG.nick);
@@ -350,11 +478,10 @@ $(document).ready(function() {
return false;
});
- // update the clock every second
+ // update the daemon uptime every 10 seconds
setInterval(function () {
- var now = new Date();
- $("#currentTime").text(util.timeString(now));
- }, 1000);
+ updateUptime();
+ }, 10*1000);
if (CONFIG.debug) {
$("#loading").hide();
View
3  index.html
@@ -179,8 +179,9 @@
</div>
<div id="toolbar">
<ul id="status">
- <li class="date" id="currentTime">12:59</li>
<li><a id="usersLink" href="#">5 users</a></li>
+ <li>uptime: <span id="uptime">?</span></li>
+ <li>memory: <span id="rss">?</span>mb RSS</li>
</ul>
<input tabindex="1" type="text" id="entry"/>
</div>
View
28 server.js
@@ -1,6 +1,16 @@
HOST = null; // localhost
PORT = 8001;
+// when the daemon started
+var starttime = (new Date()).getTime();
+
+var mem = process.memoryUsage();
+// every 10 seconds poll for the memory.
+setInterval(function () {
+ mem = process.memoryUsage();
+}, 10*1000);
+
+
var fu = require("./fu"),
sys = require("sys"),
url = require("url"),
@@ -125,7 +135,9 @@ fu.get("/who", function (req, res) {
var session = sessions[id];
nicks.push(session.nick);
}
- res.simpleJSON(200, { nicks: nicks });
+ res.simpleJSON(200, { nicks: nicks
+ , rss: mem.rss
+ });
});
fu.get("/join", function (req, res) {
@@ -143,7 +155,11 @@ fu.get("/join", function (req, res) {
//sys.puts("connection: " + nick + "@" + res.connection.remoteAddress);
channel.appendMessage(session.nick, "join");
- res.simpleJSON(200, { id: session.id, nick: session.nick});
+ res.simpleJSON(200, { id: session.id
+ , nick: session.nick
+ , rss: mem.rss
+ , starttime: starttime
+ });
});
fu.get("/part", function (req, res) {
@@ -153,7 +169,7 @@ fu.get("/part", function (req, res) {
session = sessions[id];
session.destroy();
}
- res.simpleJSON(200, { });
+ res.simpleJSON(200, { rss: mem.rss });
});
fu.get("/recv", function (req, res) {
@@ -172,7 +188,7 @@ fu.get("/recv", function (req, res) {
channel.query(since, function (messages) {
if (session) session.poke();
- res.simpleJSON(200, { messages: messages });
+ res.simpleJSON(200, { messages: messages, rss: mem.rss });
});
});
@@ -183,11 +199,11 @@ fu.get("/send", function (req, res) {
var session = sessions[id];
if (!session || !text) {
res.simpleJSON(400, { error: "No such session id" });
- return;
+ return;
}
session.poke();
channel.appendMessage(session.nick, "msg", text);
- res.simpleJSON(200, {});
+ res.simpleJSON(200, { rss: mem.rss });
});
Please sign in to comment.
Something went wrong with that request. Please try again.