From 47f4516ae25e0dd772990a688505a16c0edf6ee3 Mon Sep 17 00:00:00 2001 From: theolivenbaum Date: Thu, 2 Sep 2021 18:20:11 +0200 Subject: [PATCH] Add flag to check if the socket is connected --- ElectronNET.API/BridgeConnector.cs | 8 +++++--- ElectronNET.API/IpcMain.cs | 3 ++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/ElectronNET.API/BridgeConnector.cs b/ElectronNET.API/BridgeConnector.cs index 14a9def..1a36934 100644 --- a/ElectronNET.API/BridgeConnector.cs +++ b/ElectronNET.API/BridgeConnector.cs @@ -95,6 +95,8 @@ namespace ElectronNET.API private static Task _waitForConnection => _waitForBeingConnected.Task; + public static bool IsConnected => _waitForConnection is Task task && task.IsCompletedSuccessfully; + public static void Emit(string eventString, params object[] args) { //We don't care about waiting for the event to be emitted, so this doesn't need to be async @@ -387,14 +389,14 @@ namespace ElectronNET.API socket.OnConnected += (_, __) => { - Log("ElectronNET socket connected on port {0}!", BridgeSettings.SocketPort); _waitForBeingConnected.TrySetResult(); + Log("ElectronNET socket connected on port {0}!", BridgeSettings.SocketPort); }; socket.OnReconnectAttempt += (_, __) => { - Log("ElectronNET socket is trying to reconnect on port {0}...", BridgeSettings.SocketPort); _waitForBeingConnected = new(); + Log("ElectronNET socket is trying to reconnect on port {0}...", BridgeSettings.SocketPort); }; socket.OnReconnectError += (_, ex) => @@ -404,8 +406,8 @@ namespace ElectronNET.API socket.OnReconnected += (_, __) => { - Log("ElectronNET socket reconnected on port {0}...", BridgeSettings.SocketPort); _waitForBeingConnected.TrySetResult(); + Log("ElectronNET socket reconnected on port {0}...", BridgeSettings.SocketPort); }; diff --git a/ElectronNET.API/IpcMain.cs b/ElectronNET.API/IpcMain.cs index 0beedfd..537f1fb 100644 --- a/ElectronNET.API/IpcMain.cs +++ b/ElectronNET.API/IpcMain.cs @@ -37,6 +37,8 @@ namespace ElectronNET.API } } + public bool IsConnected => BridgeConnector.IsConnected; + /// /// Listens to channel, when a new message arrives listener would be called with /// listener(event, args...). @@ -62,7 +64,6 @@ namespace ElectronNET.API }); } - /// /// Listens to channel, when a new message arrives listener would be called with /// listener(event, args...). This listner will keep the window event sender id