Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
320 changes: 153 additions & 167 deletions ElectronNET.API/App.cs

Large diffs are not rendered by default.

160 changes: 79 additions & 81 deletions ElectronNET.API/AutoUpdater.cs

Large diffs are not rendered by default.

115 changes: 96 additions & 19 deletions ElectronNET.API/BridgeConnector.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,121 @@
using Quobject.SocketIoClientDotNet.Client;
using System;
using System;
using SocketIOClient;
using SocketIOClient.Newtonsoft.Json;

namespace ElectronNET.API
{
internal static class BridgeConnector
{
private static Socket _socket;
private static SocketIO _socket;
private static object _syncRoot = new object();

public static Socket Socket

public static void Emit(string eventString, params object[] args) => Socket.EmitAsync(eventString, args);
public static void Off(string eventString) => Socket.Off(eventString);
public static void On(string eventString, Action fn) => Socket.On(eventString, _ => fn());
public static void On<T>(string eventString, Action<T> fn) => Socket.On(eventString, (o) =>
{
fn(o.GetValue<T>(0));
});

public static void Once<T>(string eventString, Action<T> fn) => Socket.On(eventString, (o) =>
{
Socket.Off(eventString);
fn(o.GetValue<T>(0));
});


private static SocketIO Socket
{
get
{
if(_socket == null && HybridSupport.IsElectronActive)
if (_socket is null)
{
lock (_syncRoot)
if (HybridSupport.IsElectronActive)
{
if (_socket == null && HybridSupport.IsElectronActive)

lock (_syncRoot)
{
_socket = IO.Socket("http://localhost:" + BridgeSettings.SocketPort);
_socket.On(Socket.EVENT_CONNECT, () =>
if (_socket is null && HybridSupport.IsElectronActive)
{
Console.WriteLine("BridgeConnector connected!");
});
var socket = new SocketIO($"http://localhost:{BridgeSettings.SocketPort}", new SocketIOOptions()
{
EIO = 3
});

socket.JsonSerializer = new NewtonsoftJsonSerializer(socket.Options.EIO);


socket.OnConnected += (_, __) =>
{
Console.WriteLine("BridgeConnector connected!");
};

socket.ConnectAsync().Wait();

_socket = socket;
}
}
}
}
else if(_socket == null && !HybridSupport.IsElectronActive)
{
lock (_syncRoot)
else
{
if (_socket == null && !HybridSupport.IsElectronActive)
{
_socket = IO.Socket(new Uri("http://localhost"), new IO.Options { AutoConnect = false });
}
throw new Exception("Missing Socket Port");
}
}

return _socket;
}
}
}

public interface IListener : System.IComparable<IListener>
{
int GetId();
void Call(params object[] args);
}

public class ListenerImpl : IListener
{
private static int id_counter = 0;
private int Id;
private readonly Action fn1;
private readonly Action<object> fn;

public ListenerImpl(Action<object> fn)
{

this.fn = fn;
this.Id = id_counter++;
}

public ListenerImpl(Action fn)
{

this.fn1 = fn;
this.Id = id_counter++;
}

public void Call(params object[] args)
{
if (fn != null)
{
var arg = args.Length > 0 ? args[0] : null;
fn(arg);
}
else
{
fn1();
}
}

public int CompareTo(IListener other)
{
return this.GetId().CompareTo(other.GetId());
}

public int GetId()
{
return Id;
}
}
}
14 changes: 7 additions & 7 deletions ElectronNET.API/BrowserView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@ public Task<Rectangle> GetBoundsAsync()
{
var taskCompletionSource = new TaskCompletionSource<Rectangle>();

BridgeConnector.Socket.On("browserView-getBounds-reply", (result) =>
BridgeConnector.On<Rectangle>("browserView-getBounds-reply", (result) =>
{
BridgeConnector.Socket.Off("browserView-getBounds-reply");
taskCompletionSource.SetResult((Rectangle)result);
BridgeConnector.Off("browserView-getBounds-reply");
taskCompletionSource.SetResult(result);
});

BridgeConnector.Socket.Emit("browserView-getBounds", Id);
BridgeConnector.Emit("browserView-getBounds", Id);

return taskCompletionSource.Task;
}

public void SetBounds(Rectangle value)
{
BridgeConnector.Socket.Emit("browserView-setBounds", Id, JObject.FromObject(value, _jsonSerializer));
BridgeConnector.Emit("browserView-setBounds", Id, JObject.FromObject(value, _jsonSerializer));
}

/// <summary>
Expand All @@ -69,7 +69,7 @@ internal BrowserView(int id)
/// <param name="options"></param>
public void SetAutoResize(AutoResizeOptions options)
{
BridgeConnector.Socket.Emit("browserView-setAutoResize", Id, JObject.FromObject(options, _jsonSerializer));
BridgeConnector.Emit("browserView-setAutoResize", Id, JObject.FromObject(options, _jsonSerializer));
}

/// <summary>
Expand All @@ -80,7 +80,7 @@ public void SetAutoResize(AutoResizeOptions options)
/// <param name="color">Color in #aarrggbb or #argb form. The alpha channel is optional.</param>
public void SetBackgroundColor(string color)
{
BridgeConnector.Socket.Emit("browserView-setBackgroundColor", Id, color);
BridgeConnector.Emit("browserView-setBackgroundColor", Id, color);
}

private JsonSerializer _jsonSerializer = new JsonSerializer()
Expand Down
Loading