Skip to content

Commit

Permalink
fix: app not launching in debugging mode properly
Browse files Browse the repository at this point in the history
  • Loading branch information
reisxd committed Feb 29, 2024
1 parent 6f4372e commit 0a2fe30
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 18 deletions.
4 changes: 4 additions & 0 deletions tizenbrew-app/TizenBrew/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 11 additions & 2 deletions tizenbrew-app/TizenBrew/js/wsClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

let client;
const isTizen3 = navigator.userAgent.includes('Tizen 3.0');
let canLaunchModules = false;

function connect() {
const ip = localStorage.getItem('ip');
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions tizenbrew-app/TizenBrew/moduleManager.html
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ <h1 class="has-mb-1 has-text-white">Add</h1>
document.getElementById('appList').innerHTML += `
<div class="card has-w-96 is-shadowed column is-one-quarter is-full-mobile is-flex has-bg-primary item" id="add">
<div class="has-mb-12">
<h5 class="has-text-light">Set Server IP</h5>
<label class="label">Server IP:</label>
<h5 class="has-text-light">Add Module</h5>
<label class="label">Module Name (NPM Only):</label>
<input type="text" class="input" placeholder="@foxreis/tizentube" id="appName">
</div>
</div>
Expand Down
36 changes: 26 additions & 10 deletions tizenbrew-app/TizenBrew/service/debugger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down
15 changes: 11 additions & 4 deletions tizenbrew-app/TizenBrew/service/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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.');
Expand All @@ -47,6 +51,7 @@ module.exports.onStart = function () {
}

server.on('connection', (ws) => {
global.currentClient = ws;
ws.on('message', (msg) => {
let message;
try {
Expand All @@ -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': {
Expand Down

0 comments on commit 0a2fe30

Please sign in to comment.