Browse files

work in progress on fixing browser refresh disconnect issue

  • Loading branch information...
1 parent 59fc454 commit 98e9ab884a083bb13bf38782edd1bea537a741e9 @dannycoates dannycoates committed Jul 14, 2011
Showing with 71 additions and 49 deletions.
  1. +10 −12 lib/debug-server.js
  2. +61 −37 lib/session.js
View
22 lib/debug-server.js
@@ -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);
@@ -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);
View
98 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
@@ -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() {
@@ -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);
@@ -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),

0 comments on commit 98e9ab8

Please sign in to comment.