Permalink
Browse files

Moved common example code in shared.js

  • Loading branch information...
Jonas Pfenniger
Jonas Pfenniger committed Dec 26, 2009
1 parent ebcb7c5 commit b2ea726e22fc56e28af5c5de0908c0ede5f36833
Showing with 53 additions and 94 deletions.
  1. +6 −50 example/chat.js
  2. +8 −44 example/ping.js
  3. +39 −0 example/shared.js
View
@@ -3,57 +3,13 @@
// Devel mode
require.paths.unshift('../lib');
-var http = require('http'),
- ws = require('http/websocket'),
- posix = require('posix'),
- sys = require('sys');
-
-const publicDir = './public';
-
-function ping(msg, conn) {
- sys.debug("PING: " + msg);
- conn.send(msg);
-}
-
-function notFound(req, res) {
- var msg = "<h1>File not found : " + req.uri.path + "</h1>";
- res.sendHeader(404, {
- "Content-Type": "text/html",
- "Content-Length": msg.length
- });
- res.sendBody(msg);
- res.finish();
-}
+var http = require('http'),
+ ws = require('http/websocket'),
+ sys = require('sys'),
+ notFound = require('./shared').notFound,
+ sendFile = require('./shared').sendFile;
-var mimeReg = /\.[^\.]+$/
-mimeMap = {
- ".html": "text/html",
- ".js": "text/javascript",
- ".xml": "application/xml",
- ".swf": "application/x-shockwave-flash"
-}
-
-function sendFile(req, res, path) {
- var data;
- try {
- data = posix.cat(path, "binary").wait();
- if (data) {
- var cType = mimeMap[mimeReg.exec(path)[0]] || "application/octet-stream";
- res.sendHeader(200, {
- "Content-Length": data.length,
- "Content-Type": cType
- });
- res.sendBody(data, "binary");
- res.finish();
- return;
- }
- } catch(e) {
- sys.debug(e);
- }
-
- sys.debug("File not found: " + path);
- notFound(req, res);
-}
+const publicDir = './public';
var chat = new ws.Server();
chat.addListener("connect", function(conn) {
View
@@ -3,10 +3,11 @@
// Devel mode
require.paths.unshift('../lib');
-var http = require('http'),
- ws = require('http/websocket'),
- posix = require('posix'),
- sys = require('sys');
+var http = require('http'),
+ ws = require('http/websocket'),
+ sys = require('sys'),
+ notFound = require('./shared').notFound,
+ sendFile = require('./shared').sendFile;
const publicDir = './public';
@@ -15,52 +16,15 @@ function ping(msg, conn) {
conn.send(msg);
}
-function notFound(req, res) {
- var msg = "<h1>File not found : " + req.uri.path + "</h1>";
- res.sendHeader(404, {
- "Content-Type": "text/html",
- "Content-Length": msg.length
- });
- res.sendBody(msg);
- res.finish();
-}
-
-var mimeReg = /\.[^\.]+$/
-mimeMap = {
- ".html": "text/html",
- ".js": "text/javascript",
- ".xml": "application/xml",
- ".swf": "application/x-shockwave-flash"
-}
-
-function sendFile(req, res, path) {
- var data;
- try {
- data = posix.cat(path, "binary").wait();
- if (data) {
- var cType = mimeMap[mimeReg.exec(path)[0]] || "application/octet-stream";
- res.sendHeader(200, {
- "Content-Length": data.length,
- "Content-Type": cType
- });
- res.sendBody(data, "binary");
- res.finish();
- return;
- }
- } catch(e) {
- sys.debug(e);
- }
-
- sys.debug("File not found: " + path);
- notFound(req, res);
-}
-
function handler(req, res) {
sys.debug(req.method + ' ' + req.uri.path);
if (ws.askUpgrade(req)) {
sys.debug("Upgrading to WebSocket");
var conn = new ws.Connection(req, res);
conn.addListener("message", ping);
+ conn.addListener("close", function() {
+ sys.debug("Closing WebSocket");
+ });
} else {
var filePath = publicDir + req.uri.path;
sendFile(req, res, filePath);
View
@@ -0,0 +1,39 @@
+
+var posix = require('posix');
+
+const mimeReg = /\.[^\.]+$/
+const mimeMap = {
+ ".html": "text/html",
+ ".js": "text/javascript",
+ ".xml": "application/xml",
+ ".swf": "application/x-shockwave-flash"
+}
+
+
+function notFound(req, res) {
+ var msg = "<h1>File not found : " + req.uri.path + "</h1>";
+ res.sendHeader(404, {
+ "Content-Type": "text/html",
+ "Content-Length": msg.length
+ });
+ res.sendBody(msg);
+ res.finish();
+}
+
+function sendFile(req, res, path) {
+ posix.cat(path, "binary").addCallback(function(data) {
+ var cType = mimeMap[mimeReg.exec(path)[0]] || "application/octet-stream";
+ res.sendHeader(200, {
+ "Content-Length": data.length,
+ "Content-Type": cType
+ });
+ res.sendBody(data, "binary");
+ res.finish();
+ }).addErrback(function() {
+ notFound(req, res);
+ });
+}
+
+// Public functions
+exports.notFound = notFound;
+exports.sendFile = sendFile;

0 comments on commit b2ea726

Please sign in to comment.