implement auto port detect for web bridge

This commit is contained in:
Gregor Biswanger
2017-10-06 05:46:01 +02:00
parent 9ed69cd65c
commit 21cb393bc0
5 changed files with 40 additions and 33 deletions

View File

@@ -3,5 +3,6 @@
public static class BridgeSettings
{
public static string SocketPort { get; set; }
public static string WebPort { get; set; }
}
}

View File

@@ -13,12 +13,16 @@ namespace ElectronNET.API
{
BridgeSettings.SocketPort = argument.ToUpper().Replace("/ELECTRONPORT=", "");
Console.WriteLine("Use Electron Port: " + BridgeSettings.SocketPort);
} else if(argument.ToUpper().Contains("ELECTRONWEBPORT"))
{
BridgeSettings.WebPort = argument.ToUpper().Replace("/ELECTRONWEBPORT=", "");
}
}
if(IsElectronActive())
{
builder.UseContentRoot(AppDomain.CurrentDomain.BaseDirectory);
builder.UseContentRoot(AppDomain.CurrentDomain.BaseDirectory)
.UseUrls("http://0.0.0.0:" + BridgeSettings.WebPort);
}
return builder;

View File

@@ -2,12 +2,11 @@ const { app, BrowserWindow, Notification } = require('electron');
const fs = require('fs');
const path = require('path');
const process = require('child_process').spawn;
let io, window, apiProcess;
const portfinder = require('portfinder');
const portfinder = require('detect-port');
let io, window, apiProcess, loadURL;
app.on('ready', () => {
portfinder.getPort((error, port) => {
portfinder(8000, (error, port) => {
startSocketApiBridge(port);
});
});
@@ -24,7 +23,7 @@ function startSocketApiBridge(port) {
options.show = true;
window = new BrowserWindow(options);
window.loadURL('http://localhost:5000');
window.loadURL(loadURL);
window.on('closed', function () {
mainWindow = null;
@@ -39,18 +38,22 @@ function startSocketApiBridge(port) {
});
}
function startAspCoreBackend(port) {
// run server
var binPath = path.join(__dirname, 'bin');
fs.readdir(binPath, (error, files) => {
const exeFiles = files.filter((name) => name.indexOf('.exe') > -1);
const exeFileName = exeFiles[0];
const apipath = path.join(binPath, exeFileName);
apiProcess = process(apipath, ['/electronPort=' + port]);
function startAspCoreBackend(electronPort) {
portfinder(8000, (error, electronWebPort) => {
loadURL = `http://localhost:${electronWebPort}`
const arguments = [`/electronPort=${electronPort}`, `/electronWebPort=${electronWebPort}`];
apiProcess.stdout.on('data', (data) => {
var text = data.toString();
console.log(`stdout: ${data.toString()}`);
var binPath = path.join(__dirname, 'bin');
fs.readdir(binPath, (error, files) => {
const exeFiles = files.filter((name) => name.indexOf('.exe') > -1);
const exeFileName = exeFiles[0];
const apipath = path.join(binPath, exeFileName);
apiProcess = process(apipath, arguments);
apiProcess.stdout.on('data', (data) => {
var text = data.toString();
console.log(`stdout: ${data.toString()}`);
});
});
});
}

View File

@@ -18,6 +18,11 @@
"negotiator": "0.6.1"
}
},
"address": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/address/-/address-1.0.3.tgz",
"integrity": "sha512-z55ocwKBRLryBs394Sm3ushTtBeg6VAeuku7utSoSnsJKvKcnXFIyC6vh27n3rXyxSgkJBBCAvyOn7gSUcTYjg=="
},
"after": {
"version": "0.8.2",
"resolved": "https://registry.npmjs.org/after/-/after-0.8.2.tgz",
@@ -59,11 +64,6 @@
"resolved": "https://registry.npmjs.org/assert-plus/-/assert-plus-1.0.0.tgz",
"integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU="
},
"async": {
"version": "1.5.2",
"resolved": "https://registry.npmjs.org/async/-/async-1.5.2.tgz",
"integrity": "sha1-7GphrlZIDAw8skHJVhjiCJL5Zyo="
},
"asynckit": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
@@ -311,6 +311,15 @@
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
"integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk="
},
"detect-port": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/detect-port/-/detect-port-1.2.1.tgz",
"integrity": "sha512-2KWLTLsfpi/oYPGNBEniPcFzr1GW/s+Xq/4hJmTQRdE8ULuRwGnRPuVhS/cf+Z4ZEXNo7EO2f6oydHJQd94KMg==",
"requires": {
"address": "1.0.3",
"debug": "2.6.9"
}
},
"ecc-jsbn": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/ecc-jsbn/-/ecc-jsbn-0.1.1.tgz",
@@ -963,16 +972,6 @@
"pinkie": "2.0.4"
}
},
"portfinder": {
"version": "1.0.13",
"resolved": "https://registry.npmjs.org/portfinder/-/portfinder-1.0.13.tgz",
"integrity": "sha1-uzLs2HwnEErm7kS1o8y/Drsa7ek=",
"requires": {
"async": "1.5.2",
"debug": "2.6.9",
"mkdirp": "0.5.0"
}
},
"pretty-bytes": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/pretty-bytes/-/pretty-bytes-1.0.4.tgz",

View File

@@ -10,8 +10,8 @@
"author": "",
"license": "ISC",
"dependencies": {
"detect-port": "^1.2.1",
"electron": "^1.7.8",
"portfinder": "^1.0.13",
"socket.io": "^2.0.3"
}
}