#!/usr/bin/nodejs
// Brolink.js
// The server for brolink.vim
// By Jonathan Warner, 2014
// http://github.com/jaxbot/brolink.vim

console.log("Brolink");
console.log("Server version: 2.0.0");
console.log("======================");
console.log("Dedicated to everyone who missed the first chest in OOT's Forest Temple");
console.log("");

var WebSocketServer = require("websocket").server;
var http = require("http");
var fs = require("fs");
var path = require("path");

var connections = [];

var server = http.createServer(function(request, response) {
	console.log("Requested: " + request.url);

	switch (request.url) {
		case "/reloadCSS":
			broadcast("___RCSS");
			break;
		case "/reloadPage":
			broadcast("___RPAGE");
			break;
		case "/reloadTemplate":
			broadcast("___RTEMPLATE");
			break;
		case "/evaluateJS":
			request.on('data', function(data) {
				broadcast(data);
			});
			break;
		case "/socket.js":
			fs.readFile(path.resolve(__dirname,"socket.js"), "utf8", function(err,data) {
				if (err) {
					console.log(err);
				}
				response.setHeader('content-type', 'text/javascript');
				response.writeHead(200);
				response.write(data);
				response.end();
			});
			return;
			break;
		default:
			response.writeHead(404);
			response.end();
			return;
			break;
	}

	response.writeHead(200);
	response.end();

});

server.listen(9001, function() {
	console.log("Server listening on port 9001");
});

wsServer = new WebSocketServer({
	httpServer: server,
		 autoAcceptConnections: false
});

wsServer.on('request', function(request) {

	var connection = request.accept('', request.origin);
	console.log("Connection accepted.");

	connection.on('close', function(reasonCode, description) {
		console.log("Disconnected: " + connection.remoteAddress);
	});
	connection.on('message', function(msg) {
		broadcast(msg.utf8Data);
	});

	connections.push(connection);
});

function broadcast(data) {
	for (var i = 0; i < connections.length; i++) {
		connections[i].sendUTF(data);
	}
	console.log("Broadcast: " + data);
}


@@ -124,16 +124,40 @@
 nk.vim%0A%0A
+var VERSION = %222.5.0%22;%0A%0A
 console.
@@ -205,14 +205,19 @@
 on: 
-2.0.0%22
+%22 + VERSION
 );%0Ac
@@ -623,146 +623,70 @@
 l);%0A
+%09
 %0A%09
-switch (request.url) %7B%0A%09%09case %22/reloadCSS%22:%0A%09%09%09broadcast(%22___RCSS%22);%0A%09%09%09break;%0A%09%09case %22/reloadPage%22:%0A%09%09%09broadcast(%22___RPAGE%22);%0A%09%09%09break;
+var pieces = request.url.split(%22/%22);%0A%0A%09switch (pieces%5B1%5D) %7B
 %0A%09%09c
@@ -694,23 +694,14 @@
 se %22
-/
 reload
-Template
 %22:%0A%09
@@ -716,33 +716,29 @@
 ast(
-%22___RTEMPLATE%22
+pieces%5B2%5D
 );%0A%09%09%09
-b
 re
-ak
+turn
 ;%0A%09%09
@@ -747,17 +747,16 @@
 se %22
-/
 evaluate
 JS%22:
@@ -755,10 +755,8 @@
 uate
-JS
 %22:%0A%09
@@ -820,29 +820,30 @@
 ;%0A%09%09%09%7D);%0A%09%09%09
-b
 re
-ak
+turn
 ;%0A%09%09case %22/s
@@ -840,17 +840,16 @@
 %09%09case %22
-/
 socket.j
@@ -890,16 +890,17 @@
 dirname,
+ 
 %22socket.
@@ -926,16 +926,17 @@
 ion(err,
+ 
 data) %7B%0A
@@ -1080,21 +1080,19 @@
 esponse.
-write
+end
 (data);%0A
@@ -1098,66 +1098,27 @@
 %0A%09%09%09
-%09response.end();%0A%09%09%09%7D);%0A%09%09%09return;%0A%09%09%09break;%0A%09%09default:%0A%09%09
+%7D);%0A%09%09%09return;%0A%09%7D%0A%0A
 %09res
@@ -1137,16 +1137,14 @@
 ead(
-404
+200
 );%0A
-%09%09
 %09res
@@ -1157,76 +1157,28 @@
 end(
-);%0A%09%09%09return;%0A%09%09%09break;%0A%09%7D%0A%0A%09response.writeHead(200);%0A%09response.end(
+%22Brolink %22 + VERSION
 );%0A%0A
@@ -1326,10 +1326,8 @@
 r,%0A%09
-%09 
 auto
@@ -1662,25 +1662,27 @@
 sg) %7B%0A%09%09
-broadcast
+console.log
 (msg.utf

Make JS server a little less ridiculous