Support debugger reconnection when the packager goes down.

Summary:
This should resolve the issue highlighted in #1709
Closes https://github.com/facebook/react-native/pull/1992
Github Author: Sean Powell <sean@longdivision.co.uk>
This commit is contained in:
Sean Powell 2015-07-16 14:16:50 -07:00
parent 81ad713f5f
commit fa4c570d33
1 changed files with 40 additions and 27 deletions

View File

@ -27,6 +27,11 @@ window.onbeforeunload = function() {
}
};
// Alias native implementations needed by the debugger before platform-specific
// implementations are loaded into the global namespace
var debuggerSetTimeout = window.setTimeout;
var DebuggerWebSocket = window.WebSocket;
function setStatus(status) {
document.getElementById('status').innerHTML = status;
}
@ -58,35 +63,43 @@ var messageHandlers = {
sendReply(JSON.stringify(returnValue));
}
}
};
function connectToDebuggerProxy() {
var ws = new DebuggerWebSocket('ws://' + window.location.host + '/debugger-proxy');
ws.onopen = function() {
if (sessionID) {
setStatus('Debugger session #' + sessionID + ' active');
ws.send(JSON.stringify({replyID: parseInt(sessionID, 10)}));
} else {
setStatus('Waiting, press ⌘R in simulator to reload and connect');
}
};
ws.onmessage = function(message) {
var object = JSON.parse(message.data);
var sendReply = function(result) {
ws.send(JSON.stringify({replyID: object.id, result: result}));
};
var handler = messageHandlers[object.method];
if (handler) {
handler(object, sendReply);
} else {
console.warn('Unknown method: ' + object.method);
}
};
ws.onclose = function() {
setStatus('Disconnected from proxy. Attempting reconnection. Is node server running?');
sessionID = null;
window.localStorage.removeItem('sessionID');
debuggerSetTimeout(connectToDebuggerProxy, 100);
};
}
var ws = new WebSocket('ws://' + window.location.host + '/debugger-proxy');
ws.onopen = function() {
if (sessionID) {
setStatus('Debugger session #' + sessionID + ' active');
ws.send(JSON.stringify({replyID: parseInt(sessionID, 10)}));
} else {
setStatus('Waiting, press ⌘R in simulator to reload and connect');
}
}
ws.onmessage = function(message) {
var object = JSON.parse(message.data);
var sendReply = function(result) {
ws.send(JSON.stringify({replyID: object.id, result: result}));
}
var handler = messageHandlers[object.method];
if (handler) {
handler(object, sendReply);
} else {
console.warn('Unknown method: ' + object.method);
}
}
ws.onclose = function() {
setStatus('Disconnected from proxy. Is node server running?');
}
connectToDebuggerProxy();
function loadScript(src, callback) {
var script = document.createElement('script');