feat: Add ElectronHostHook sample application (#967)

This commit is contained in:
Aditya
2025-12-07 22:57:35 +05:30
parent ed6d24dbb1
commit b00adcbd38
9 changed files with 185 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
using ElectronNET.API;
using Microsoft.AspNetCore.Mvc;
namespace ElectronNET.Samples.ElectronHostHook.Controllers
{
public class HomeController : Controller
{
public async Task<IActionResult> Index()
{
string message = "Electron not active";
if (HybridSupport.IsElectronActive)
{
// Call the HostHook defined in ElectronHostHook/index.ts
var result = await Electron.HostHook.CallAsync<string>("ping", "Hello from C#");
message = $"Sent 'Hello from C#', Received: '{result}'";
}
return View("Index", message);
}
}
}

View File

@@ -0,0 +1,18 @@
import { Socket } from "socket.io";
export class Connector {
constructor(private socket: Socket, public app: any) {
}
on(key: string, javaScriptCode: Function): void {
this.socket.on(key, (...args: any[]) => {
const id: string = args.pop();
const done = (result: any) => {
this.socket.emit(id, result);
};
args = [...args, done];
javaScriptCode(...args);
});
}
}

View File

@@ -0,0 +1,16 @@
import { Connector } from "./connector";
import { Socket } from "socket.io";
export class HookService extends Connector {
constructor(socket: Socket, public app: any) {
super(socket, app);
}
onHostReady(): void {
// execute your own JavaScript Host logic here
this.on("ping", (msg, done) => {
console.log("Received ping from C#:", msg);
done("pong: " + msg);
});
}
}

View File

@@ -0,0 +1,12 @@
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": false,
"sourceMap": true,
"target": "es5",
"lib": ["es2015", "dom"]
},
"exclude": ["node_modules", "wwwroot"]
}

View File

@@ -0,0 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<ElectronNetDevMode>true</ElectronNetDevMode>
</PropertyGroup>
<Import Project="..\ElectronNET\build\ElectronNET.Core.props" Condition="$(ElectronNetDevMode)" />
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
<AspNetCoreModuleName>AspNetCoreModule</AspNetCoreModuleName>
<IsPackable>false</IsPackable>
<TypeScriptModuleKind>commonjs</TypeScriptModuleKind>
<TypeScriptUseNodeJS>true</TypeScriptUseNodeJS>
<TypeScriptTSConfig>ElectronHostHook/tsconfig.json</TypeScriptTSConfig>
<TypeScriptCompileOnSaveEnabled>true</TypeScriptCompileOnSaveEnabled>
<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
</PropertyGroup>
<ItemGroup>
<TypeScriptCompile Remove="**\node_modules\**" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ElectronNET.API\ElectronNET.API.csproj" Condition="$(ElectronNetDevMode)" />
<ProjectReference Include="..\ElectronNET.AspNet\ElectronNET.AspNet.csproj" Condition="$(ElectronNetDevMode)" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="ElectronNET.Core" Version="0.2.0" Condition="'$(ElectronNetDevMode)' != 'true'" />
<PackageReference Include="ElectronNET.Core.AspNet" Version="0.2.0" Condition="'$(ElectronNetDevMode)' != 'true'" />
<PackageReference Include="Microsoft.TypeScript.MSBuild" Version="5.9.3" />
</ItemGroup>
<Import Project="..\ElectronNET\build\ElectronNET.Core.targets" Condition="$(ElectronNetDevMode)" />
</Project>

View File

@@ -0,0 +1,35 @@
using ElectronNET.API;
namespace ElectronNET.Samples.ElectronHostHook
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseElectron(args);
builder.Services.AddElectron();
builder.Services.AddControllersWithViews();
var app = builder.Build();
app.UseStaticFiles();
app.UseRouting();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
if (HybridSupport.IsElectronActive)
{
Task.Run(async () =>
{
var window = await Electron.WindowManager.CreateWindowAsync();
});
}
app.Run();
}
}
}

View File

@@ -0,0 +1,12 @@
{
"profiles": {
"ElectronNET.Samples.ElectronHostHook": {
"commandName": "Project",
"launchBrowser": false,
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,24 @@
@model string
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>ElectronHostHook Sample</title>
<style>
body { font-family: sans-serif; padding: 20px; }
.result { padding: 10px; background-color: #f0f0f0; border: 1px solid #ccc; margin-top: 10px; }
</style>
</head>
<body>
<h1>ElectronHostHook Sample</h1>
<p>This sample demonstrates bidirectional communication between C# and the Electron Host process.</p>
<div class="result">
<strong>Result:</strong> @Model
</div>
</body>
</html>