Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

update chat

  • Loading branch information...
commit 6359b7d5fc50f64867140d4aa7e68ab770439dfe 1 parent 731be6f
@twilson63 twilson63 authored
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
27 example.coffee
@@ -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
23 example.js
@@ -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,7 +12,7 @@
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
@@ -20,7 +20,7 @@
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
73 server.coffee
@@ -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,20 +32,25 @@ 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: {}
@@ -55,7 +58,8 @@ 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,18 +117,30 @@ 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]
@@ -132,23 +148,12 @@ fu.get "/recv", (req, res) ->
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 })
-)
View
108 server.js
@@ -1,10 +1,15 @@
(function(){
var Channel, HOST, MESSAGE_BACKLOG, PORT, SESSION_TIMEOUT, channel, createSession, fu, kill_sessions, mem, mem_usage, qs, sessions, starttime, sys, url;
+ var __slice = Array.prototype.slice, __bind = function(func, obj, args) {
+ return function() {
+ return func.apply(obj || {}, args ? args.concat(__slice.call(arguments, 0)) : arguments);
+ };
+ };
HOST = null;
- PORT = 8001;
+ PORT = process.env.PORT || 8001;
starttime = new Date().getTime();
mem = process.memoryUsage();
- mem_usage = function mem_usage() {
+ mem_usage = function() {
mem = process.memoryUsage();
return mem;
};
@@ -15,24 +20,10 @@
qs = require("querystring");
MESSAGE_BACKLOG = 200;
SESSION_TIMEOUT = 60 * 1000;
- Channel = function Channel() {
- Channel.prototype.messages = [];
- Channel.prototype.callbacks = [];
-
- var on_setInterval;
- on_setInterval = function on_setInterval() {
- var _a, now;
- now = new Date();
- _a = [];
- while (this.callbacks.length > 0 && now - this.callbacks[0].timestamp > 30 * 1000) {
- _a.push(this.callbacks.shift().callback([]));
- }
- return _a;
- };
- setInterval(on_setInterval, 3000);
- return this;
- };
- Channel.prototype.appendMesssage = function appendMesssage(nick, type, text) {
+ Channel = function() { };
+ Channel.prototype.messages = [];
+ Channel.prototype.callbacks = [];
+ Channel.prototype.appendMessage = function(nick, type, text) {
var _a, m;
m = {
nick: nick,
@@ -57,7 +48,7 @@
}
return _a;
};
- Channel.prototype.query = function query(since, callback) {
+ Channel.prototype.query = function(since, callback) {
var _a, _b, _c, matching, message;
matching = [];
_b = this.messages;
@@ -67,18 +58,31 @@
matching.push(message);
}
}
+ sys.p(matching);
if (matching.length !== 0) {
- return callback(matching);
+ sys.puts("Callback Matching");
+ callback(matching);
} else {
- return this.callbacks.push({
+ sys.puts("Added Call back");
+ this.callbacks.push({
timestamp: new Date(),
callback: callback
});
}
+ return setInterval(__bind(function() {
+ var _d, now;
+ now = new Date();
+ _d = [];
+ while (this.callbacks.length > 0 && now - this.callbacks[0].timestamp > 30 * 1000) {
+ _d.push(this.callbacks.shift().callback([]));
+ }
+ return _d;
+ }, this), 3000);
};
+
sessions = {};
channel = new Channel();
- createSession = function createSession(nick) {
+ createSession = function(nick) {
var _a, _b, _c, session;
if (nick.length > 50) {
return null;
@@ -97,11 +101,11 @@
nick: nick,
id: Math.floor(Math.random() * 99999999999).toString(),
timestamp: new Date(),
- poke: function poke() {
+ poke: function() {
session.timestamp = new Date();
return session.timestamp;
},
- destroy: function destroy() {
+ destroy: function() {
channel.appendMessage(session.nick, "part");
return delete sessions[session.id];
}
@@ -109,7 +113,7 @@
sessions[session.id] = session;
return session;
};
- kill_sessions = function kill_sessions() {
+ kill_sessions = function() {
var _a, _b, _c, now, session;
now = new Date();
_b = sessions;
@@ -128,7 +132,6 @@
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', function(req, res) {
var _a, _b, _c, nicks, session;
nicks = [];
@@ -169,6 +172,23 @@
starttime: starttime
});
});
+ fu.get("/send", function(req, res) {
+ var id, session, text;
+ id = qs.parse(url.parse(req.url).query).id;
+ text = qs.parse(url.parse(req.url).query).text;
+ session = sessions[id];
+ if (!session || !text) {
+ res.simpleJSON(400, {
+ error: "No such session id"
+ });
+ return null;
+ }
+ session.poke();
+ channel.appendMessage(session.nick, "msg", text);
+ return res.simpleJSON(200, {
+ rss: mem.rss
+ });
+ });
fu.get("/part", function(req, res) {
var id, session;
id = qs.parse(url.parse(req.url).query).id;
@@ -179,43 +199,23 @@
return res.simpleJSON(200, {
rss: mem.rss
});
- }, fu.get("/recv", function(req, res) {
+ });
+ fu.get("/recv", function(req, res) {
var id, session, since;
- if (!qs.parse(url.parse(req.url).query).since) {
- res.simpleJSON(400, {
- error: "Must supply since parameter"
- });
- return null;
- }
+ sys.puts("recv called");
id = qs.parse(url.parse(req.url).query).id;
if (id && sessions[id]) {
session = sessions[id];
session.poke();
}
since = parseInt(qs.parse(url.parse(req.url).query).since, 10);
- return channel.query(since, function(message) {
- session ? session.poke() : null;
+ channel.query(since, function(messages) {
+ //if session then session.poke()
return res.simpleJSON(200, {
messages: messages,
rss: mem.rss
});
});
- }));
- fu.get("/send", function(req, res) {
- var id, session, text;
- id = qs.parse(url.parse(req.url).query).id;
- text = qs.parse(url.parse(req.url).query).text;
- session = sessions[id];
- if (!session || !text) {
- res.simpleJSON(400, {
- error: "No such session id"
- });
- return null;
- }
- session.poke();
- channel.appendMessage(session.nick, "msg", text);
- return res.simpleJSON(200, {
- rss: mem.rss
- });
+ return true;
});
})();
Please sign in to comment.
Something went wrong with that request. Please try again.