Permalink
Browse files

lot closer to getting console working, still issues with socket.io se…

…nding messages, added formating to the console logs, working on keeping log window open and still passing it the host to watch
  • Loading branch information...
1 parent 92d91f1 commit 0ad6837236764d5918bc23a0c8450a72423e4c8d @matthewfl committed Jul 4, 2011
Showing with 87 additions and 14 deletions.
  1. +10 −1 Bespin/control.js
  2. +39 −0 lib/format.js
  3. +10 −1 sandbox/index.js
  4. +16 −4 static/live_console.html
  5. +12 −8 test.js
View
@@ -324,16 +324,25 @@ exports.saveCommand = function (args, request) {
fileList.push(loadFile);
};
+//var console_win;
+
exports.testCommand = function (args, request) {
// this seems to work with better with popup blockers
- var win = window.open("");
+ var win = window.open("", "DEMO");
+ if(typeof console_win == "undefined" || console_win.closed)
+ console_win = window.open("http://console.test.jsapp.us:7654", "CONSOLE", "status=0,toolbar=0,location=0,menubar=0,directories=0,width=275,height=500,scrollbars=1");
Ajax.Call({
"action": "test",
"code": env.editor.value,
"randToken": randomToken,
"fileName": loadFile
}, function (p) {
win.location.href=p;
+ try {
+ setTimeout(function () {
+ console_win.location.hash=p;
+ }, 500);
+ }catch(e) {alert(e)}
});
Ajax.send();
track("test");
View
@@ -0,0 +1,39 @@
+// copied from the node console.js file
+
+// console object
+var formatRegExp = /%[sdj]/g;
+function format(f) {
+ var util = require('util');
+
+ if (typeof f !== 'string') {
+ var objects = [];
+ for (var i = 0; i < arguments.length; i++) {
+ objects.push(util.inspect(arguments[i]));
+ }
+ return objects.join(' ');
+ }
+
+
+ var i = 1;
+ var args = arguments;
+ var str = String(f).replace(formatRegExp, function(x) {
+ switch (x) {
+ case '%s': return String(args[i++]);
+ case '%d': return Number(args[i++]);
+ case '%j': return JSON.stringify(args[i++]);
+ default:
+ return x;
+ }
+ });
+ for (var len = args.length, x = args[i]; i < len; x = args[++i]) {
+ if (x === null || typeof x !== 'object') {
+ str += ' ' + x;
+ } else {
+ str += ' ' + util.inspect(x);
+ }
+ }
+ return str;
+}
+
+
+exports.format = format;
View
@@ -51,7 +51,16 @@ function SandBox (code, config) {
clearInterval: clearInterval,
debug: console.log,
- console: {log: self.config.log, info: self.config.log, warn: self.config.log, error: self.config.error, dir: nothing, time: nothing, timeEnd: nothing, trace: nothing, assert: function (a) { if(!a) throw "assert"; } }
+ console: {
+ log: self.config.log,
+ info: self.config.log,
+ warn: self.config.error,
+ error: self.config.error,
+ dir: nothing, time: nothing,
+ timeEnd: nothing,
+ trace: nothing,
+ assert: function (a) { if(!a) throw "assert"; }
+ }
};
try {
Script.runInNewContext(prefixBase + "(function () { "+code._+" })();", this.context, "main");
View
@@ -1,18 +1,30 @@
<html>
<head>
<title>Live console</title>
- <script src="http://static.test.jsapp.us:7654/socket.io/socket.io.js"></script>
+ <script src="/socket.io/socket.io.js"></script>
<script>
+var hostSet=0;
var socket = io.connect('http://'+location.host);
socket.on('message', function (data) {
-document.body.innerHTML += data;
clearInterval(hostSet);
+document.body.innerHTML += data;
});
-document.title += " "+location.host.split(":")[0];
+function setHost () {
+host = location.hash.substring(location.hash.indexOf("//")+2);
+document.title = "Live console for "+host;
// even sending this once the connect event fired, worked maybe 10% of the time
+clearInterval(hostSet);
hostSet = setInterval(function () {
-socket.send(location.host.split(":")[0]);
+socket.send(host);
}, 500);
+}
+var lastHash;
+setInterval(function () {
+if(lastHash!=location.hash) {
+setHost();
+lastHash=location.hash;
+}
+}, 200);
</script>
</head>
<body>
View
20 test.js
@@ -5,6 +5,7 @@ var sandbox = require('./sandbox');
var io = require('./lib/socket.io');
var fs = require('fs');
+var format = require('./lib/format').format;
var EventEmitter = require('events').EventEmitter;
var ErrorEmitter = new EventEmitter();
@@ -45,12 +46,11 @@ var server = http.createServer(function (req, res) {
tmp_db=boxes[name].config._tmp_db;
}
boxes[name] = new sandbox.SandBox(d, {test: true, user: urlInfo.query.user || null, name: urlInfo.query.fileName || null, _tmp_db: tmp_db,
- error: function (e) {
- ErrorEmitter.emit(name, e);
+ error: function () {
+ ErrorEmitter.emit(name, format.apply(this, arguments));
},
- log: function (l) {
- console.log("emmiting event", name);
- LogEmitter.emit(name, l);
+ log: function () {
+ LogEmitter.emit(name, format.apply(this, arguments));
}
});
boxes[name]._timer = setTimeout(function () {
@@ -104,13 +104,17 @@ io.sockets.on('connection', function (socket) {
}
});
socket.on('message', function (data) {
- if(host) return;
+ console.log("=========================================",data);
+ if(host) {
+ ErrorEmitter.removeListener(host, error_send);
+ LogEmitter.removeListener(host, log_send);
+ }
host = data;
- console.log("setting up event", host);
ErrorEmitter.on(host, error_send);
LogEmitter.on(host, log_send);
- socket.send("connected to: "+data);
+ socket.send("Connected to: "+data+"<br>");
});
+ socket.send("Connected to console");
});
server.listen(config.testPort);

0 comments on commit 0ad6837

Please sign in to comment.