Skip to content

Instantly share code, notes, and snippets.

@maryrosecook
Created March 18, 2010 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maryrosecook/336937 to your computer and use it in GitHub Desktop.
Save maryrosecook/336937 to your computer and use it in GitHub Desktop.
var getMap = {};
fu.get = function (path, handler) {
getMap[path] = handler;
};
var server = createServer(function (req, res) {
if (req.method === "GET" || req.method === "HEAD") {
var handler = getMap[url.parse(req.url).pathname] || notFound;
res.simpleText = function (code, body) {
res.sendHeader(code, [ ["Content-Type", "text/plain"]
, ["Content-Length", body.length]
]);
res.write(body);
res.close();
};
res.simpleJSON = function (code, obj) {
var body = JSON.stringify(obj);
res.sendHeader(code, [ ["Content-Type", "text/json"]
, ["Content-Length", body.length]
]);
res.write(body);
res.close();
};
handler(req, res);
}
});
fu.listen = function (port, host) {
server.listen(port, host);
};
fu.get("/latest_message", function (req, res) {
var since = parseInt(qs.parse(url.parse(req.url).query).since);
if(since < latestMessageReceived) // new message since client last requested
sendLatestMessageToClient(res); // send it straight to them
else
messageRequests.push({ callback: sendLatestMessageToClient, res: res }); // queue up the requst
});
var transmission_errors = 0;
var lastMessageReceived = 0; // a long time ago
function longPoll (data) {
if (data && data.message) {
updateMessage(data.message);
lastMessageReceived = new Date().getTime();
}
$.ajax({ cache: false,
type: "GET",
url: "/latest_message",
dataType: "json",
data: { since: lastMessageReceived },
error: function () {
transmission_errors += 1;
setTimeout(longPoll, 10*1000);
},
success: function (data) {
setTimeout(function () {
transmission_errors = 0;
longPoll(data);
}, 100);
}
});
}
var latestMessageReceived = new Date().getTime();
fu.get("/send_message", function (req, res) {
var message = qs.parse(url.parse(req.url).query).message;
storeMessage(message, function() {
latestMessageReceived = new Date().getTime();
res.simpleJSON(200, {});
// respond to queued message requests from clients now that we have a new message to send out
while (messageRequests.length > 0)
{
var messageRequest = messageRequests.shift();
messageRequest.callback(messageRequest.res);
}
});
});
function sendLatestMessageToClient(res) {
var redisClient = new redis.createClient();
redisClient.stream.addListener("connect", function () {
redisClient.lindex('messages', 0, function (err, value) {
res.simpleJSON(200, { message: value.toString(), timestamp: new Date().getTime() });
redisClient.close();
});
});
}
function storeMessage(message, callback) {
var redisClient = new redis.createClient();
redisClient.stream.addListener("connect", function () {
redisClient.lpush('messages', message, function (err, value) {
redisClient.close();
callback();
});
});
}
function tryToSendMessage(message) {
jQuery.get("/unique_chalk",
{ message: message },
function (data) {
if(data && data.message) {
jQuery.get("/send_message", { message: message }, function (data) {} , "json");
updateErrorMessage("");
$("#message_field").attr("value", "");
}
else
updateErrorMessage("said before");
}, "json");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment