var	http = require('http');
var dispatcher = require('./dispatcher'); 
var	WebSocketServer = require('ws').Server;
 
dispatcher.setStatic('/static');
var adminWS = [ ];
 
var notify = function(req, res) {
	for(c in adminWS) {
		if(typeof adminWS[c].send === "function") {
			adminWS[c].send(JSON.stringify({ 
				id: req.connection.remoteAddress,
				userAgent: req.headers['user-agent'],
				time: (new Date()).getTime()
			}));
		}
	}
}
 
dispatcher.onGet("/homepage", function(req, res) {
    res.end("<h1>Homepage</h1");
    notify(req, res);
});
 

var server = http.createServer(function (req, res) {
    dispatcher.dispatch(req, res);
});
server.listen(1337, '127.0.0.1');

var wss = new WebSocketServer({server:server});
wss.on('connection', function(ws) {
    adminWS.push(ws);
});
