-
-
Notifications
You must be signed in to change notification settings - Fork 745
feat: Add ElectronHostHook sample application (#967) #968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
FlorianRappl
merged 2 commits into
ElectronNET:main
from
adityashirsatrao007:feat/host-hook-sample
Dec 9, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
src/ElectronNET.Samples.ElectronHostHook/Controllers/HomeController.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
| } | ||
| } |
3 changes: 3 additions & 0 deletions
3
src/ElectronNET.Samples.ElectronHostHook/ElectronHostHook/.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| node_modules | ||
| *.js | ||
| *.js.map |
21 changes: 21 additions & 0 deletions
21
src/ElectronNET.Samples.ElectronHostHook/ElectronHostHook/connector.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| 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(); | ||
| try { | ||
| javaScriptCode(...args, (data) => { | ||
| if (data) { | ||
| this.socket.emit(`${key}Complete${id}`, data); | ||
| } | ||
| }); | ||
| } catch (error) { | ||
| this.socket.emit(`${key}Error${id}`, `Host Hook Exception`, error); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
16 changes: 16 additions & 0 deletions
16
src/ElectronNET.Samples.ElectronHostHook/ElectronHostHook/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { Connector } from "./connector"; | ||
softworkz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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); | ||
| }); | ||
| } | ||
| } | ||
12 changes: 12 additions & 0 deletions
12
src/ElectronNET.Samples.ElectronHostHook/ElectronHostHook/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "name": "electron-host-hook", | ||
| "version": "1.0.0", | ||
| "description": "Connector for Electron.NET projects.", | ||
| "main": "index.js", | ||
| "dependencies": { | ||
| "socket.io": "^4.8.1" | ||
| }, | ||
| "devDependencies": { | ||
| "typescript": "^5.9.3" | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/ElectronNET.Samples.ElectronHostHook/ElectronHostHook/tsconfig.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "module": "commonjs", | ||
| "target": "ES2019", | ||
| "sourceMap": true, | ||
| "skipLibCheck": true | ||
| }, | ||
| "exclude": ["node_modules"] | ||
| } |
36 changes: 36 additions & 0 deletions
36
src/ElectronNET.Samples.ElectronHostHook/ElectronNET.Samples.ElectronHostHook.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| 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, async () => | ||
| { | ||
| var window = await Electron.WindowManager.CreateWindowAsync(); | ||
| }); | ||
|
|
||
| builder.Services.AddElectron(); | ||
| builder.Services.AddControllersWithViews(); | ||
|
|
||
| var app = builder.Build(); | ||
|
|
||
| app.UseStaticFiles(); | ||
| app.UseRouting(); | ||
|
|
||
| app.MapControllerRoute( | ||
| name: "default", | ||
| pattern: "{controller=Home}/{action=Index}/{id?}"); | ||
|
|
||
| app.Run(); | ||
| } | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/ElectronNET.Samples.ElectronHostHook/Properties/launchSettings.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "profiles": { | ||
| "ElectronNET.Samples.ElectronHostHook": { | ||
| "commandName": "Project", | ||
| "launchBrowser": false, | ||
| "applicationUrl": "http://localhost:5000", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| } | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
src/ElectronNET.Samples.ElectronHostHook/Views/Home/Index.cshtml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| @model string | ||
| @{ | ||
| Layout = null; | ||
| } | ||
|
|
||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
softworkz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| <meta charset="utf-8"> | ||
| <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> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.