Skip to content

Instantly share code, notes, and snippets.

@frankbryce
Last active August 25, 2016 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frankbryce/ce04b9deb477bdd443799103c9f4a6a2 to your computer and use it in GitHub Desktop.
Save frankbryce/ce04b9deb477bdd443799103c9f4a6a2 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WampSharp.V2;
using WampSharp.V2.Client;
using WampSharp.V2.Core.Contracts;
namespace HubUSA.LPR.CameraService
{
/// <summary>
/// Our wrapper around reconnection logic for proxies, in order for subscribers
/// to stay perpetually subscribed
/// </summary>
public class WampReconnector : IDisposable
{
private readonly IWampChannel _channel;
private readonly IList<object> _subscribers;
private readonly WampChannelReconnector _reconnector;
/// <summary>
/// Our wrapper around reconnection logic for proxies, in order for subscribers
/// to stay perpetually subscribed
/// </summary>
public WampReconnector(
IWampChannel channel,
IEnumerable<object> subscribers)
{
_channel = channel;
_subscribers = subscribers.ToList();
_reconnector = new WampChannelReconnector(_channel, Reconnect);
}
/// <summary>
/// Starts the reconnection implemented in wampsharp, and
/// registers all of the subscribers within the reconnection logic
/// </summary>
public void Start()
{
_reconnector.Start();
}
private Task Reconnect()
{
_channel.Open().Wait();
var tasks = new List<Task>();
foreach (var subscriber in _subscribers)
{
tasks.Add(_channel.RealmProxy.Services.RegisterSubscriber(subscriber));
}
foreach (var task in tasks)
{
task.Wait();
}
return Task.FromResult(0);
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
/// <filterpriority>2</filterpriority>
public void Dispose()
{
_reconnector?.Dispose();
_channel?.Close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment