Skip to content

Commit b00adcb

Browse files
feat: Add ElectronHostHook sample application (#967)
1 parent ed6d24d commit b00adcb

File tree

9 files changed

+185
-0
lines changed

9 files changed

+185
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using ElectronNET.API;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
namespace ElectronNET.Samples.ElectronHostHook.Controllers
5+
{
6+
public class HomeController : Controller
7+
{
8+
public async Task<IActionResult> Index()
9+
{
10+
string message = "Electron not active";
11+
if (HybridSupport.IsElectronActive)
12+
{
13+
// Call the HostHook defined in ElectronHostHook/index.ts
14+
var result = await Electron.HostHook.CallAsync<string>("ping", "Hello from C#");
15+
message = $"Sent 'Hello from C#', Received: '{result}'";
16+
}
17+
18+
return View("Index", message);
19+
}
20+
}
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Socket } from "socket.io";
2+
3+
export class Connector {
4+
constructor(private socket: Socket, public app: any) {
5+
}
6+
7+
on(key: string, javaScriptCode: Function): void {
8+
this.socket.on(key, (...args: any[]) => {
9+
const id: string = args.pop();
10+
const done = (result: any) => {
11+
this.socket.emit(id, result);
12+
};
13+
14+
args = [...args, done];
15+
javaScriptCode(...args);
16+
});
17+
}
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Connector } from "./connector";
2+
import { Socket } from "socket.io";
3+
4+
export class HookService extends Connector {
5+
constructor(socket: Socket, public app: any) {
6+
super(socket, app);
7+
}
8+
9+
onHostReady(): void {
10+
// execute your own JavaScript Host logic here
11+
this.on("ping", (msg, done) => {
12+
console.log("Received ping from C#:", msg);
13+
done("pong: " + msg);
14+
});
15+
}
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"noImplicitAny": false,
5+
"noEmitOnError": true,
6+
"removeComments": false,
7+
"sourceMap": true,
8+
"target": "es5",
9+
"lib": ["es2015", "dom"]
10+
},
11+
"exclude": ["node_modules", "wwwroot"]
12+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
<PropertyGroup>
3+
<ElectronNetDevMode>true</ElectronNetDevMode>
4+
</PropertyGroup>
5+
6+
<Import Project="..\ElectronNET\build\ElectronNET.Core.props" Condition="$(ElectronNetDevMode)" />
7+
8+
<PropertyGroup>
9+
<TargetFramework>net8.0</TargetFramework>
10+
<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
11+
<AspNetCoreModuleName>AspNetCoreModule</AspNetCoreModuleName>
12+
<IsPackable>false</IsPackable>
13+
<TypeScriptModuleKind>commonjs</TypeScriptModuleKind>
14+
<TypeScriptUseNodeJS>true</TypeScriptUseNodeJS>
15+
<TypeScriptTSConfig>ElectronHostHook/tsconfig.json</TypeScriptTSConfig>
16+
<TypeScriptCompileOnSaveEnabled>true</TypeScriptCompileOnSaveEnabled>
17+
<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
18+
</PropertyGroup>
19+
20+
<ItemGroup>
21+
<TypeScriptCompile Remove="**\node_modules\**" />
22+
</ItemGroup>
23+
24+
<ItemGroup>
25+
<ProjectReference Include="..\ElectronNET.API\ElectronNET.API.csproj" Condition="$(ElectronNetDevMode)" />
26+
<ProjectReference Include="..\ElectronNET.AspNet\ElectronNET.AspNet.csproj" Condition="$(ElectronNetDevMode)" />
27+
</ItemGroup>
28+
29+
<ItemGroup>
30+
<PackageReference Include="ElectronNET.Core" Version="0.2.0" Condition="'$(ElectronNetDevMode)' != 'true'" />
31+
<PackageReference Include="ElectronNET.Core.AspNet" Version="0.2.0" Condition="'$(ElectronNetDevMode)' != 'true'" />
32+
<PackageReference Include="Microsoft.TypeScript.MSBuild" Version="5.9.3" />
33+
</ItemGroup>
34+
35+
<Import Project="..\ElectronNET\build\ElectronNET.Core.targets" Condition="$(ElectronNetDevMode)" />
36+
</Project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using ElectronNET.API;
2+
3+
namespace ElectronNET.Samples.ElectronHostHook
4+
{
5+
public class Program
6+
{
7+
public static void Main(string[] args)
8+
{
9+
var builder = WebApplication.CreateBuilder(args);
10+
11+
builder.WebHost.UseElectron(args);
12+
builder.Services.AddElectron();
13+
builder.Services.AddControllersWithViews();
14+
15+
var app = builder.Build();
16+
17+
app.UseStaticFiles();
18+
app.UseRouting();
19+
20+
app.MapControllerRoute(
21+
name: "default",
22+
pattern: "{controller=Home}/{action=Index}/{id?}");
23+
24+
if (HybridSupport.IsElectronActive)
25+
{
26+
Task.Run(async () =>
27+
{
28+
var window = await Electron.WindowManager.CreateWindowAsync();
29+
});
30+
}
31+
32+
app.Run();
33+
}
34+
}
35+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"profiles": {
3+
"ElectronNET.Samples.ElectronHostHook": {
4+
"commandName": "Project",
5+
"launchBrowser": false,
6+
"applicationUrl": "http://localhost:5000",
7+
"environmentVariables": {
8+
"ASPNETCORE_ENVIRONMENT": "Development"
9+
}
10+
}
11+
}
12+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
@model string
2+
@{
3+
Layout = null;
4+
}
5+
6+
<!DOCTYPE html>
7+
<html>
8+
<head>
9+
<meta name="viewport" content="width=device-width" />
10+
<title>ElectronHostHook Sample</title>
11+
<style>
12+
body { font-family: sans-serif; padding: 20px; }
13+
.result { padding: 10px; background-color: #f0f0f0; border: 1px solid #ccc; margin-top: 10px; }
14+
</style>
15+
</head>
16+
<body>
17+
<h1>ElectronHostHook Sample</h1>
18+
<p>This sample demonstrates bidirectional communication between C# and the Electron Host process.</p>
19+
20+
<div class="result">
21+
<strong>Result:</strong> @Model
22+
</div>
23+
</body>
24+
</html>

src/ElectronNET.sln

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ElectronNET.IntegrationTest
6262
EndProject
6363
Project("{54A90642-561A-4BB1-A94E-469ADEE60C69}") = "ElectronNET.Host", "ElectronNET.Host\ElectronNET.Host.esproj", "{1C5FD66E-A1C6-C436-DF7C-3ECE4FEDDFE6}"
6464
EndProject
65+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ElectronNET.Samples.ElectronHostHook", "ElectronNET.Samples.ElectronHostHook\ElectronNET.Samples.ElectronHostHook.csproj", "{B8D65F3A-7E54-4632-9F1C-46679237B312}"
66+
ProjectSection(ProjectDependencies) = postProject
67+
{1C5FD66E-A1C6-C436-DF7C-3ECE4FEDDFE6} = {1C5FD66E-A1C6-C436-DF7C-3ECE4FEDDFE6}
68+
{8860606D-6847-F22A-5AED-DF4E0984DD24} = {8860606D-6847-F22A-5AED-DF4E0984DD24}
69+
EndProjectSection
70+
EndProject
6571
Global
6672
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6773
Debug|Any CPU = Debug|Any CPU
@@ -105,6 +111,10 @@ Global
105111
{1C5FD66E-A1C6-C436-DF7C-3ECE4FEDDFE6}.Release|Any CPU.ActiveCfg = Release|Any CPU
106112
{1C5FD66E-A1C6-C436-DF7C-3ECE4FEDDFE6}.Release|Any CPU.Build.0 = Release|Any CPU
107113
{1C5FD66E-A1C6-C436-DF7C-3ECE4FEDDFE6}.Release|Any CPU.Deploy.0 = Release|Any CPU
114+
{B8D65F3A-7E54-4632-9F1C-46679237B312}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
115+
{B8D65F3A-7E54-4632-9F1C-46679237B312}.Debug|Any CPU.Build.0 = Debug|Any CPU
116+
{B8D65F3A-7E54-4632-9F1C-46679237B312}.Release|Any CPU.ActiveCfg = Release|Any CPU
117+
{B8D65F3A-7E54-4632-9F1C-46679237B312}.Release|Any CPU.Build.0 = Release|Any CPU
108118
EndGlobalSection
109119
GlobalSection(SolutionProperties) = preSolution
110120
HideSolutionNode = FALSE
@@ -120,6 +130,7 @@ Global
120130
{06CAADC7-DE5B-47B4-AB2A-E9501459A2D1} = {D36CDFFD-3438-42E4-A7FF-88BA19AC4964}
121131
{AE877E48-6B44-63C2-8EA0-DB58D096B553} = {75129C45-FC6F-41B0-A485-07F4A7E031ED}
122132
{1C5FD66E-A1C6-C436-DF7C-3ECE4FEDDFE6} = {1BB6F634-2831-4496-83A6-BC6761DCEC8D}
133+
{B8D65F3A-7E54-4632-9F1C-46679237B312} = {EDCBFC49-2AEE-4BAF-9368-4409298C52FC}
123134
EndGlobalSection
124135
GlobalSection(ExtensibilityGlobals) = postSolution
125136
SolutionGuid = {81A62E71-9E04-4EFE-AD5C-23165375F8EF}

0 commit comments

Comments
 (0)