Permalink
Browse files

added node server for brackets with a websocket proxy

  • Loading branch information...
1 parent f796996 commit 8f2a5a8f28c6fdaa9b80c41c11f632ff46bdf532 @joelrbrandt joelrbrandt committed Apr 26, 2012
Showing with 114 additions and 0 deletions.
  1. +1 −0 src/node/.gitignore
  2. +14 −0 src/node/package.json
  3. +75 −0 src/node/proxy.js
  4. +24 −0 src/node/webserver.js
View
@@ -0,0 +1 @@
+node_modules/
View
@@ -0,0 +1,14 @@
+{ "name" : "cocoanode-webserver"
+, "description" : "something whitty"
+, "version" : "0.0.1"
+, "author" : "Joel Brandt <joelrbrandt@gmail.com>"
+, "engines" : {"node" : ">=0.7.8"}
+, "main" : "webserver.js"
+, "directories" :
+ { "public" : "./public"
+ }
+, "dependencies" :
+ { "connect": ">= 0.0.1"
+ , "ws": ">= 0.0.1"
+ }
+}
View
@@ -0,0 +1,75 @@
+// proxy.js
+
+
+var fs = require('fs');
+
+// add some stuff to fs because the return types are sometimes complicated objects that don't jsonify
+fs.statBrackets = function(path, callback) {
+ fs.stat(path, function(err, stats) {
+ if (err) {
+ callback(err, null);
+ } else {
+ result = {};
+ result.isFile = stats.isFile();
+ result.isDirectory = stats.isDirectory();
+ result.mtime = stats.mtime;
+ result.filesize = stats.size;
+ callback(0, result); // TODO: hack, need handling of error being null in callbacks on client side
+ }
+ })
+}
+
+// A test namespace
+
+var stupid = {};
+
+stupid.reverse = function(s) {
+ return s.split("").reverse().join("");
+};
+
+// end test namespace
+
+
+var namespaces = {
+ fs : fs,
+ stupid : stupid
+};
+
+
+function createCallback(id, ws) {
+ return function() {
+ var args = Array.prototype.slice.call(arguments);
+ ws.send(JSON.stringify({id: id, result: args}));
+ };
+}
+
+function handleConnection(ws) {
+ ws.on('message', function(message) {
+ console.log('received: %s', message);
+ try {
+ m = JSON.parse(message);
+ //console.log('parsed version:');
+ //console.log(m);
+ doCommand(m.id, m.namespace, m.command, m.args, m.isAsync, ws);
+ } catch (e) {
+ console.log("Error: could't parse the message or something");
+ }
+ })
+}
+
+function doCommand(id, namespace, command, args, isAsync, ws) {
+ try {
+ var f = namespaces[namespace][command];
+ var callback = createCallback(id, ws)
+ if (isAsync) {
+ args.push(callback)
+ f.apply(global, args)
+ } else {
+ callback(f.apply(global, args))
+ }
+ } catch (e) {
+ console.log("Error: Couldn't run the command " + namespace + "." + command + " with args " + JSON.stringify(args));
+ }
+}
+
+exports.handleConnection = handleConnection;
View
@@ -0,0 +1,24 @@
+console.log('starting webserver in dir: ' + __dirname)
+
+var connect = require('connect')
+var WebSocketServer = require('ws').Server
+var proxy = require('./proxy');
+
+var app = connect()
+.use(connect.static(__dirname + '/../../brackets/src/'))
+.use(function(req, res){
+ res.end('hello world\n');
+})
+.listen(3000);
+
+var wss = new WebSocketServer({server: app});
+wss.on('connection', proxy.handleConnection);
+
+console.log('servers started, trying to redirect');
+try {
+ var cocoa = require('cocoa');
+ console.log(cocoa.goToURL("http://localhost:3000"));
+}
+catch (e) {
+ console.log("unable to redirect shell, maybe we aren't running in one");
+}

0 comments on commit 8f2a5a8

Please sign in to comment.