diff --git a/json-chat-nodejs/.gitignore b/json-chat-nodejs/.gitignore new file mode 100644 index 0000000..df1adac --- /dev/null +++ b/json-chat-nodejs/.gitignore @@ -0,0 +1,3 @@ +node_modules +ender.js +ender.min.js diff --git a/json-chat-nodejs/README.md b/json-chat-nodejs/README.md new file mode 100644 index 0000000..1c83930 --- /dev/null +++ b/json-chat-nodejs/README.md @@ -0,0 +1,31 @@ +HTTP Chat Engine +=== + +Implement a chat server that only stores messages for 60 seconds. + +Clients should be able to query for messages in ranges from `since` to `until` (in ms). + +If `since` is omitted, the server should assume `since=0` + +If `until` is in the past, the server should assume `until={now}` (and respond immediately). + +If `until` is omitted, the server should respond the next time a message is sent to that room. + +If `until` is in the future, the server should not respond until the appointed time, including all new messages in the room. + +Node.JS Boilerplate: +=== + + npm install -g spark + + cd ~/http-chat-nodejs + npm install # reads in package.json for deps + spark & # starts server.js according to config.js + +Ender.JS Boilerplate: +=== + + npm install -g ender + + cd ~/http-chat-nodejs/public + ender build jeesh querystring ahr2 diff --git a/json-chat-nodejs/config.js b/json-chat-nodejs/config.js new file mode 100644 index 0000000..d305e23 --- /dev/null +++ b/json-chat-nodejs/config.js @@ -0,0 +1,11 @@ +(function () { + "use strict"; + + var config + + config = { + port: 5080 + }; + + module.exports = config; +}()); diff --git a/json-chat-nodejs/package.json b/json-chat-nodejs/package.json new file mode 100644 index 0000000..9f54a6f --- /dev/null +++ b/json-chat-nodejs/package.json @@ -0,0 +1,17 @@ +{ + "author": "AJ ONeal (http://coolaj86.info)", + "name": "json-chat", + "description": "A simple chat server with message queueing capabilities", + "version": "0.0.0", + "repository": { + "type": "git", + "url": "git://github.com/coolaj86/fizzbuzz.git" + }, + "engines": { + "node": "~v0.4.8" + }, + "dependencies": { + "connect": ">= 1.0.0" + }, + "devDependencies": {} +} diff --git a/json-chat-nodejs/public/app.js b/json-chat-nodejs/public/app.js new file mode 100644 index 0000000..db2a7d6 --- /dev/null +++ b/json-chat-nodejs/public/app.js @@ -0,0 +1,47 @@ +(function () { + "use strict"; + + var $ = require('ender') + , request = require('ahr2') + ; + + function assignHandlers() { + // do long-polling to get new chat messages + function getNewMessages() { + + request({ + "method": "GET" + , "href": "/RoomA" + , "query": { + since: new Date().valueOf() - (10 * 1000) + , ignoreme: 'foo' + } + }).when(function (err, ahr, data) { + console.log('should implement a GET request here'); + setTimeout(getNewMessages, 10 * 1000); + }); + } + getNewMessages(); + + // Assign the event handler on the form + $('body').delegate('#chatform form', 'submit', function (ev) { + ev.preventDefault(); + + request({ + "method": "POST" + , "href": "/RoomA" + , "body": { + name: $("#chatform [name=name]").val() + , message: $("#chatform [name=message]").val() + } + , "headers": { "Content-Type": "application/json" } + }).when(function (err, ahr, data) { + alert('you clicked the submit button (or perhaps hit enter)'); + console.log(err, ahr, data); + }); + }); + } + + // wait until the dom is ready to do stuff with it + $.domReady(assignHandlers); +}()); diff --git a/json-chat-nodejs/public/index.html b/json-chat-nodejs/public/index.html new file mode 100644 index 0000000..cc940c2 --- /dev/null +++ b/json-chat-nodejs/public/index.html @@ -0,0 +1,29 @@ + + + + + + + +
+ Display the messages here. +
+
+
+ Name: + +
+ + Room: + +
+ + Message: + +
+ + +
+
+ + diff --git a/json-chat-nodejs/server.js b/json-chat-nodejs/server.js new file mode 100644 index 0000000..e5a0921 --- /dev/null +++ b/json-chat-nodejs/server.js @@ -0,0 +1,15 @@ +(function () { + "use strict"; + + var connect = require('connect') + , server + ; + + server = connect.createServer( + connect.favicon() + , connect.static(__dirname + '/public') + // implement chat storage engine + ); + + module.exports = server; +}()); diff --git a/json-chat-nodejs/tests.sh b/json-chat-nodejs/tests.sh new file mode 100644 index 0000000..040bd8b --- /dev/null +++ b/json-chat-nodejs/tests.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +HOST=localhost:5080 + +# immediatley adds a message to RoomA +curl --silent http://${HOST}/RoomA \ + -X POST \ + -H 'Content-Type: application/json' \ + -d '{ + "name": "AJ" + , "message": "Hello World" + }' +# output should look something like +# { "error": false, "errors": [], "timestamp": 1234567890, "status": "ok"} + + + +# immediatley adds a message to RoomB +curl --silent http://${HOST}/RoomB \ + -X POST \ + -H 'Content-Type: application/json' \ + -d '{ + "name": "AJ" + , "message": "Hello Anti-World" + }' + + + +# waits 10 seconds before responding with the latest messages +# should get the message +let WHEN=`date +%s`000-10000 # 10 seconds ago +let THEN=`date +%s`000+10000 # 10 seconds from now +curl --silent http://${HOST}/RoomA?since=${WHEN}&until=${THEN} \ + -X POST \ + -H 'Content-Type: application/json' \ + -d '{ + "name": "AJ" + , "message": "Hello Anti-World" + }' \ + & +# output should look something like +# [ +# { "timestamp": 1234567890, "name": "AJ", "message": "blah..."} +# , { "timestamp": 1234567890, "name": "Eric", "message": "blah..."} +# ] + + + +# this should post to RoomA and be seen in the request above +sleep 5 +curl --silent http://${HOST}/RoomA \ + -X POST \ + -H 'Content-Type: application/json' \ + -d '{ + "name": "Eric" + , "message": "Hello Back Atcha" + }'