Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added ping

  • Loading branch information...
commit f18b28961099dc8984f4e0a8f3058dc4a5eb8777 1 parent 28f1029
@metadaddy-sfdc authored
Showing with 51 additions and 6 deletions.
  1. +24 −0 client.js
  2. +27 −6 server.js
View
24 client.js
@@ -4,6 +4,8 @@ var CONFIG = { debug: false
, last_message_time: 1
, focus: true //event listeners bound in onConnect
, unread: 0 //updated in the message-processing loop
+ , ping_timeout: 10*1000
+ , ping: null
};
var nicks = [];
@@ -142,6 +144,12 @@ function userPart(nick, timestamp) {
updateUsersLink();
}
+//handles ping
+function recvPing(nick, timestamp) {
+ //put it in the stream
+ //addMessage(nick, "ping", timestamp, "ping");
+}
+
// utility functions
util = {
@@ -299,6 +307,10 @@ function longPoll (data) {
case "part":
userPart(message.nick, message.timestamp);
break;
+
+ case "ping":
+ recvPing(message.nick, message.timestamp);
+ break;
}
}
//update the document title to include unread message count if blurred
@@ -325,6 +337,7 @@ function longPoll (data) {
}
, success: function (data) {
transmission_errors = 0;
+ clearTimeout(ping);
//if everything went well, begin another request immediately
//the server will take a long time to respond
//how long? well, it will wait until there is another message
@@ -333,6 +346,9 @@ function longPoll (data) {
longPoll(data);
}
});
+
+ // Send a ping if we don't get a response within CONFIG.ping_timeout
+ ping = setTimeout(sendPing, CONFIG.ping_timeout);
}
//submit a new message to the server
@@ -344,6 +360,14 @@ function send(msg) {
}
}
+function sendPing() {
+ if (CONFIG.debug === false) {
+ // XXX should be POST
+ // XXX should add to messages immediately
+ jQuery.get("/ping", {id: CONFIG.id}, function (data) { }, "json");
+ }
+}
+
//Transition the page to the state that prompts the user for a nickname
function showConnect () {
$("#connect").show();
View
33 server.js
@@ -29,12 +29,12 @@ client.query("CREATE TABLE chat_messages(nick varchar(50), type varchar(10), tex
function(err, result) {
if (err) {
if (err.code === '42P07') {
- console.log("chat_messages table already exists");
+ sys.puts("chat_messages table already exists");
} else {
- console.log(err);
+ sys.puts(err);
}
} else {
- console.log("created chat_messages table");
+ sys.puts("created chat_messages table");
}
});
@@ -45,13 +45,13 @@ var channel = new function () {
var query = client.query("SELECT * FROM chat_messages ORDER BY timestamp LIMIT $1", [MESSAGE_BACKLOG]);
query.on('row', function(row) {
- console.log(row);
+ sys.puts(sys.inspect(row));
messages.push(row);
});
this.appendMessage = function (nick, type, text) {
var m = { nick: nick
- , type: type // "msg", "join", "part"
+ , type: type // "msg", "join", "part", "ping"
, text: text
, timestamp: (new Date()).getTime()
};
@@ -66,10 +66,16 @@ var channel = new function () {
case "part":
sys.puts(nick + " part");
break;
+ case "ping":
+ sys.puts(nick + " ping");
+ break;
}
messages.push( m );
- client.query("INSERT INTO chat_messages(nick, type, text, timestamp) values($1, $2, $3, $4)", [m.nick, m.type, m.text, m.timestamp]);
+ if (type !== "ping") {
+ client.query("INSERT INTO chat_messages(nick, type, text, timestamp) values($1, $2, $3, $4)",
+ [m.nick, m.type, m.text, m.timestamp]);
+ }
while (callbacks.length > 0) {
callbacks.shift().callback([m]);
@@ -234,3 +240,18 @@ fu.get("/send", function (req, res) {
channel.appendMessage(session.nick, "msg", text);
res.simpleJSON(200, { rss: mem.rss });
});
+
+fu.get("/ping", function (req, res) {
+ var id = qs.parse(url.parse(req.url).query).id;
+
+ var session = sessions[id];
+ if (!session) {
+ res.simpleJSON(400, { error: "No such session id" });
+ return;
+ }
+
+ session.poke();
+
+ channel.appendMessage(session.nick, "ping");
+ res.simpleJSON(200, { rss: mem.rss });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.