mirror of
https://github.com/ElectronNET/Electron.NET.git
synced 2026-07-19 07:06:26 +00:00
implement auto port support for socket bridge communication
This commit is contained in:
@@ -19,7 +19,7 @@ namespace ElectronNET.API
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||
};
|
||||
|
||||
_socket = IO.Socket("http://localhost:3000");
|
||||
_socket = IO.Socket("http://localhost:" + BridgeSettings.SocketPort);
|
||||
_socket.On(Socket.EVENT_CONNECT, () =>
|
||||
{
|
||||
Console.WriteLine("Verbunden!");
|
||||
|
||||
7
ElectronNET.API/BridgeSettings.cs
Normal file
7
ElectronNET.API/BridgeSettings.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace ElectronNET.API
|
||||
{
|
||||
public static class BridgeSettings
|
||||
{
|
||||
public static string SocketPort { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -11,5 +11,11 @@
|
||||
<PackageReference Include="SocketIoClientDotNet" Version="1.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.AspNetCore.Hosting.Abstractions">
|
||||
<HintPath>..\..\..\..\..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.hosting.abstractions\2.0.0\lib\netstandard2.0\Microsoft.AspNetCore.Hosting.Abstractions.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
</Project>
|
||||
|
||||
32
ElectronNET.API/WebHostBuilderExtensions.cs
Normal file
32
ElectronNET.API/WebHostBuilderExtensions.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using System;
|
||||
|
||||
namespace ElectronNET.API
|
||||
{
|
||||
public static class WebHostBuilderExtensions
|
||||
{
|
||||
public static IWebHostBuilder UseElectron(this IWebHostBuilder builder, string[] args)
|
||||
{
|
||||
foreach (string argument in args)
|
||||
{
|
||||
if (argument.ToUpper().Contains("ELECTRONPORT"))
|
||||
{
|
||||
BridgeSettings.SocketPort = argument.ToUpper().Replace("/ELECTRONPORT=", "");
|
||||
Console.WriteLine("Use Electron Port: " + BridgeSettings.SocketPort);
|
||||
}
|
||||
}
|
||||
|
||||
if(IsElectronActive())
|
||||
{
|
||||
builder.UseContentRoot(AppDomain.CurrentDomain.BaseDirectory);
|
||||
}
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
private static bool IsElectronActive()
|
||||
{
|
||||
return !string.IsNullOrEmpty(BridgeSettings.SocketPort);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -45,7 +45,7 @@ namespace ElectronNET.CLI.Commands
|
||||
Console.WriteLine("Executing electron magic in this directory: " + buildPath);
|
||||
|
||||
// Need a solution for --asar support
|
||||
ProcessHelper.CmdExecute($"electron-packager . --platform=win32 --arch=x64 --electronVersion=1.7.8 --out=\"{buildPath}\" --overwrite", tempPath);
|
||||
ProcessHelper.CmdExecute($"electron-packager . --platform=win32 --arch=x64 --out=\"{buildPath}\" --overwrite", tempPath);
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
@@ -55,7 +55,6 @@ namespace ElectronNET.CLI.Commands
|
||||
|
||||
Console.WriteLine("Start npm install...");
|
||||
ProcessHelper.CmdExecute("npm install", tempPath);
|
||||
ProcessHelper.CmdExecute("npm install electron@1.7.8", tempPath);
|
||||
|
||||
ProcessHelper.CmdExecute(@"electron.cmd ""..\..\main.js""", Path.Combine(tempPath, "node_modules", ".bin"), false, false);
|
||||
|
||||
|
||||
1411
ElectronNET.CLI/ElectronHost/package-lock.json
generated
1411
ElectronNET.CLI/ElectronHost/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -1,15 +0,0 @@
|
||||
{
|
||||
"name": "ElectronNET.Host",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"socket.io": "^2.0.3"
|
||||
}
|
||||
}
|
||||
@@ -18,8 +18,8 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="ElectronHost\package-lock.json" />
|
||||
<EmbeddedResource Include="ElectronHost\package.json" />
|
||||
<EmbeddedResource Include="..\ElectronNET.Host\package-lock.json" Link="ElectronHost\package-lock.json" />
|
||||
<EmbeddedResource Include="..\ElectronNET.Host\package.json" Link="ElectronHost\package.json" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,50 +1,59 @@
|
||||
const { app, BrowserWindow, Notification } = require('electron');
|
||||
const io = require('socket.io')(3000);
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const process = require('child_process').spawn;
|
||||
let window;
|
||||
let apiProcess;
|
||||
let io, window, apiProcess;
|
||||
|
||||
const portfinder = require('portfinder');
|
||||
|
||||
app.on('ready', () => {
|
||||
portfinder.getPort((error, port) => {
|
||||
startSocketApiBridge(port);
|
||||
});
|
||||
});
|
||||
|
||||
function startSocketApiBridge(port) {
|
||||
io = require('socket.io')(port);
|
||||
startAspCoreBackend(port);
|
||||
|
||||
io.on('connection', (socket) => {
|
||||
console.log('ASP.NET Core Application connected...');
|
||||
|
||||
socket.on('createBrowserWindow', (options) => {
|
||||
console.log(options);
|
||||
options.show = true;
|
||||
|
||||
window = new BrowserWindow(options);
|
||||
window.loadURL('http://localhost:5000');
|
||||
|
||||
window.on('closed', function () {
|
||||
mainWindow = null;
|
||||
apiProcess = null;
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('createNotification', (options) => {
|
||||
const notification = new Notification(options);
|
||||
notification.show();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
apiProcess = process(apipath, ['/electronPort=' + port]);
|
||||
|
||||
apiProcess.stdout.on('data', (data) => {
|
||||
var text = data.toString();
|
||||
console.log(`stdout: ${data.toString()}`);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
io.on('connection', (socket) => {
|
||||
console.log('ASP.NET Core Application connected...');
|
||||
|
||||
socket.on('createBrowserWindow', (options) => {
|
||||
console.log(options);
|
||||
options.show = true;
|
||||
|
||||
window = new BrowserWindow(options);
|
||||
window.loadURL('http://localhost:5000');
|
||||
|
||||
window.on('closed', function () {
|
||||
mainWindow = null;
|
||||
apiProcess = null;
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('createNotification', (options) => {
|
||||
const notification = new Notification(options);
|
||||
notification.show();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Quit when all windows are closed.
|
||||
app.on('window-all-closed', () => {
|
||||
|
||||
15
ElectronNET.Host/package-lock.json
generated
15
ElectronNET.Host/package-lock.json
generated
@@ -59,6 +59,11 @@
|
||||
"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",
|
||||
@@ -958,6 +963,16 @@
|
||||
"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",
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"electron": "^1.7.8",
|
||||
"portfinder": "^1.0.13",
|
||||
"socket.io": "^2.0.3"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Microsoft.AspNetCore;
|
||||
using ElectronNET.API;
|
||||
using Microsoft.AspNetCore;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
@@ -22,7 +23,7 @@ namespace ElectronNET.WebApp
|
||||
//{
|
||||
//Console.WriteLine("Test Switch for Electron detection: " + args[0]);
|
||||
return WebHost.CreateDefaultBuilder(args)
|
||||
.UseContentRoot(AppDomain.CurrentDomain.BaseDirectory)
|
||||
.UseElectron(args)
|
||||
.UseStartup<Startup>()
|
||||
.Build();
|
||||
//}
|
||||
|
||||
@@ -10,13 +10,14 @@
|
||||
"profiles": {
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"commandLineArgs": "/electronPort=3000",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"ElectronNET.WebApp": {
|
||||
"commandName": "Project",
|
||||
"commandLineArgs": "/electronPort=3000",
|
||||
"launchBrowser": true,
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
@@ -24,4 +25,4 @@
|
||||
"applicationUrl": "http://localhost:50395/"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user