diff --git a/tizenbrew-app/TizenBrew/index.html b/tizenbrew-app/TizenBrew/index.html index 034e5e0..fb1400c 100644 --- a/tizenbrew-app/TizenBrew/index.html +++ b/tizenbrew-app/TizenBrew/index.html @@ -83,6 +83,10 @@ case 13: var packageName = selectedItem.getAttribute("data-packagename"); var appPath = selectedItem.getAttribute("data-appPath"); + if (!canLaunchModules) { + alert("You can't launch modules while the service hasn't connected yet."); + break; + } window.send({ type: 'launch', packageName }); if (appPath.startsWith("http")) { location.href = appPath; diff --git a/tizenbrew-app/TizenBrew/js/wsClient.js b/tizenbrew-app/TizenBrew/js/wsClient.js index 746ff43..d50ce75 100644 --- a/tizenbrew-app/TizenBrew/js/wsClient.js +++ b/tizenbrew-app/TizenBrew/js/wsClient.js @@ -2,6 +2,7 @@ let client; const isTizen3 = navigator.userAgent.includes('Tizen 3.0'); +let canLaunchModules = false; function connect() { const ip = localStorage.getItem('ip'); @@ -32,11 +33,12 @@ function onMessage(msg) { const message = JSON.parse(msg.data); switch (message.type) { case 'debugStatus': { - if (message.inDebug) { + if (message.inDebug.tizenDebug) { + canLaunchModules = message.inDebug.webDebug; send({ type: 'loadModules', modules: JSON.parse(localStorage.getItem('modules')) }); } else { send({ type: 'relaunchInDebug', isTizen3, tvIp: webapis.network.getIp() }); - send({ type: 'loadModules', modules: JSON.parse(localStorage.getItem('modules')) }); + tizen.application.getCurrentApplication().exit(); } break; } @@ -72,6 +74,13 @@ function onMessage(msg) { setTimeout(() => { document.getElementById('errorDiv').innerHTML = ''; }, 5000); + break; + } + + case 'canLaunchModules': { + canLaunchModules = true; + document.getElementById('wsText').innerText = 'Connected to server.'; + break; } default: { // This should never happen. diff --git a/tizenbrew-app/TizenBrew/moduleManager.html b/tizenbrew-app/TizenBrew/moduleManager.html index c6fca7d..b1382c1 100644 --- a/tizenbrew-app/TizenBrew/moduleManager.html +++ b/tizenbrew-app/TizenBrew/moduleManager.html @@ -163,8 +163,8 @@

Add

document.getElementById('appList').innerHTML += `
-
Set Server IP
- +
Add Module
+
diff --git a/tizenbrew-app/TizenBrew/service/debugger.js b/tizenbrew-app/TizenBrew/service/debugger.js index 035b147..f96243e 100644 --- a/tizenbrew-app/TizenBrew/service/debugger.js +++ b/tizenbrew-app/TizenBrew/service/debugger.js @@ -6,19 +6,29 @@ let currentID = 12; let contextID = 1; function startDebugging(port, adb_conn, ip) { - global.inDebug = true; - try { + let attempts = 1; + global.inDebug.tizenDebug = true; + const connectionInterval = setInterval(() => { fetch(`http://${ip}:${port}/json`).then( res => res.json() ).then( debuggerJson => { + global.inDebug.webDebug = true; + global.currentClient.send(JSON.stringify({ type: 'canLaunchModules' })); + clearInterval(connectionInterval); return attachDebugger(debuggerJson[0].webSocketDebuggerUrl, adb_conn); - }); - } catch (error) { - global.inDebug = false; - console.log('Error attaching debugger:', error.message); - adb_conn._stream.end(); - } + }).catch( + e => { + if (attempts >= 5) { + global.currentClient.send(JSON.stringify({ type: 'error', message: 'Failed to connect to debugger.' })); + clearInterval(connectionInterval); + global.inDebug.tizenDebug = false; + adb_conn._stream.end(); + return; + } + attempts++; + }); + }, 1250); } function attachDebugger(wsUrl, adb_conn) { @@ -46,12 +56,18 @@ function attachDebugger(wsUrl, adb_conn) { client.onclose = () => { global.currentModule = null; - global.inDebug = false; + global.inDebug = { + tizenDebug: false, + webDebug: false + };; } client.onerror = () => { global.currentModule = null; - global.inDebug = false; + global.inDebug = { + tizenDebug: false, + webDebug: false + };; } return client; } diff --git a/tizenbrew-app/TizenBrew/service/service.js b/tizenbrew-app/TizenBrew/service/service.js index 7ba45c2..961647c 100644 --- a/tizenbrew-app/TizenBrew/service/service.js +++ b/tizenbrew-app/TizenBrew/service/service.js @@ -11,7 +11,11 @@ module.exports.onStart = function () { const server = new WebSocket.Server({ port: 8081 }); let adb; - global.inDebug = false; + global.inDebug = { + tizenDebug: false, + webDebug: false + }; + global.currentClient = null; function createAdbConnection(isTizen3, ip) { if (adb) { @@ -37,8 +41,8 @@ module.exports.onStart = function () { }); }); - adb._stream.on('error', () => { - console.log('ADB connection error.'); + adb._stream.on('error', (e) => { + console.log('ADB connection error. ' + e); }); adb._stream.on('close', () => { console.log('ADB connection closed.'); @@ -47,6 +51,7 @@ module.exports.onStart = function () { } server.on('connection', (ws) => { + global.currentClient = ws; ws.on('message', (msg) => { let message; try { @@ -61,7 +66,9 @@ module.exports.onStart = function () { break; } case 'relaunchInDebug': { - createAdbConnection(message.isTizen3, message.tvIp); + setTimeout(() => { + createAdbConnection(message.isTizen3, message.tvIp); + }, 1000); break; } case 'loadModules': {