Permalink
Browse files

update chat

  • Loading branch information...
1 parent 731be6f commit 6359b7d5fc50f64867140d4aa7e68ab770439dfe @twilson63 twilson63 committed Jun 12, 2010
Showing with 124 additions and 123 deletions.
  1. +9 −18 example.coffee
  2. +14 −9 example.js
  3. +8 −8 fu.js
  4. +39 −34 server.coffee
  5. +54 −54 server.js
View
@@ -1,20 +1,11 @@
-x: {
- TYPES: {
- ".3gp": "video/3gpp",
- ".a": "application/octet-stream",
- ".ai": "application/postscript",
- ".aif": "audio/x-aiff",
- ".aiff" : "audio/x-aiff",
- ".asc" : "application/pgp-signature",
- ".asf" : "video/x-ms-asf",
- ".asm" : "text/x-asm",
- ".asx" : "video/x-ms-asf",
- ".atom" : "application/atom+xml" },
- l: ->
- @TYPES[".asm"]
+class Hello
+ constructor: ->
+ @name: "world"
+ m: ->
+ puts "Hello"
+ puts @name
-
-}
-
-puts x.l()
+h: new Hello()
+
+h.m()
View
@@ -1,9 +1,14 @@
-var sys = require('sys'),
- http = require('http');
-http.createServer(function (req, res) {
- setTimeout(function () {
- res.writeHead(200, {'Content-Type': 'text/plain'});
- res.end('Hello World\n');
- }, 2000);
-}).listen(8000);
-sys.puts('Server running at http://127.0.0.1:8000/');
+(function(){
+ var Hello, h;
+ Hello = function() {
+ this.name = "world";
+ return this;
+ };
+ Hello.prototype.m = function() {
+ puts("Hello");
+ return puts(this.name);
+ };
+
+ h = new Hello();
+ h.m();
+})();
View
16 fu.js
@@ -12,15 +12,15 @@
DEBUG = false;
fu = exports;
NOT_FOUND = "Not Found\n";
- notFound = function notFound(req, res) {
+ notFound = function(req, res) {
res.writeHead(404, {
"Content-Type": "text/plain",
"Content-Length": NOT_FOUND.length
});
return res.end(NOT_FOUND);
};
getMap = {};
- fu.get = function get(path, handler) {
+ fu.get = function(path, handler) {
getMap[path] = handler;
return getMap[path];
};
@@ -35,7 +35,7 @@
});
return res.end(body);
}, this);
- res.simpleJSON = function simpleJSON(code, obj) {
+ res.simpleJSON = function(code, obj) {
var body;
body = JSON.stringify(obj);
res.writeHead(code, {
@@ -47,14 +47,14 @@
return handler(req, res);
}
});
- fu.listen = function listen(port, host) {
+ fu.listen = function(port, host) {
server.listen(port, host);
return sys.puts("Server at http://" + (host || "127.0.0.1") + ":" + port.toString() + "/");
};
- fu.close = function close() {
+ fu.close = function() {
return server.close();
};
- extname = function extname(path) {
+ extname = function(path) {
var index;
index = path.lastIndexOf(".");
if (index < 0) {
@@ -63,7 +63,7 @@
return path.substring(index);
}
};
- fu.staticHandler = function staticHandler(filename) {
+ fu.staticHandler = function(filename) {
var body, content_type, headers, loadResponseData;
body = "";
headers = "";
@@ -267,7 +267,7 @@
".yml": "text/yaml",
".zip": "application/zip"
},
- lookupExtension: function lookupExtension(ext, fallback) {
+ lookupExtension: function(ext, fallback) {
return this.TYPES[ext.toLowerCase];
//|| fallback || 'application/octet-stream'
}
View
@@ -1,5 +1,5 @@
HOST: null
-PORT: 8001
+PORT: process.env.PORT || 8001
starttime: new Date().getTime()
@@ -18,12 +18,10 @@ qs: require "querystring"
MESSAGE_BACKLOG: 200
SESSION_TIMEOUT: 60 * 1000
-class Channel
+class Channel
messages: []
-
callbacks: []
-
- appendMesssage: (nick, type, text) ->
+ appendMessage: (nick, type, text) ->
m: { nick: nick, type: type, text: text, timestamp: new Date().getTime() }
switch type
when "msg"
@@ -34,28 +32,34 @@ class Channel
sys.puts nick + " part"
@messages.push m
@callbacks.shift().callback([m]) while @callbacks.length > 0
- @messages.shift() while @messages.length > MESSAGE_BACKLOG
+ @messages.shift() while @messages.length > MESSAGE_BACKLOG
query: (since, callback) ->
matching: []
for message in @messages
matching.push message if message.timestamp > since
- if matching.length != 0 then callback matching else @callbacks.push { timestamp: new Date(), callback: callback }
-
-
- constructor: ->
- on_setInterval: ->
+ sys.p matching
+ if matching.length isnt 0
+ sys.puts "Callback Matching"
+ callback matching
+ else
+ sys.puts "Added Call back"
+ @callbacks.push { timestamp: new Date(), callback: callback }
+
+ setInterval( =>
now: new Date()
- @callbacks.shift().callback([]) while @callbacks.length > 0 and now - @callbacks[0].timestamp > 30*1000
- setInterval on_setInterval, 3000
+ @callbacks.shift().callback([]) while @callbacks.length > 0 and now - @callbacks[0].timestamp > 30*1000
+ 3000
+ )
sessions: {}
channel: new Channel()
createSession: (nick) ->
if nick.length > 50 then return null
- if /[^\w_\-^!]/.exec nick then return null
+ if /[^\w_\-^!]/.exec(nick)
+ return null
for session in sessions
if session.nick == nick then return null
@@ -90,7 +94,7 @@ fu.get("/", fu.staticHandler("index.html"))
fu.get("/style.css", fu.staticHandler("style.css"))
fu.get("/client.js", fu.staticHandler("client.js"))
fu.get("/jquery-1.4.2.min.js", fu.staticHandler("jquery-1.4.2.min.js"))
-#
+
fu.get('/who', (req, res) ->
nicks: []
for session in sessions
@@ -113,42 +117,43 @@ fu.get('/join', (req, res) ->
res.simpleJSON(200, { id: session.id, nick: session.nick, rss: mem.rss, starttime: starttime })
)
+fu.get("/send", (req, res) ->
+ id: qs.parse(url.parse(req.url).query).id
+ text: qs.parse(url.parse(req.url).query).text
+
+ session: sessions[id]
+ if !session or !text
+ res.simpleJSON(400, { error: "No such session id"})
+ return
+ session.poke()
+ channel.appendMessage session.nick, "msg", text
+ res.simpleJSON(200, { rss: mem.rss })
+)
+
+
fu.get("/part", (req, res) ->
id: qs.parse(url.parse(req.url).query).id
if id and sessions[id]
session: sessions[id]
session.destroy()
res.simpleJSON(200, { rss: mem.rss })
+)
-fu.get "/recv", (req, res) ->
- if !qs.parse(url.parse(req.url).query).since
- res.simpleJSON(400, { error: "Must supply since parameter" })
- return
-
+fu.get("/recv", (req, res) ->
+ sys.puts "recv called"
id: qs.parse(url.parse(req.url).query).id
if id and sessions[id]
session: sessions[id]
session.poke()
since: parseInt(qs.parse(url.parse(req.url).query).since, 10)
- channel.query(since, (message) ->
- if session then session.poke()
+ channel.query(since, (messages) ->
+ #if session then session.poke()
res.simpleJSON(200, { messages: messages, rss: mem.rss })
)
+ true
)
-fu.get("/send", (req, res) ->
- id: qs.parse(url.parse(req.url).query).id
- text: qs.parse(url.parse(req.url).query).text
-
- session: sessions[id]
- if !session or !text
- res.simpleJSON(400, { error: "No such session id"})
- return
- session.poke()
- channel.appendMessage session.nick, "msg", text
- res.simpleJSON(200, { rss: mem.rss })
-)
Oops, something went wrong.

0 comments on commit 6359b7d

Please sign in to comment.