/// <summary> /// Start discovering. /// </summary> public async Task StartAsync() { if (!IsStarted) { _discoveredServices = new List <ResonanceSignalRDiscoveredService <TReportedServiceInformation> >(); _client = SignalRClientFactory.Default.Create(Mode, HubUrl); _client.EnableAutoReconnection = EnableAutoReconnection; _client.Error += OnDisconnected; await _client.StartAsync(); await _client.InvokeAsync(ResonanceHubMethods.RegisterDiscoveryClient, Credentials); var services = await _client.InvokeAsync <List <TReportedServiceInformation> >(ResonanceHubMethods.GetAvailableServices, Credentials); foreach (var serviceInfo in services) { OnServiceRegistered(serviceInfo); } _client.On <TReportedServiceInformation>(ResonanceHubMethods.ServiceRegistered, OnServiceRegistered); _client.On <TReportedServiceInformation>(ResonanceHubMethods.ServiceUnRegistered, OnServiceUnregistered); IsStarted = true; } }
/// <summary> /// Initializes a new instance of the <see cref="ResonanceRegisteredService{TCredentials, TResonanceServiceInformation, TAdapterInformation}"/> class. /// </summary> /// <param name="credentials">The credentials.</param> /// <param name="serviceInformation">The service information.</param> /// <param name="mode">The mode.</param> /// <param name="signalRClient">The signal r client.</param> internal ResonanceRegisteredService(TCredentials credentials, TResonanceServiceInformation serviceInformation, SignalRMode mode, ISignalRClient signalRClient) { IsRegistered = true; Mode = mode; Credentials = credentials; ServiceInformation = serviceInformation; _client = signalRClient; _client.Reconnecting -= OnReconnecting; _client.Reconnecting += OnReconnecting; _client.Reconnected -= OnReconnected; _client.Reconnected += OnReconnected; _client.Error -= OnError; _client.Error += OnError; _client.On <String, TAdapterInformation>(ResonanceHubMethods.ConnectionRequest, OnConnectionRequest); }
/// <summary> /// Called when the adapter is connecting. /// </summary> /// <returns></returns> protected override Task OnConnect() { bool completed = false; TaskCompletionSource <object> completionSource = new TaskCompletionSource <object>(); Task.Factory.StartNew(() => { try { _client = SignalRClientFactory.Default.Create(Mode, Url); _client.StartAsync().GetAwaiter().GetResult(); if (Role == SignalRAdapterRole.Connect) { _client.On(ResonanceHubMethods.Connected, () => { try { if (!completed) { completed = true; completionSource.SetResult(true); } } catch (Exception ex) { if (!completed) { Logger.LogError(ex, "Error occurred after successful connection."); completed = true; completionSource.SetException(ex); } } }); _client.On(ResonanceHubMethods.Declined, () => { try { if (!completed) { completed = true; var ex = new ConnectionDeclinedException(); Logger.LogError(ex, "Error occurred after session created."); completionSource.SetException(ex); } } catch (Exception ex) { if (!completed) { Logger.LogError(ex, "Error occurred after session created."); completed = true; completionSource.SetException(ex); } } }); } _client.On(ResonanceHubMethods.Disconnected, () => { if (State == ResonanceComponentState.Connected) { //OnDisconnect(false); //Don't know what to do here.. We already have the resonance disconnection message. //Maybe just raise an event.. } }); _client.On(ResonanceHubMethods.ServiceDown, () => { OnFailed(new ServiceDownException()); }); Logger.LogInformation("Authenticating with the remote hub {HubUrl}...", _client.Url); _client.InvokeAsync(ResonanceHubMethods.Login, Credentials).GetAwaiter().GetResult(); if (Role == SignalRAdapterRole.Connect) { Logger.LogInformation("Connecting to service {ServiceId}...", ServiceId); SessionId = _client.InvokeAsync <String>(ResonanceHubMethods.Connect, ServiceId).GetAwaiter().GetResult(); } else { Logger.LogInformation("Accepting connection {SessionId}...", SessionId); _client.InvokeAsync(ResonanceHubMethods.AcceptConnection, SessionId).GetAwaiter().GetResult(); if (!completed) { completed = true; completionSource.SetResult(true); } } _client.On <byte[]>(ResonanceHubMethods.DataAvailable, (data) => { OnDataAvailable(data); }); _client.Error += OnError; _client.Reconnecting += OnReconnecting; _client.Reconnected += OnReconnected; } catch (Exception ex) { completed = true; Logger.LogError(ex, "Error occurred while trying to connect."); completionSource.SetException(ex); } }); TimeoutTask.StartNew(() => { if (!completed) { completed = true; completionSource.SetException(new TimeoutException("Could not connect after the given timeout.")); } }, ConnectionTimeout); return(completionSource.Task); }