Commits

Andrija Cacanovic  committed 4cc20d9

refactoring

  • Participants
  • Parent commits 00bd0cb

Comments (0)

Files changed (5)

File server/HttpServerHandler.js

+/*
+ * Source: http://thecodinghumanist.com/blog/archives/2011/5/6/serving-static-files-from-node-js
+ * */
+
+var helper = require('./helper')
+, http = require('http')
+, path = require('path')
+, fs = require('fs');
+
+
+exports.HttpServerHandler = function(config){
+	this.port = helper.checkValue(config, "port", 80);
+	this.server = null;
+};
+
+exports.HttpServerHandler.prototype = {		
+		createServer: function() {
+			this.server = http.createServer(this.serverHandler);
+			return this;
+		},
+		start: function() {
+			helper.print(this.port);
+			this.server.listen(this.port);
+			return this;
+		},
+		serverHandler: function(request, response) {
+
+			
+			var filePath = '.' + request.url;
+			if (filePath == './') {
+				filePath = './index.html';
+			}
+
+			var extname = path.extname(filePath);
+			var contentType = 'text/html';
+			switch (extname) {
+				case '.js':
+					contentType = 'text/javascript';
+					break;
+				case '.css':
+					contentType = 'text/css';
+					break;
+			}
+			
+			path.exists(filePath, function(exists) {
+			
+				if (exists) {
+					fs.readFile(filePath, function(error, content) {
+						if (error) {
+							response.writeHead(500);
+							response.end();
+						}
+						else {
+							response.writeHead(200, { 'Content-Type': contentType });
+							response.end(content, 'utf-8');
+						}
+					});
+				}
+				else {
+					response.writeHead(404);
+					response.end();
+				}
+			});	
+		}
+};
+
+
+
+

File server/IoServerHandler.js

+ var io = require('socket.io')
+ , helper = require('./helper');
+
+ var clientList = [];
+ 
+exports.IoServerHandler = function(){
+	this.clientList = [];
+	this.server = null;
+};
+
+exports.IoServerHandler.prototype = {
+		start: function(httpServer) {
+			this.server = io.listen(httpServer);
+			this.server.sockets.on('connection', this.onSocketConnected);
+			helper.print('io start');
+		},
+		onSocketConnected: function(socket) {
+			helper.print('connected');		
+			
+			clientList.push(socket);
+			
+			socket.emit('msg', {type: "sys", messageBody: "connected"});
+			
+			socket.on('msg', function(data) {
+				for(var i=0;i<clientList.length;i++) {
+					if(clientList[i]) {
+						clientList[i].emit('msg', data);
+					}
+				}
+			});
+		}		
+};

File server/helper.js

+var sys = require("util");
+
+exports.checkValue = function(obj, key, defaultValue){	
+	if(!!obj && obj[key] !== undefined && obj[key] !== null) {
+		return obj[key];
+	}
+	return defaultValue;
+};
+		
+exports.print = function(value) {
+	sys.puts(value.toString());
+};
+

File server/server.js

-var app = require('http').createServer(handler)
-  , io = require('socket.io').listen(app)
-  , fs = require('fs')
+var helper = require('./helper')
+	, HttpServerHandler = require('./HttpServerHandler').HttpServerHandler
+	, IoServerHandler = require('./IoServerHandler').IoServerHandler
+	
+	, serverHandler = new HttpServerHandler({port: 80})
+	, ioServerHandler = new IoServerHandler();
 
-app.listen(80);
+serverHandler.createServer();
+ioServerHandler.start(serverHandler.server);
+serverHandler.start();
 
-function handler (req, res) {
-	fs.readFile(__dirname + '/index.html',
-		function (err, data) {
-			if (err) {
-				res.writeHead(500);
-				return res.end('Error loading index.html');
-			}
 
-			res.writeHead(200);
-			res.end(data);
-		});
-}
-var clientList = [];
-io.sockets.on('connection', function (socket) {
-	clientList.push(socket);
-	socket.emit('msg', {type: "sys", messageBody: "connected echo"});
-	socket.on('msg', function (data) {
-		//io.sockets.broadcast('msg', data);
-		for(var i=0;i<clientList.length;i++) {
-			if(clientList[i]) {
-				clientList[i].emit('msg', data);
-			}
-		}
-	});
-	socket.on('disconnect', function () {
-		//clientList.remove(this);
-		io.sockets.emit({type: "sys", messageBody: "user disconnected"});
-	});
-});
-

File server/start_server.sh

+#!/bin/bash
+sudo node ./server.js