Files
laatzen/Common/OmniPlus/Exchange/SignalR/DataHub.cs
T

168 lines
4.6 KiB
C#

namespace SignalR
{
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR.Client;
public delegate void DataReceivedHandler(DataDictionary data);
public delegate void StateHandler(string state);
[Guid("26CA0187-06E4-4A6E-B7EB-A57996365FEF")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IDataHub
{
void DataReceived(DataDictionary data);
void StateChanged(string state);
}
[Guid("B126CD14-DF35-4D56-B3B3-97D9FE606D61")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IDataHubClient
{
bool IsConnected { get; }
void Connect(string url);
void Disconnect();
void Send(DataDictionary dictionary);
}
[Guid("BBF1EDDB-84BC-4FA0-85E8-1B16BD331C42")]
[ComSourceInterfaces(typeof(IDataHub))]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("SignalR.DataHub")]
public class DataHub : IDataHubClient
{
private HubConnection connection;
private IDisposable disposable;
private IHubProxy proxy;
public DataHub()
{
}
public event DataReceivedHandler DataReceived;
public event StateHandler StateChanged;
public bool IsConnected
{
get
{
try
{
return this.connection?.State == ConnectionState.Connected;
}
catch (Exception e)
{
this.NotifyStateChanged(e);
return false;
}
}
}
public void Connect(string url)
{
this.connection = new HubConnection(url, queryString: $"key={Environment.MachineName}");
this.connection.Closed += this.Connection_Closed;
this.connection.ConnectionSlow += this.Connection_ConnectionSlow;
this.connection.Error += this.Connection_Error;
this.connection.Received += this.Connection_Received;
this.connection.Reconnected += this.Connection_Reconnected;
this.connection.Reconnecting += this.Connection_Reconnecting;
this.connection.StateChanged += this.Connection_StateChanged;
this.proxy = this.connection.CreateHubProxy("DataHub");
this.disposable = this.proxy.On<Dictionary<string, object>>(nameof(this.Send), this.Receive);
try
{
this.connection
.Start()
.Wait();
}
catch (Exception e)
{
this.NotifyStateChanged(e.ToString());
}
}
public void Disconnect()
{
Task.Factory.StartNew(() =>
{
try
{
this.disposable.Dispose();
}
catch (Exception e)
{
this.NotifyStateChanged(e);
}
try
{
this.connection.Stop();
}
catch (Exception e)
{
this.NotifyStateChanged(e);
}
});
}
public void Send(DataDictionary dictionary)
=> this.proxy.Invoke(nameof(this.Send), dictionary.Generic());
private void Connection_Closed()
{
this.NotifyStateChanged("Connection closed.");
}
private void Connection_ConnectionSlow()
{
this.NotifyStateChanged("Connection slow.");
}
private void Connection_Error(Exception exception)
{
this.NotifyStateChanged(exception);
}
private void Connection_Received(string data)
{
this.NotifyStateChanged(data);
}
private void Connection_Reconnected()
{
this.NotifyStateChanged("Connection reconnected.");
}
private void Connection_Reconnecting()
{
this.NotifyStateChanged("Connection reconnecting.");
}
private void Connection_StateChanged(StateChange state)
{
this.NotifyStateChanged(state);
}
private void NotifyStateChanged(object state)
{
this.StateChanged?.Invoke($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {state}");
}
private void Receive(Dictionary<string, object> data)
{
this.DataReceived?.Invoke(new DataDictionary(data));
}
}
}