refactored NvHTTP.init() to be less confusing. This closes #17

This commit is contained in:
R. Aidan Campbell 2016-03-11 13:38:00 -05:00
parent b2cbd36ec4
commit 028156609b
2 changed files with 78 additions and 90 deletions

View File

@ -32,8 +32,6 @@ function updateBitrateField() {
}
function moduleDidLoad() {
snackbarLog("NaCl module loaded.");
if(!pairingCert) { // we couldn't load a cert. Make one.
console.log("Failed to load local cert. Generating new one");
sendMessage('makeCert', []).then(function (cert) {
@ -41,12 +39,16 @@ function moduleDidLoad() {
pairingCert = cert;
console.log("Generated new cert.")
});
sendMessage('httpInit', [pairingCert.cert, pairingCert.privateKey, myUniqueid]);
}
if(!myUniqueid) {
console.log("Failed to get uniqueId. Generating new one");
myUniqueid = uniqueid();
storeData('uniqueid', myUniqueid, null);
}
sendMessage('httpInit', [pairingCert.cert, pairingCert.privateKey, myUniqueid]).then(function (ret) {
snackbarLog('Initialization complete.');
});
}
// because the user can change the target host at any time, we continually have to check
@ -71,15 +73,13 @@ function hideAllWorkflowDivs() {
// pair button was pushed. pass what the user entered into the GFEHostIPField.
function pairPushed() {
if(!pairingCert) {
console.log("User wants to pair, and we still have no cert. Problem = very yes.")
snackbarLog('ERROR: cert has not been generated yet. Is NaCL initialized?');
console.log("User wants to pair, and we still have no cert. Problem = very yes.");
return;
}
$('#pairButton')[0].innerHTML = 'Pairing...';
snackbarLog('Pairing...');
snackbarLog('Attempting pair to: ' + target);
updateTarget();
console.log("Attempting to pair to: " + target);
sendMessage('httpInit', [pairingCert.cert, pairingCert.privateKey, myUniqueid]).then(function (ret) {
console.log('httpInit function completed. it returned: ' + ret);
var randomNumber = String("0000" + (Math.random()*10000|0)).slice(-4);
var pairingDialog = document.querySelector('#pairingDialog');
document.getElementById('pairingDialogText').innerHTML =
@ -110,7 +110,6 @@ function pairPushed() {
document.getElementById('pairingDialogText').innerHTML = 'Error: Pairing failed with code: ' + ret2;
}
console.log("pairing attempt returned: " + ret2);
})
});
}
@ -132,9 +131,8 @@ function showAppsPushed() {
if (api.currentGame != 0) $('#selectGame')[0].value = api.currentGame;
});
} else {
sendMessage('httpInit', [pairingCert.cert, pairingCert.privateKey, myUniqueid]).then(function (ret) {
api = new NvHTTP(target, myUniqueid);
api.init().then(function (ret) {
api.refreshServerInfo().then(function (ret) {
api.getAppList().then(function (appList) {
for(var i = 0; i < appList.length; i++) { // programmatically add each app
var opt = document.createElement('option');
@ -146,7 +144,6 @@ function showAppsPushed() {
if (api.currentGame != 0) $('#selectGame')[0].value = api.currentGame;
});
});
});
}
showAppsMode();
}
@ -175,9 +172,8 @@ function startPushed() {
var rikey = '00000000000000000000000000000000';
var rikeyid = 0;
sendMessage('httpInit', [pairingCert.cert, pairingCert.privateKey, myUniqueid]).then(function (ret) {
api = new NvHTTP(target, myUniqueid);
api.init().then(function (ret) {
api.refreshServerInfo().then(function (ret) {
if (api.currentGame == 0) {
api.getAppByName("Steam").then(function (app) {
api.launchApp(app.id,
@ -190,14 +186,12 @@ function startPushed() {
sendMessage('startRequest', [target, streamWidth, streamHeight, frameRate, bitrate.toString(), api.serverMajorVersion.toString()]);
});
});
}
else {
} else {
api.resumeApp(rikey, rikeyid).then(function (ret) {
sendMessage('startRequest', [target, streamWidth, streamHeight, frameRate, bitrate.toString(), api.serverMajorVersion.toString()]);
});
}
});
});
// we just finished the gameSelection section. only expose the NaCl section
playGameMode();
@ -208,7 +202,11 @@ function startSelectedGame() {
// we're just grabbing the currently selected option from #selectGame, and feeding it into NvHTTP
// if we need to reconnect to the target, and `target` has been updated, we can pass the appID we listed from the previous target
// then everyone's sad. So we won't do that. Because the only way to see the startGame button is to list the apps for the target anyways.
if (api && api.paired) {
if(!api || !api.paired) {
api = new NvHTTP(target, myUniqueid);
}
// refresh the server info, because the user might have quit the game.
api.refreshServerInfo().then(function (ret) {
if(api.currentGame != 0) {
api.getAppById(api.currentGame).then(function (currentApp) {
snackbarLog('Error: ' + target + ' is already in app: ' + currentApp.title);
@ -236,14 +234,7 @@ function startSelectedGame() {
).then(function (ret) {
sendMessage('startRequest', [target, streamWidth, streamHeight, frameRate, bitrate.toString(), api.serverMajorVersion.toString()]);
});
} else { // time to reconnect.
sendMessage('httpInit', [pairingCert.cert, pairingCert.privateKey, myUniqueid]).then(function (ret) {
api = new NvHTTP(target, myUniqueid);
api.init().then(function (ret) {
return startSelectedGame(); // probably a horrible idea. Now that we're initialized, recurse so that we hit the `if` statement properly.
});
});
}
playGameMode();
}
@ -277,19 +268,16 @@ function fullscreenNaclModule() {
function stopPushed() {
sendMessage('stopRequested');
snackbarLog('stopRequested');
stopGame();
}
function stopGame() {
if (api && api.paired) {
return api.quitApp();
} else {
sendMessage('httpInit', [pairingCert.cert, pairingCert.privateKey, myUniqueid]).then(function (ret) {
if(!api || !api.paired) {
api = new NvHTTP(target, myUniqueid);
api.init().then(function (ret) {
}
api.refreshServerInfo().then(function (ret) {
return api.quitApp();
});
});
}
}
function storeData(key, data, callbackFunction) {

View File

@ -33,7 +33,7 @@ function NvHTTP(address, clientUid) {
};
NvHTTP.prototype = {
init: function () {
refreshServerInfo: function () {
return sendMessage('openUrl', [_self._baseUrlHttps+'/serverinfo?'+_self._buildUidStr()]).then(function(ret) {
$xml = _self._parseXML(ret);
$root = $xml.find('root')