This commit is contained in:
rafael-aero
2021-07-12 19:50:39 +02:00
parent 8880e040f7
commit 126d39f4a5
37 changed files with 1045 additions and 1053 deletions

View File

@@ -1,39 +1,65 @@
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");
}
}
@@ -41,4 +67,55 @@ namespace ElectronNET.API
}
}
}
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;
}
}
}