Skip to content

Commit

Permalink
added websocket ping to keep the connection alive
Browse files Browse the repository at this point in the history
  • Loading branch information
dannycoates committed Jul 23, 2011
1 parent 25670cb commit 7f5b6de
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
11 changes: 7 additions & 4 deletions bin/inspector.js
Expand Up @@ -30,7 +30,7 @@ process.argv.forEach(function (arg) {

fs.readFile(path.join(__dirname, '../config.json'), function(err, data) {
var config,
dserver;
debugServer;
if (err) {
console.warn("could not load config.json\n" + err.toString());
config = {};
Expand All @@ -49,11 +49,14 @@ fs.readFile(path.join(__dirname, '../config.json'), function(err, data) {
if (!config.debugPort) {
config.debugPort = 5858;
}
if (options.webPort) {
config.webPort = options.webPort;
}

dserver = new DebugServer();
dserver.on('close', function () {
debugServer = new DebugServer();
debugServer.on('close', function () {
console.log('session closed');
process.exit();
});
dserver.listen(options.webPort || config.webPort);
debugServer.start(config);
});
6 changes: 5 additions & 1 deletion front-end/node/Overrides.js
Expand Up @@ -2,7 +2,11 @@
// Wire up websocket to talk to backend
WebInspector.loaded = function() {
WebInspector.socket = new WebSocket("ws://" + window.location.host + '/debug?port=' + WebInspector.queryParamsObject.port);
WebInspector.socket.onmessage = function(message) { WebInspector_syncDispatch(message.data); }
WebInspector.socket.onmessage = function(message) {
if (message && message.data !== 'ping') {
WebInspector_syncDispatch(message.data);
}
}
WebInspector.socket.onerror = function(error) { console.error(error); }
WebInspector.socket.onopen = function() {
InspectorFrontendHost.sendMessageToBackend = WebInspector.socket.send.bind(WebInspector.socket);
Expand Down
26 changes: 13 additions & 13 deletions lib/debug-server.js
Expand Up @@ -6,6 +6,7 @@ var Http = require('http'),
inherits = require('util').inherits,
WEBROOT = require('path').join(__dirname, '../front-end'),
sessions = {},
config = {},
webPort;

function serveStaticFiles(req, res) {
Expand All @@ -20,44 +21,43 @@ function getDebuggerPort(url, defaultPort) {
function getSession(debuggerPort) {
var session = sessions[debuggerPort];
if (!session) {
session = Session.create(debuggerPort, {}); // TODO fix config
session = Session.create(debuggerPort, config);
sessions[debuggerPort] = session;
// TODO session on close
}
return session;
}

function handleWebSocketConnection(conn) {
var port = getDebuggerPort(conn._req.url, 5858); // TODO
getSession(port).join(conn)
var port = getDebuggerPort(conn._req.url, config.debugPort);
getSession(port).join(conn);
}

function handleServerListening() {
console.log(
'visit http://0.0.0.0:' +
webPort +
'/debug?port=5858 to start debugging'); // TODO port
config.webPort +
'/debug?port=' + config.debugPort + ' to start debugging');
}

function DebugServer() {
function DebugServer() {}

inherits(DebugServer, EventEmitter);

DebugServer.prototype.start = function(options) {
config = options;
var httpServer = Http.createServer(serveStaticFiles);
this.wsServer = WebSocket.createServer({ server: httpServer });
this.wsServer.on('connection', handleWebSocketConnection);
this.wsServer.on('listening', handleServerListening);
this.wsServer.listen(config.webPort);
}

inherits(DebugServer, EventEmitter);

DebugServer.prototype.close = function() {
if (this.wsServer) {
this.wsServer.close();
this.emit('close');
}
}

DebugServer.prototype.listen = function(port) {
webPort = port;
this.wsServer.listen(port);
}

exports.DebugServer = DebugServer;
8 changes: 8 additions & 0 deletions lib/session.js
Expand Up @@ -235,8 +235,16 @@ exports.create = function(debuggerPort, config) {
}
}

function sendPing() {
if (conn) {
conn.write('ping');
setTimeout(sendPing, 30000);
}
}

function browserConnected() { // TODO find a better name
sendEvent('debuggerWasEnabled');
sendPing();
var args = { arguments: { includeSource: true, types: 4 }};
debug.request('scripts', args, function(msg) {
parsedScripts(msg);
Expand Down

0 comments on commit 7f5b6de

Please sign in to comment.