This commit is contained in:
Slava Kim
2014-09-25 21:01:40 -07:00
parent 448ea0ea7f
commit d68aa09c11
3 changed files with 13 additions and 48 deletions

View File

@@ -38,23 +38,16 @@ var writeFile = function (directoryPath, fileName, content, cb) {
var restartServer = function (location) {
console.log(DEBUG_TAG + 'restartserver with location ' + location);
var fail = function (err) { console.log(DEBUG_TAG + 'something failed: ' + err.message) };
var httpd = cordova && cordova.plugins && cordova.plugins.CorHttpd;
var httpd = cordova && cordova.plugins && cordova.plugins.CordovaUpdate;
if (! httpd) {
fail(new Error('no httpd'));
return;
}
var startServer = function (cordovajsRoot, prevUrl) {
var port;
if (prevUrl) {
var parts = prevUrl.split(':');
if (parts.length)
port = parseInt(parts[parts.length - 1], 10);
}
var startServer = function (cordovajsRoot) {
httpd.startServer({
'www_root' : location,
'port' : port,
'cordovajs_root': cordovajsRoot
}, function (url) {
Package.reload.Reload._reload();
@@ -62,17 +55,7 @@ var restartServer = function (location) {
};
httpd.getCordovajsRoot(function (cordovajsRoot) {
httpd.getURL(function (url) {
if (url.length > 0) {
// already have a server running, stop it
httpd.stopServer(function () {
startServer(cordovajsRoot, url);
}, fail);
} else {
// just start a server
startServer(cordovajsRoot);
}
}, fail);
startServer(cordovajsRoot);
}, fail);
};

View File

@@ -68,5 +68,5 @@ Package.on_use(function(api) {
Cordova.depends({
'org.apache.cordova.device': '0.2.11',
'com.rjfun.cordova.httpd': 'https://github.com/meteor/cordova-httpd/tarball/f68bcb43be30878823718bcd01bd6d97561eb337'
'com.meteor.cordova-update': 'https://github.com/meteor/com.meteor.cordova-update/tarball/9eddfcc144e8eb523c2b8d317f8837346435b1a6'
});

View File

@@ -42,15 +42,10 @@
return p.slice(1);
};
var randomInt = function (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
var loadTries = 0;
var loadFromLocation = function (location) {
var cordovaRoot = decodeURI(window.location.href).replace(/\/index.html$/, '/').replace(/^file:\/\/?/, '');
var httpd = cordova && cordova.plugins && cordova.plugins.CorHttpd;
var port = randomInt(10000, 50000);
var httpd = cordova && cordova.plugins && cordova.plugins.CordovaUpdate;
var retry = function () {
loadTries++;
@@ -62,27 +57,14 @@
}
};
httpd.getURL(function(url){
if (url.length > 0) {
// if server is already running, stop it and retry
httpd.stopServer(retry, retry);
} else {
console.log(DEBUG_TAG + 'Starting the server on port ' + port);
httpd.startServer({
'www_root' : location,
'port' : port,
'cordovajs_root': cordovaRoot
}, function (url) {
// go to the new proxy url
window.location = url;
}, function (error) {
// failed to start a server, is port already in use?
retry();
});
}
}, function () {
// failed to call to server: retry
httpd.startServing({
'www_root' : location,
'cordovajs_root': cordovaRoot
}, function (url) {
// go to the new proxy url
window.location = url;
}, function (error) {
// failed to start a server, is port already in use?
retry();
});
};