diff --git a/src/ElectronNET.API/Bridge/BridgeConnector.cs b/src/ElectronNET.API/Bridge/BridgeConnector.cs index 3c06b430..d1eb4053 100644 --- a/src/ElectronNET.API/Bridge/BridgeConnector.cs +++ b/src/ElectronNET.API/Bridge/BridgeConnector.cs @@ -4,7 +4,7 @@ namespace ElectronNET.API { internal static class BridgeConnector { - public static SocketIoFacade Socket + public static ISocketConnection Socket { get { diff --git a/src/ElectronNET.API/Bridge/ISocketConnection.cs b/src/ElectronNET.API/Bridge/ISocketConnection.cs new file mode 100644 index 00000000..316576a1 --- /dev/null +++ b/src/ElectronNET.API/Bridge/ISocketConnection.cs @@ -0,0 +1,56 @@ +namespace ElectronNET.API; + +using System; +using System.Threading.Tasks; + +/// +/// Common interface for communication facades. +/// Provides methods for bidirectional communication between .NET and Electron. +/// +internal interface ISocketConnection : IDisposable +{ + /// + /// Raised when the bridge connection is established. + /// + event EventHandler BridgeConnected; + + /// + /// Raised when the bridge connection is lost. + /// + event EventHandler BridgeDisconnected; + + /// + /// Establishes the connection to Electron. + /// + void Connect(); + + /// + /// Registers a persistent event handler. + /// + void On(string eventName, Action action); + + /// + /// Registers a persistent event handler with a typed parameter. + /// + void On(string eventName, Action action); + + /// + /// Registers a one-time event handler. + /// + void Once(string eventName, Action action); + + /// + /// Registers a one-time event handler with a typed parameter. + /// + void Once(string eventName, Action action); + + /// + /// Removes an event handler. + /// + void Off(string eventName); + + /// + /// Sends a message to Electron. + /// + Task Emit(string eventName, params object[] args); +} \ No newline at end of file diff --git a/src/ElectronNET.API/Bridge/SocketIOFacade.cs b/src/ElectronNET.API/Bridge/SocketIOConnection.cs similarity index 95% rename from src/ElectronNET.API/Bridge/SocketIOFacade.cs rename to src/ElectronNET.API/Bridge/SocketIOConnection.cs index 86d870e2..7bf85675 100644 --- a/src/ElectronNET.API/Bridge/SocketIOFacade.cs +++ b/src/ElectronNET.API/Bridge/SocketIOConnection.cs @@ -8,13 +8,13 @@ namespace ElectronNET.API; using SocketIO.Serializer.SystemTextJson; using SocketIO = SocketIOClient.SocketIO; -internal class SocketIoFacade : IDisposable +internal class SocketIOConnection : ISocketConnection { private readonly SocketIO _socket; private readonly object _lockObj = new object(); private bool _isDisposed; - public SocketIoFacade(string uri) + public SocketIOConnection(string uri) { _socket = new SocketIO(uri); _socket.Serializer = new SystemTextJsonSerializer(ElectronJson.Options); @@ -140,7 +140,7 @@ private void CheckDisposed() { if (this._isDisposed) { - throw new ObjectDisposedException(nameof(SocketIoFacade)); + throw new ObjectDisposedException(nameof(SocketIOConnection)); } } } \ No newline at end of file diff --git a/src/ElectronNET.API/ElectronNetRuntime.cs b/src/ElectronNET.API/ElectronNetRuntime.cs index 78d976e8..e38fd935 100644 --- a/src/ElectronNET.API/ElectronNetRuntime.cs +++ b/src/ElectronNET.API/ElectronNetRuntime.cs @@ -49,7 +49,7 @@ static ElectronNetRuntime() internal static Func OnAppReadyCallback { get; set; } - internal static SocketIoFacade GetSocket() + internal static ISocketConnection GetSocket() { return RuntimeControllerCore?.Socket; } diff --git a/src/ElectronNET.API/Runtime/Controllers/RuntimeControllerBase.cs b/src/ElectronNET.API/Runtime/Controllers/RuntimeControllerBase.cs index fe527c7e..2b5229ea 100644 --- a/src/ElectronNET.API/Runtime/Controllers/RuntimeControllerBase.cs +++ b/src/ElectronNET.API/Runtime/Controllers/RuntimeControllerBase.cs @@ -12,7 +12,7 @@ protected RuntimeControllerBase() { } - internal abstract SocketIoFacade Socket { get; } + internal abstract ISocketConnection Socket { get; } internal abstract ElectronProcessBase ElectronProcess { get; } diff --git a/src/ElectronNET.API/Runtime/Controllers/RuntimeControllerDotNetFirst.cs b/src/ElectronNET.API/Runtime/Controllers/RuntimeControllerDotNetFirst.cs index 70591674..0ceaeb8a 100644 --- a/src/ElectronNET.API/Runtime/Controllers/RuntimeControllerDotNetFirst.cs +++ b/src/ElectronNET.API/Runtime/Controllers/RuntimeControllerDotNetFirst.cs @@ -19,7 +19,7 @@ public RuntimeControllerDotNetFirst() { } - internal override SocketIoFacade Socket + internal override ISocketConnection Socket { get { diff --git a/src/ElectronNET.API/Runtime/Controllers/RuntimeControllerElectronFirst.cs b/src/ElectronNET.API/Runtime/Controllers/RuntimeControllerElectronFirst.cs index fdb458f0..436ba921 100644 --- a/src/ElectronNET.API/Runtime/Controllers/RuntimeControllerElectronFirst.cs +++ b/src/ElectronNET.API/Runtime/Controllers/RuntimeControllerElectronFirst.cs @@ -17,7 +17,7 @@ public RuntimeControllerElectronFirst() { } - internal override SocketIoFacade Socket + internal override ISocketConnection Socket { get { diff --git a/src/ElectronNET.API/Runtime/Services/SocketBridge/SocketBridgeService.cs b/src/ElectronNET.API/Runtime/Services/SocketBridge/SocketBridgeService.cs index 7e8be64b..7200f496 100644 --- a/src/ElectronNET.API/Runtime/Services/SocketBridge/SocketBridgeService.cs +++ b/src/ElectronNET.API/Runtime/Services/SocketBridge/SocketBridgeService.cs @@ -9,7 +9,7 @@ internal class SocketBridgeService : LifetimeServiceBase { private readonly int socketPort; private readonly string socketUrl; - private SocketIoFacade socket; + private SocketIOConnection socket; public SocketBridgeService(int socketPort) { @@ -19,11 +19,11 @@ public SocketBridgeService(int socketPort) public int SocketPort => this.socketPort; - internal SocketIoFacade Socket => this.socket; + internal SocketIOConnection Socket => this.socket; protected override Task StartCore() { - this.socket = new SocketIoFacade(this.socketUrl); + this.socket = new SocketIOConnection(this.socketUrl); this.socket.BridgeConnected += this.Socket_BridgeConnected; this.socket.BridgeDisconnected += this.Socket_BridgeDisconnected; Task.Run(this.Connect); diff --git a/src/ElectronNET.AspNet/Runtime/Controllers/RuntimeControllerAspNetBase.cs b/src/ElectronNET.AspNet/Runtime/Controllers/RuntimeControllerAspNetBase.cs index 65487df8..ab807f3a 100644 --- a/src/ElectronNET.AspNet/Runtime/Controllers/RuntimeControllerAspNetBase.cs +++ b/src/ElectronNET.AspNet/Runtime/Controllers/RuntimeControllerAspNetBase.cs @@ -25,7 +25,7 @@ protected RuntimeControllerAspNetBase(AspNetLifetimeAdapter aspNetLifetimeAdapte internal override SocketBridgeService SocketBridge => this.socketBridge; - internal override SocketIoFacade Socket + internal override ISocketConnection Socket { get {