diff --git a/src/ElectronNET.API/Bridge/BridgeConnector.cs b/src/ElectronNET.API/Bridge/BridgeConnector.cs index 3c06b43..d1eb405 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 0000000..316576a --- /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 86d870e..7bf8567 100644 --- a/src/ElectronNET.API/Bridge/SocketIOFacade.cs +++ b/src/ElectronNET.API/Bridge/SocketIOConnection.cs @@ -8,13 +8,13 @@ using ElectronNET.API.Serialization; 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 @@ internal class SocketIoFacade : IDisposable { 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 78d976e..e38fd93 100644 --- a/src/ElectronNET.API/ElectronNetRuntime.cs +++ b/src/ElectronNET.API/ElectronNetRuntime.cs @@ -49,7 +49,7 @@ 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 fe527c7..2b5229e 100644 --- a/src/ElectronNET.API/Runtime/Controllers/RuntimeControllerBase.cs +++ b/src/ElectronNET.API/Runtime/Controllers/RuntimeControllerBase.cs @@ -12,7 +12,7 @@ { } - 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 7059167..0ceaeb8 100644 --- a/src/ElectronNET.API/Runtime/Controllers/RuntimeControllerDotNetFirst.cs +++ b/src/ElectronNET.API/Runtime/Controllers/RuntimeControllerDotNetFirst.cs @@ -19,7 +19,7 @@ { } - 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 fdb458f..436ba92 100644 --- a/src/ElectronNET.API/Runtime/Controllers/RuntimeControllerElectronFirst.cs +++ b/src/ElectronNET.API/Runtime/Controllers/RuntimeControllerElectronFirst.cs @@ -17,7 +17,7 @@ { } - 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 7e8be64..7200f49 100644 --- a/src/ElectronNET.API/Runtime/Services/SocketBridge/SocketBridgeService.cs +++ b/src/ElectronNET.API/Runtime/Services/SocketBridge/SocketBridgeService.cs @@ -9,7 +9,7 @@ { private readonly int socketPort; private readonly string socketUrl; - private SocketIoFacade socket; + private SocketIOConnection socket; public SocketBridgeService(int socketPort) { @@ -19,11 +19,11 @@ 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 65487df..ab807f3 100644 --- a/src/ElectronNET.AspNet/Runtime/Controllers/RuntimeControllerAspNetBase.cs +++ b/src/ElectronNET.AspNet/Runtime/Controllers/RuntimeControllerAspNetBase.cs @@ -25,7 +25,7 @@ internal override SocketBridgeService SocketBridge => this.socketBridge; - internal override SocketIoFacade Socket + internal override ISocketConnection Socket { get {