public async Task <ConnectionContext> ConnectAsync(HubServiceEndpoint hubServiceEndpoint,
                                                           TransferFormat transferFormat,
                                                           string connectionId,
                                                           string target,
                                                           CancellationToken cancellationToken  = default,
                                                           IDictionary <string, string> headers = null)
        {
            var provider = hubServiceEndpoint.Provider;
            var hubName  = hubServiceEndpoint.Hub;

            Task <string> accessTokenGenerater() => provider.GenerateServerAccessTokenAsync(hubName, _userId);

            var url = GetServiceUrl(provider, hubName, connectionId, target);
            var connectionOptions = new WebSocketConnectionOptions
            {
                Headers = headers,
                Proxy   = provider.Proxy,
            };
            var connection = new WebSocketConnectionContext(connectionOptions, _loggerFactory, accessTokenGenerater);

            try
            {
                await connection.StartAsync(url, cancellationToken);

                return(connection);
            }
            catch
            {
                await connection.StopAsync();

                throw;
            }
        }
 public WebSocketConnectionContext(WebSocketConnectionOptions httpConnectionOptions, ILoggerFactory loggerFactory, Func <Task <string> > accessTokenProvider)
 {
     Transport    = _websocketTransport = new WebSocketsTransport(httpConnectionOptions, loggerFactory, accessTokenProvider);
     ConnectionId = "sc_" + Guid.NewGuid();
 }