Created
March 18, 2010 21:49
-
-
Save maryrosecook/336937 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function storeMessage(message, callback) { | |
var redisClient = new redis.createClient(); | |
redisClient.stream.addListener("connect", function () { | |
redisClient.lpush('messages', message, function (err, value) { | |
redisClient.close(); | |
callback(); | |
}); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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