Files
Electron.NET/src/ElectronNET.API/Bridge/SocketIOConnection.cs

146 lines
3.6 KiB
C#
Raw Normal View History

2025-10-13 13:22:37 +02:00
#pragma warning disable IDE0130 // Namespace does not match folder structure
// ReSharper disable once CheckNamespace
namespace ElectronNET.API;
using System;
using System.Threading.Tasks;
using ElectronNET.API.Serialization;
using SocketIO.Serializer.SystemTextJson;
2025-10-13 13:22:37 +02:00
using SocketIO = SocketIOClient.SocketIO;
2026-03-05 11:25:22 +01:00
internal class SocketIOConnection : ISocketConnection
2025-10-13 13:22:37 +02:00
{
private readonly SocketIO _socket;
private readonly object _lockObj = new object();
private bool _isDisposed;
2025-10-13 13:22:37 +02:00
2026-03-05 11:25:22 +01:00
public SocketIOConnection(string uri)
2025-10-13 13:22:37 +02:00
{
_socket = new SocketIO(uri);
_socket.Serializer = new SystemTextJsonSerializer(ElectronJson.Options);
// Use default System.Text.Json serializer from SocketIOClient.
// Outgoing args are normalized to camelCase via SerializeArg in Emit.
2025-10-13 13:22:37 +02:00
}
public event EventHandler BridgeDisconnected;
public event EventHandler BridgeConnected;
public void Connect()
{
this.CheckDisposed();
2025-11-09 03:50:24 +01:00
_socket.OnError += (sender, e) => { Console.WriteLine($"BridgeConnector Error: {sender} {e}"); };
2025-10-13 13:22:37 +02:00
_socket.OnConnected += (_, _) =>
{
Console.WriteLine("BridgeConnector connected!");
this.BridgeConnected?.Invoke(this, EventArgs.Empty);
};
_socket.OnDisconnected += (_, _) =>
{
Console.WriteLine("BridgeConnector disconnected!");
this.BridgeDisconnected?.Invoke(this, EventArgs.Empty);
};
_socket.ConnectAsync().GetAwaiter().GetResult();
}
public void On(string eventName, Action action)
{
this.CheckDisposed();
lock (_lockObj)
2025-10-13 13:22:37 +02:00
{
2025-11-09 03:50:24 +01:00
_socket.On(eventName, _ => { Task.Run(action); });
}
2025-10-13 13:22:37 +02:00
}
public void On<T>(string eventName, Action<T> action)
{
this.CheckDisposed();
lock (_lockObj)
2025-10-13 13:22:37 +02:00
{
_socket.On(eventName, response =>
{
var value = response.GetValue<T>();
Task.Run(() => action(value));
});
}
2025-10-13 13:22:37 +02:00
}
public void Once(string eventName, Action action)
{
this.CheckDisposed();
lock (_lockObj)
2025-10-13 13:22:37 +02:00
{
_socket.On(eventName, _ =>
{
this.Off(eventName);
Task.Run(action);
});
}
2025-10-13 13:22:37 +02:00
}
public void Once<T>(string eventName, Action<T> action)
{
this.CheckDisposed();
lock (_lockObj)
2025-10-13 13:22:37 +02:00
{
_socket.On(eventName, (socketIoResponse) =>
2025-11-09 03:50:24 +01:00
{
this.Off(eventName);
2025-11-09 03:50:24 +01:00
Task.Run(() => action(socketIoResponse.GetValue<T>()));
});
}
2025-10-13 13:22:37 +02:00
}
public void Off(string eventName)
{
if (_isDisposed)
{
return;
}
lock (_lockObj)
{
_socket.Off(eventName);
}
2025-10-13 13:22:37 +02:00
}
public async Task Emit(string eventName, params object[] args)
{
if (!_isDisposed)
{
await _socket.EmitAsync(eventName, args).ConfigureAwait(false);
}
}
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_isDisposed = true;
_socket.Dispose();
}
2025-10-13 13:22:37 +02:00
}
private void CheckDisposed()
2025-10-13 13:22:37 +02:00
{
if (this._isDisposed)
{
2026-03-05 11:25:22 +01:00
throw new ObjectDisposedException(nameof(SocketIOConnection));
}
2025-10-13 13:22:37 +02:00
}
2025-11-15 08:05:31 +01:00
}