// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; using System.ComponentModel; using Microsoft.AspNetCore.SignalR.Internal.Protocol; using Microsoft.AspNetCore.Sockets.Client; using Newtonsoft.Json; namespace Microsoft.AspNetCore.SignalR.Client { public class HubConnectionBuilder : IHubConnectionBuilder { private readonly Dictionary, object> _settings = new Dictionary, object>(); private Func _connectionFactoryDelegate; public void ConfigureConnectionFactory(Func connectionFactoryDelegate) => _connectionFactoryDelegate = connectionFactoryDelegate; public void AddSetting(string name, T value) { _settings[new KeyValuePair(name, typeof(T))] = value; } public bool TryGetSetting(string name, out T value) { value = default(T); if (!_settings.TryGetValue(new KeyValuePair(name, typeof(T)), out var setting)) { return false; } value = (T)setting; return true; } public HubConnection Build() { if (_connectionFactoryDelegate == null) { throw new InvalidOperationException("Cannot create IConnection instance. The connection factory was not configured."); } var connection = _connectionFactoryDelegate(); var loggerFactory = ((IHubConnectionBuilder)this).GetLoggerFactory(); var hubProtocol = ((IHubConnectionBuilder)this).GetHubProtocol(); return new HubConnection(connection, hubProtocol ?? new JsonHubProtocol(), loggerFactory); } [EditorBrowsable(EditorBrowsableState.Never)] public override int GetHashCode() { return base.GetHashCode(); } [EditorBrowsable(EditorBrowsableState.Never)] public override bool Equals(object obj) { return base.Equals(obj); } [EditorBrowsable(EditorBrowsableState.Never)] public override string ToString() { return base.ToString(); } [EditorBrowsable(EditorBrowsableState.Never)] public new Type GetType() { return base.GetType(); } } }