Skip to content

Commit e854451

Browse files
authored
Merge pull request #968 from adityashirsatrao007/feat/host-hook-sample
feat: Add ElectronHostHook sample application (#967)
2 parents ed6d24d + 331c2f5 commit e854451

File tree

11 files changed

+197
-0
lines changed

11 files changed

+197
-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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
*.js
3+
*.js.map
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
try {
11+
javaScriptCode(...args, (data) => {
12+
if (data) {
13+
this.socket.emit(`${key}Complete${id}`, data);
14+
}
15+
});
16+
} catch (error) {
17+
this.socket.emit(`${key}Error${id}`, `Host Hook Exception`, error);
18+
}
19+
});
20+
}
21+
}
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+
"name": "electron-host-hook",
3+
"version": "1.0.0",
4+
"description": "Connector for Electron.NET projects.",
5+
"main": "index.js",
6+
"dependencies": {
7+
"socket.io": "^4.8.1"
8+
},
9+
"devDependencies": {
10+
"typescript": "^5.9.3"
11+
}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"target": "ES2019",
5+
"sourceMap": true,
6+
"skipLibCheck": true
7+
},
8+
"exclude": ["node_modules"]
9+
}
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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, async () =>
12+
{
13+
var window = await Electron.WindowManager.CreateWindowAsync();
14+
});
15+
16+
builder.Services.AddElectron();
17+
builder.Services.AddControllersWithViews();
18+
19+
var app = builder.Build();
20+
21+
app.UseStaticFiles();
22+
app.UseRouting();
23+
24+
app.MapControllerRoute(
25+
name: "default",
26+
pattern: "{controller=Home}/{action=Index}/{id?}");
27+
28+
app.Run();
29+
}
30+
}
31+
}
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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@model string
2+
@{
3+
Layout = null;
4+
}
5+
6+
<!DOCTYPE html>
7+
<html lang="en">
8+
<head>
9+
<meta charset="utf-8">
10+
<meta name="viewport" content="width=device-width" />
11+
<title>ElectronHostHook Sample</title>
12+
<style>
13+
body { font-family: sans-serif; padding: 20px; }
14+
.result { padding: 10px; background-color: #f0f0f0; border: 1px solid #ccc; margin-top: 10px; }
15+
</style>
16+
</head>
17+
<body>
18+
<h1>ElectronHostHook Sample</h1>
19+
<p>This sample demonstrates bidirectional communication between C# and the Electron Host process.</p>
20+
21+
<div class="result">
22+
<strong>Result:</strong> @Model
23+
</div>
24+
</body>
25+
</html>

0 commit comments

Comments
 (0)