Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 744
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| node_modules | ||
| *.js | ||
| *.js.map |
| 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); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { Connector } from "./connector"; | ||
softworkz marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| 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); | ||
| }); | ||
| } | ||
| } | ||
| 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" | ||
| } | ||
| } |
| 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"] | ||
| } |
| 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> |
| 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(); | ||
| } | ||
| } | ||
| } |
| 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" | ||
| } | ||
| } | ||
| } | ||
| } |
| 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.
Uh oh!There was an error while loading. Please reload this page. | ||
| <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> | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.