Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add join/part messages.

  • Loading branch information...
commit f91e7d8bb92e430c3f0c17da85ea7034bfe041ad 1 parent 2c48678
@ry ry authored
Showing with 62 additions and 23 deletions.
  1. +25 −6 client.js
  2. +1 −1  index.html
  3. +36 −16 server.js
View
31 client.js
@@ -143,7 +143,19 @@ function longPoll (data) {
if (message.timestamp > CONFIG.last_message_time)
CONFIG.last_message_time = message.timestamp;
- addMessage(message.nick, message.text);
+ switch (message.type) {
+ case "msg":
+ addMessage(message.nick, message.text, message.timestamp);
+ break;
+
+ case "join":
+ addMessage(message.nick, "joined", message.timestamp, "join");
+ break;
+
+ case "part":
+ addMessage(message.nick, "left", message.timestamp, "part");
+ break;
+ }
}
}
}
@@ -188,11 +200,14 @@ function showLoad () {
$("#toolbar").hide();
}
-function showChat () {
- $("#connect").hide();
- $("#loading").hide();
+function showChat (nick) {
+ $("#nick").text(nick);
$("#toolbar").show();
$("#entry").focus();
+
+ $("#connect").hide();
+ $("#loading").hide();
+
scrollDown();
}
@@ -206,9 +221,13 @@ function onConnect (session) {
CONFIG.nick = session.nick;
CONFIG.id = session.id;
- showChat();
+ showChat(CONFIG.nick);
}
+$(window).unload(function () {
+ jQuery.get("/part", {id: CONFIG.id}, function (data) { }, "json");
+});
+
$(document).ready(function() {
$("#entry").keypress(handleKeyPress);
@@ -220,7 +239,7 @@ $(document).ready(function() {
$.ajax({ cache: false
, type: "GET" // XXX should be POST
, dataType: "json"
- , url: "/connect"
+ , url: "/join"
, data: { nick: nick }
, error: function () {
alert("error connecting to server");
View
2  index.html
@@ -201,7 +201,7 @@
<div id="toolbar">
<ul id="status">
<li class="date" id="currentTime">12:59</li>
- <li>node chat</li>
+ <li id="nick">chat</li>
</ul>
<input tabindex="1" type="text" id="entry" />
</div>
View
52 server.js
@@ -8,9 +8,10 @@ var channel = new function () {
var messages = [];
var callbacks = [];
- this.appendMessage = function (nick, message) {
+ this.appendMessage = function (nick, type, text) {
var m = { nick: nick
- , text: message
+ , type: type // "msg", "join", "part"
+ , text: text
, timestamp: (new Date()).getTime()
};
@@ -54,16 +55,25 @@ var sessions = {};
function createSession (nick) {
for (var i in sessions) {
var session = sessions[i];
- if (session && session.nick === nick)
- return null;
- }
- var session = { nick: nick
- , id: Math.floor(Math.random()*99999999999).toString()
- , timestamp: new Date()
- };
- session.poke = function () {
- session.timestamp = new Date();
+ if (session && session.nick === nick) return null;
}
+
+ var session = {
+ nick: nick,
+
+ id: Math.floor(Math.random()*99999999999).toString(),
+
+ timestamp: new Date(),
+
+ poke: function () {
+ session.timestamp = new Date();
+ },
+
+ destroy: function () {
+ delete sessions[session.id];
+ }
+ };
+
sessions[session.id] = session;
return session;
}
@@ -76,7 +86,7 @@ setInterval(function () {
var session = sessions[id];
if (now - session.timestamp > SESSION_TIMEOUT) {
- delete sessions[id];
+ session.destroy();
}
}
}, 1000);
@@ -89,22 +99,32 @@ function onLoad () {
fu.get("/client.js", fu.staticHandler("client.js"));
fu.get("/jquery-1.2.6.min.js", fu.staticHandler("jquery-1.2.6.min.js"));
- fu.get("/connect", function (req, res) {
+ fu.get("/join", function (req, res) {
var nick = req.uri.params["nick"];
if (nick == null || nick.length == 0) {
res.simpleJSON(400, {error: "Bad nick."});
return;
}
-
var session = createSession(nick);
if (session == null) {
res.simpleJSON(400, {error: "Nick in use"});
return;
}
-
+ channel.appendMessage(session.nick, "join");
res.simpleJSON(200, { id: session.id, nick: session.nick});
});
+ fu.get("/part", function (req, res) {
+ var id = req.uri.params.id;
+ var session;
+ if (id && sessions[id]) {
+ session = sessions[id];
+ channel.appendMessage(session.nick, "part");
+ session.destroy();
+ }
+ res.simpleJSON(200, { });
+ });
+
fu.get("/recv", function (req, res) {
if (!req.uri.params.since) {
res.simpleJSON(400, { error: "Must supply since parameter" });
@@ -137,7 +157,7 @@ function onLoad () {
session.poke();
- channel.appendMessage(session.nick, text);
+ channel.appendMessage(session.nick, "msg", text);
res.simpleJSON(200, {});
});
}
Please sign in to comment.
Something went wrong with that request. Please try again.