mirror of
https://github.com/ElectronNET/Electron.NET.git
synced 2026-07-21 08:05:57 +00:00
Refactor: Introduce IFacade interface for SocketIO and SignalR facades
- Create IFacade interface defining common API for SocketIO and SignalR - Update SocketIoFacade to implement IFacade - Update SignalRFacade to implement IFacade (add Connect no-op) - Update RuntimeControllerBase.Socket to return IFacade - Update RuntimeControllerAspNetBase.Socket to return IFacade - Update RuntimeControllerDotNetFirst.Socket to return IFacade - Update ElectronNetRuntime.GetSocket() to return IFacade - Update BridgeConnector.Socket to return IFacade This enables polymorphic usage of both facades throughout the codebase and prepares for full Electron API integration with SignalR mode.
This commit is contained in:
@@ -2,9 +2,11 @@
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace ElectronNET.API
|
||||
{
|
||||
using ElectronNET.API.Bridge;
|
||||
|
||||
internal static class BridgeConnector
|
||||
{
|
||||
public static SocketIoFacade Socket
|
||||
public static IFacade Socket
|
||||
{
|
||||
get
|
||||
{
|
||||
|
||||
62
src/ElectronNET.API/Bridge/IFacade.cs
Normal file
62
src/ElectronNET.API/Bridge/IFacade.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
namespace ElectronNET.API.Bridge
|
||||
{
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
/// <summary>
|
||||
/// Common interface for communication facades (SocketIO and SignalR).
|
||||
/// Provides methods for bidirectional communication between .NET and Electron.
|
||||
/// </summary>
|
||||
internal interface IFacade
|
||||
{
|
||||
/// <summary>
|
||||
/// Raised when the bridge connection is established.
|
||||
/// </summary>
|
||||
event EventHandler BridgeConnected;
|
||||
|
||||
/// <summary>
|
||||
/// Raised when the bridge connection is lost.
|
||||
/// </summary>
|
||||
event EventHandler BridgeDisconnected;
|
||||
|
||||
/// <summary>
|
||||
/// Establishes the connection to Electron.
|
||||
/// </summary>
|
||||
void Connect();
|
||||
|
||||
/// <summary>
|
||||
/// Registers a persistent event handler.
|
||||
/// </summary>
|
||||
void On(string eventName, Action action);
|
||||
|
||||
/// <summary>
|
||||
/// Registers a persistent event handler with a typed parameter.
|
||||
/// </summary>
|
||||
void On<T>(string eventName, Action<T> action);
|
||||
|
||||
/// <summary>
|
||||
/// Registers a one-time event handler.
|
||||
/// </summary>
|
||||
void Once(string eventName, Action action);
|
||||
|
||||
/// <summary>
|
||||
/// Registers a one-time event handler with a typed parameter.
|
||||
/// </summary>
|
||||
void Once<T>(string eventName, Action<T> action);
|
||||
|
||||
/// <summary>
|
||||
/// Removes an event handler.
|
||||
/// </summary>
|
||||
void Off(string eventName);
|
||||
|
||||
/// <summary>
|
||||
/// Sends a message to Electron.
|
||||
/// </summary>
|
||||
Task Emit(string eventName, params object[] args);
|
||||
|
||||
/// <summary>
|
||||
/// Disposes the connection.
|
||||
/// </summary>
|
||||
void DisposeSocket();
|
||||
}
|
||||
}
|
||||
@@ -4,11 +4,12 @@ namespace ElectronNET.API;
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using ElectronNET.API.Bridge;
|
||||
using ElectronNET.API.Serialization;
|
||||
using SocketIO.Serializer.SystemTextJson;
|
||||
using SocketIO = SocketIOClient.SocketIO;
|
||||
|
||||
internal class SocketIoFacade
|
||||
internal class SocketIoFacade : IFacade
|
||||
{
|
||||
private readonly SocketIO _socket;
|
||||
private readonly object _lockObj = new object();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
namespace ElectronNET
|
||||
{
|
||||
using ElectronNET.API;
|
||||
using ElectronNET.API.Bridge;
|
||||
using ElectronNET.Runtime;
|
||||
using ElectronNET.Runtime.Controllers;
|
||||
using ElectronNET.Runtime.Data;
|
||||
@@ -49,7 +50,7 @@
|
||||
|
||||
internal static Func<Task> OnAppReadyCallback { get; set; }
|
||||
|
||||
internal static SocketIoFacade GetSocket()
|
||||
internal static IFacade GetSocket()
|
||||
{
|
||||
return RuntimeControllerCore?.Socket;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
namespace ElectronNET.Runtime.Controllers
|
||||
{
|
||||
using ElectronNET.API;
|
||||
using ElectronNET.API.Bridge;
|
||||
using ElectronNET.Runtime.Services;
|
||||
using ElectronNET.Runtime.Services.ElectronProcess;
|
||||
using ElectronNET.Runtime.Services.SocketBridge;
|
||||
@@ -12,7 +13,7 @@
|
||||
{
|
||||
}
|
||||
|
||||
internal abstract SocketIoFacade Socket { get; }
|
||||
internal abstract IFacade Socket { get; }
|
||||
|
||||
internal abstract ElectronProcessBase ElectronProcess { get; }
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
namespace ElectronNET.Runtime.Controllers
|
||||
{
|
||||
using ElectronNET.API;
|
||||
using ElectronNET.API.Bridge;
|
||||
using ElectronNET.Common;
|
||||
using ElectronNET.Runtime.Data;
|
||||
using ElectronNET.Runtime.Helpers;
|
||||
@@ -19,7 +20,7 @@
|
||||
{
|
||||
}
|
||||
|
||||
internal override SocketIoFacade Socket
|
||||
internal override IFacade Socket
|
||||
{
|
||||
get
|
||||
{
|
||||
|
||||
@@ -4,13 +4,14 @@ namespace ElectronNET.API
|
||||
using System.Collections.Concurrent;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using ElectronNET.API.Bridge;
|
||||
using ElectronNET.AspNet.Hubs;
|
||||
|
||||
/// <summary>
|
||||
/// SignalR-based facade that mimics the SocketIoFacade interface
|
||||
/// for compatibility with existing Electron API code.
|
||||
/// </summary>
|
||||
internal class SignalRFacade
|
||||
internal class SignalRFacade : IFacade
|
||||
{
|
||||
private readonly IHubContext<ElectronHub> _hubContext;
|
||||
private string _connectionId;
|
||||
@@ -26,6 +27,15 @@ namespace ElectronNET.API
|
||||
public event EventHandler BridgeDisconnected;
|
||||
public event EventHandler BridgeConnected;
|
||||
|
||||
/// <summary>
|
||||
/// SignalR connections are managed by ASP.NET Core, so this is a no-op.
|
||||
/// Connection establishment happens via the ElectronHub.
|
||||
/// </summary>
|
||||
public void Connect()
|
||||
{
|
||||
// No-op: SignalR connection is managed by ASP.NET Core
|
||||
}
|
||||
|
||||
public void SetConnectionId(string connectionId)
|
||||
{
|
||||
_connectionId = connectionId;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using ElectronNET.API;
|
||||
using ElectronNET.API.Bridge;
|
||||
using ElectronNET.Common;
|
||||
using ElectronNET.Runtime.Controllers;
|
||||
using ElectronNET.Runtime.Data;
|
||||
@@ -25,7 +26,7 @@
|
||||
|
||||
internal override SocketBridgeService SocketBridge => this.socketBridge;
|
||||
|
||||
internal override SocketIoFacade Socket
|
||||
internal override IFacade Socket
|
||||
{
|
||||
get
|
||||
{
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace ElectronNET.AspNet.Runtime
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using ElectronNET.API;
|
||||
using ElectronNET.API.Bridge;
|
||||
using ElectronNET.Common;
|
||||
using ElectronNET.Runtime.Data;
|
||||
using ElectronNET.Runtime.Services.ElectronProcess;
|
||||
@@ -44,11 +45,16 @@ namespace ElectronNET.AspNet.Runtime
|
||||
internal override ElectronProcessBase ElectronProcess => this.electronProcess;
|
||||
internal override SocketBridgeService SocketBridge => null;
|
||||
|
||||
internal override SocketIoFacade Socket
|
||||
internal override IFacade Socket
|
||||
{
|
||||
get
|
||||
{
|
||||
throw new NotImplementedException("SignalR mode uses SignalRFacade");
|
||||
if (this.State == LifetimeState.Ready)
|
||||
{
|
||||
return this.signalRFacade;
|
||||
}
|
||||
|
||||
throw new Exception("Cannot access SignalR facade. Runtime is not in 'Ready' state");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user