Create Electron-Class for API´s

This commit is contained in:
Gregor Biswanger
2017-10-14 17:58:16 +02:00
parent 9848a38b0d
commit 9abece156f
7 changed files with 281 additions and 238 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,19 @@
using Quobject.SocketIoClientDotNet.Client;
using System;
namespace ElectronNET.API
{
internal static class BridgeConnector
{
public static Socket Socket;
public static void StartConnection()
{
Socket = IO.Socket("http://localhost:" + BridgeSettings.SocketPort);
Socket.On(Socket.EVENT_CONNECT, () =>
{
Console.WriteLine("BridgeConnector connected!");
});
}
}
}

View File

@@ -0,0 +1,15 @@
namespace ElectronNET.API
{
public static class Electron
{
/// <summary>
/// Communicate asynchronously from the main process to renderer processes.
/// </summary>
public static IpcMain IpcMain { get { return IpcMain.Instance; } }
/// <summary>
/// Control your application's event lifecycle.
/// </summary>
public static App App { get { return App.Instance; } }
}
}

View File

@@ -1,20 +1,29 @@
using System;
using Quobject.SocketIoClientDotNet.Client;
namespace ElectronNET.API
{
/// <summary>
/// Communicate asynchronously from the main process to renderer processes.
/// </summary>
public class IpcMain
public sealed class IpcMain
{
private Socket _socket;
private static IpcMain _ipcMain;
public IpcMain(Socket socket)
private IpcMain() { }
public static IpcMain Instance
{
_socket = socket;
get
{
if(_ipcMain == null)
{
_ipcMain = new IpcMain();
}
return _ipcMain;
}
}
/// <summary>
/// Listens to channel, when a new message arrives listener would be called with
/// listener(event, args...).
@@ -23,8 +32,8 @@ namespace ElectronNET.API
/// <param name="listener">Callback Method.</param>
public void On(string channel, Action<object> listener)
{
_socket.Emit("registerIpcMainChannel", channel);
_socket.On(channel, listener);
BridgeConnector.Socket.Emit("registerIpcMainChannel", channel);
BridgeConnector.Socket.On(channel, listener);
}
/// <summary>
@@ -35,8 +44,8 @@ namespace ElectronNET.API
/// <param name="listener">Callback Method.</param>
public void Once(string channel, Action<object> listener)
{
_socket.Emit("registerOnceIpcMainChannel", channel);
_socket.On(channel, listener);
BridgeConnector.Socket.Emit("registerOnceIpcMainChannel", channel);
BridgeConnector.Socket.On(channel, listener);
}
/// <summary>
@@ -45,7 +54,7 @@ namespace ElectronNET.API
/// <param name="channel">Channelname.</param>
public void RemoveAllListeners(string channel)
{
_socket.Emit("removeAllListenersIpcMainChannel", channel);
BridgeConnector.Socket.Emit("removeAllListenersIpcMainChannel", channel);
}
/// <summary>
@@ -58,7 +67,7 @@ namespace ElectronNET.API
/// <param name="data">Arguments data.</param>
public void Send(string channel, params object[] data)
{
_socket.Emit("sendToIpcRenderer", channel, data);
BridgeConnector.Socket.Emit("sendToIpcRenderer", channel, data);
}
}
}

View File

@@ -23,6 +23,8 @@ namespace ElectronNET.API
{
builder.UseContentRoot(AppDomain.CurrentDomain.BaseDirectory)
.UseUrls("http://0.0.0.0:" + BridgeSettings.WebPort);
BridgeConnector.StartConnection();
}
return builder;

View File

@@ -1,8 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using ElectronNET.API;
using ElectronNET.API.Entities;
using System;
using System.IO;
namespace ElectronNET.WebApp.Controllers
{
@@ -10,24 +8,24 @@ namespace ElectronNET.WebApp.Controllers
{
public IActionResult Index()
{
App.IpcMain.On("SayHello", (args) => {
App.CreateNotification(new NotificationOptions
Electron.IpcMain.On("SayHello", (args) => {
Electron.App.CreateNotification(new NotificationOptions
{
Title = "Hallo Robert",
Body = "Nachricht von ASP.NET Core App"
});
App.IpcMain.Send("Goodbye", "Elephant!");
Electron.IpcMain.Send("Goodbye", "Elephant!");
});
App.IpcMain.On("GetPath", async (args) =>
Electron.IpcMain.On("GetPath", async (args) =>
{
string pathName = await App.GetPathAsync(PathName.pictures);
string pathName = await Electron.App.GetPathAsync(PathName.pictures);
//App.IpcMain.Send("GetPathComplete", pathName);
var result = await App.GetPathAsync(PathName.exe);
var result = await Electron.App.GetPathAsync(PathName.exe);
//var imagePath = Path.Combine(result, "Electron.png");
App.IpcMain.Send("GetPathComplete", result);
Electron.IpcMain.Send("GetPathComplete", result);
//var image = await App.GetFileIconAsync(result);

View File

@@ -35,7 +35,7 @@ namespace ElectronNET.WebApp
template: "{controller=Home}/{action=Index}/{id?}");
});
App.OpenWindow(800, 600, true);
Electron.App.OpenWindow(800, 600, true);
}
}
}