Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added ping

  • Loading branch information...
commit f18b28961099dc8984f4e0a8f3058dc4a5eb8777 1 parent 28f1029
Pat Patterson authored

Showing 2 changed files with 51 additions and 6 deletions. Show diff stats Hide diff stats

  1. 24  client.js
  2. 33  server.js
24  client.js
@@ -4,6 +4,8 @@ var CONFIG = { debug: false
4 4
              , last_message_time: 1
5 5
              , focus: true //event listeners bound in onConnect
6 6
              , unread: 0 //updated in the message-processing loop
  7
+             , ping_timeout: 10*1000
  8
+             , ping: null
7 9
              };
8 10
 
9 11
 var nicks = [];
@@ -142,6 +144,12 @@ function userPart(nick, timestamp) {
142 144
   updateUsersLink();
143 145
 }
144 146
 
  147
+//handles ping
  148
+function recvPing(nick, timestamp) {
  149
+  //put it in the stream
  150
+  //addMessage(nick, "ping", timestamp, "ping");
  151
+}
  152
+
145 153
 // utility functions
146 154
 
147 155
 util = {
@@ -299,6 +307,10 @@ function longPoll (data) {
299 307
         case "part":
300 308
           userPart(message.nick, message.timestamp);
301 309
           break;
  310
+          
  311
+        case "ping":
  312
+          recvPing(message.nick, message.timestamp);
  313
+          break;
302 314
       }
303 315
     }
304 316
     //update the document title to include unread message count if blurred
@@ -325,6 +337,7 @@ function longPoll (data) {
325 337
            }
326 338
          , success: function (data) {
327 339
              transmission_errors = 0;
  340
+             clearTimeout(ping);
328 341
              //if everything went well, begin another request immediately
329 342
              //the server will take a long time to respond
330 343
              //how long? well, it will wait until there is another message
@@ -333,6 +346,9 @@ function longPoll (data) {
333 346
              longPoll(data);
334 347
            }
335 348
          });
  349
+
  350
+  // Send a ping if we don't get a response within CONFIG.ping_timeout
  351
+  ping = setTimeout(sendPing, CONFIG.ping_timeout);       
336 352
 }
337 353
 
338 354
 //submit a new message to the server
@@ -344,6 +360,14 @@ function send(msg) {
344 360
   }
345 361
 }
346 362
 
  363
+function sendPing() {
  364
+    if (CONFIG.debug === false) {
  365
+      // XXX should be POST
  366
+      // XXX should add to messages immediately
  367
+      jQuery.get("/ping", {id: CONFIG.id}, function (data) { }, "json");
  368
+    }    
  369
+}
  370
+
347 371
 //Transition the page to the state that prompts the user for a nickname
348 372
 function showConnect () {
349 373
   $("#connect").show();
33  server.js
@@ -29,12 +29,12 @@ client.query("CREATE TABLE chat_messages(nick varchar(50), type varchar(10), tex
29 29
     function(err, result) {
30 30
         if (err) {
31 31
             if (err.code === '42P07') {
32  
-                console.log("chat_messages table already exists");
  32
+                sys.puts("chat_messages table already exists");
33 33
             } else {
34  
-                console.log(err);
  34
+                sys.puts(err);
35 35
             }
36 36
         } else {
37  
-            console.log("created chat_messages table");
  37
+            sys.puts("created chat_messages table");
38 38
         }
39 39
     });
40 40
 
@@ -45,13 +45,13 @@ var channel = new function () {
45 45
   var query = client.query("SELECT * FROM chat_messages ORDER BY timestamp LIMIT $1", [MESSAGE_BACKLOG]);
46 46
 
47 47
   query.on('row', function(row) {
48  
-    console.log(row);
  48
+    sys.puts(sys.inspect(row));
49 49
     messages.push(row);
50 50
   });
51 51
   
52 52
   this.appendMessage = function (nick, type, text) {
53 53
     var m = { nick: nick
54  
-            , type: type // "msg", "join", "part"
  54
+            , type: type // "msg", "join", "part", "ping"
55 55
             , text: text
56 56
             , timestamp: (new Date()).getTime()
57 57
             };
@@ -66,10 +66,16 @@ var channel = new function () {
66 66
       case "part":
67 67
         sys.puts(nick + " part");
68 68
         break;
  69
+      case "ping":
  70
+        sys.puts(nick + " ping");
  71
+        break;
69 72
     }
70 73
 
71 74
     messages.push( m );
72  
-    client.query("INSERT INTO chat_messages(nick, type, text, timestamp) values($1, $2, $3, $4)",    [m.nick, m.type, m.text, m.timestamp]);
  75
+    if (type !== "ping") {
  76
+      client.query("INSERT INTO chat_messages(nick, type, text, timestamp) values($1, $2, $3, $4)",
  77
+        [m.nick, m.type, m.text, m.timestamp]);
  78
+    }
73 79
 
74 80
     while (callbacks.length > 0) {
75 81
       callbacks.shift().callback([m]);
@@ -234,3 +240,18 @@ fu.get("/send", function (req, res) {
234 240
   channel.appendMessage(session.nick, "msg", text);
235 241
   res.simpleJSON(200, { rss: mem.rss });
236 242
 });
  243
+
  244
+fu.get("/ping", function (req, res) {
  245
+  var id = qs.parse(url.parse(req.url).query).id;
  246
+
  247
+  var session = sessions[id];
  248
+  if (!session) {
  249
+    res.simpleJSON(400, { error: "No such session id" });
  250
+    return;
  251
+  }
  252
+
  253
+  session.poke();
  254
+  
  255
+  channel.appendMessage(session.nick, "ping");
  256
+  res.simpleJSON(200, { rss: mem.rss });
  257
+});

0 notes on commit f18b289

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