added chat specification

This commit is contained in:
AJ ONeal 2011-08-15 14:46:18 -06:00
parent cb4b8e64c4
commit 60466f33ba
8 changed files with 210 additions and 0 deletions

3
json-chat-nodejs/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
node_modules
ender.js
ender.min.js

View File

@ -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

View File

@ -0,0 +1,11 @@
(function () {
"use strict";
var config
config = {
port: 5080
};
module.exports = config;
}());

View File

@ -0,0 +1,17 @@
{
"author": "AJ ONeal <coolaj86@gmail.com> (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": {}
}

View File

@ -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);
}());

View File

@ -0,0 +1,29 @@
<!DOCTYPE html>
<html>
<head>
<script src="ender.js"></script>
<script src="app.js"></script>
</head>
<body>
<div id="chatroom">
Display the messages here.
</div>
<div id="chatform">
<form>
Name:
<input name="name" type="text" />
<br/>
Room:
<input name="room" type="text" value="RoomA" />
<br/>
Message:
<textarea name="message"></textarea>
<br/>
<input name="submit" type="submit" value="Send Message" />
</form>
</div>
</body>
</html>

View File

@ -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;
}());

57
json-chat-nodejs/tests.sh Normal file
View File

@ -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"
}'