Skip to content

Commit

Permalink
work in progress on fixing browser refresh disconnect issue
Browse files Browse the repository at this point in the history
  • Loading branch information
dannycoates committed Jul 15, 2011
1 parent 59fc454 commit 98e9ab8
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 49 deletions.
22 changes: 10 additions & 12 deletions lib/debug-server.js
Expand Up @@ -3,10 +3,10 @@ var Http = require('http'),
path = require('path'),
WebSocket = require('websocket-server'),
paperboy = require('paperboy'),
debugr = require('./debugger'),
Session = require('./session');
Session = require('./session'),
WEBROOT = path.join(__dirname, '../front-end'),
sessions = {};

var WEBROOT = path.join(__dirname, '../front-end');
function staticFile(req, res) {
req.url = req.url.replace(/^\/debug/, '/');
paperboy.deliver(WEBROOT, req, res);
Expand All @@ -30,15 +30,13 @@ exports.create = function(options, config) {
wsServer.on('connection', function(conn) {
var port =
parseInt((/\?port=(\d+)/.exec(conn._req.url) || [null, debugPort])[1], 10),
session = Session.create(conn, debugr, port, config);

conn.on('message', function(data) {
session.handleRequest(data);
});
conn.on('close', function() {
session.close();
console.log('connection closed');
});
session = sessions[port];
if (!session) {
session = Session.create(port, config);
sessions[port] = session;
}
session.join(conn)
// XXX should session and debugger be bound together?
});

wsServer.listen(settings.webPort);
Expand Down
98 changes: 61 additions & 37 deletions lib/session.js
@@ -1,12 +1,14 @@
var http = require('http'),
events = require('events'),
debugr = require('./debugger'),
path = require('path');

///////////////////////////////////////////////////////////
// exports

exports.create = function(conn, debugr, debuggerPort, config) {
exports.create = function(debuggerPort, config) {
var debug = null,
conn = null,
//map from sourceID:lineNumber to breakpoint
breakpoints = {},
//map from sourceID to filename
Expand Down Expand Up @@ -215,20 +217,53 @@ exports.create = function(conn, debugr, debuggerPort, config) {

function sendEvent(name, data) {
data = data || {};
conn.write(JSON.stringify({
type: 'event',
event: name,
data: data
}));
if (conn) {
conn.write(JSON.stringify({
type: 'event',
event: name,
data: data
}));
}
}

function sendResponse(seq, success, data) {
data = data || {};
conn.write(JSON.stringify({
seq: seq,
success: success,
data: data
}));
if (conn) {
conn.write(JSON.stringify({
seq: seq,
success: success,
data: data
}));
}
}

function browserConnected() { // TODO find a better name
sendEvent('debuggerWasEnabled');
var args = { arguments: { includeSource: true, types: 4 }};
debug.request('scripts', args, function(msg) {
parsedScripts(msg);
debug.request('listbreakpoints', {},
function(msg) {
msg.body.breakpoints.forEach(function(bp) {
var data;
if (bp.type === 'scriptId') {
data = {
sourceID: bp.script_id,
url: sourceIDs[bp.script_id].url,
line: bp.line + 1,
enabled: bp.active,
condition: bp.condition,
number: bp.number
};
breakpoints[bp.script_id + ':' + (bp.line + 1)] = data;
sendEvent('restoredBreakpoint', data);
}
});
if (!msg.running) {
sendBacktrace();
}
});
});
}

function attach() {
Expand All @@ -244,32 +279,7 @@ exports.create = function(conn, debugr, debuggerPort, config) {
sendEvent('debuggerWasDisabled');
});
debug.on('connect', function() {
sendEvent('debuggerWasEnabled');
var args = { arguments: { includeSource: true, types: 4 }};
debug.request('scripts', args, function(msg) {
parsedScripts(msg);
debug.request('listbreakpoints', {},
function(msg) {
msg.body.breakpoints.forEach(function(bp) {
var data;
if (bp.type === 'scriptId') {
data = {
sourceID: bp.script_id,
url: sourceIDs[bp.script_id].url,
line: bp.line + 1,
enabled: bp.active,
condition: bp.condition,
number: bp.number
};
breakpoints[bp.script_id + ':' + (bp.line + 1)] = data;
sendEvent('restoredBreakpoint', data);
}
});
if (!msg.running) {
sendBacktrace();
}
});
});
browserConnected();
});
debug.on('exception', function(msg) {
breakEvent(msg);
Expand Down Expand Up @@ -926,6 +936,20 @@ exports.create = function(conn, debugr, debuggerPort, config) {
});
}
},
join: {
value: function(ws_connection) {
var self = this;
conn = ws_connection;
conn.on('message', function(data) {
self.handleRequest(data);
});
conn.on('close', function() {
// TODO what to do here? set timeout to close debugger connection
conn = null;
});
browserConnected();
}
},
handleRequest: {
value: function(data) {
var msg = JSON.parse(data),
Expand Down

0 comments on commit 98e9ab8

Please sign in to comment.