async Task <WebSocket> ConnectAsync(CancellationToken cancellationToken) { Fx.Assert(!this.closeCalled, "Shouldn't call ConnectWebSocketAsync if CloseAsync was called."); try { var connectDelay = ConnectDelayIntervals[this.connectDelayIndex]; if (connectDelay != TimeSpan.Zero) { await Task.Delay(connectDelay, cancellationToken).ConfigureAwait(false); } RelayEventSource.Log.RelayClientConnectStart(this.listener); var webSocket = new ClientWebSocket(); webSocket.Options.SetBuffer(this.bufferSize, this.bufferSize); webSocket.Options.Proxy = this.listener.Proxy; webSocket.Options.KeepAliveInterval = HybridConnectionConstants.KeepAliveInterval; webSocket.Options.SetRequestHeader("User-Agent", "ServiceBus/" + ClientAgentFileVersion); var token = await this.tokenRenewer.GetTokenAsync().ConfigureAwait(false); webSocket.Options.SetRequestHeader(RelayConstants.ServiceBusAuthorizationHeaderName, token.TokenString); // When we reconnect we need to remove the "_GXX" suffix otherwise trackingId gets longer after each reconnect string trackingId = TrackingContext.RemoveSuffix(this.listener.TrackingContext.TrackingId); // Build the websocket uri, e.g. "wss://contoso.servicebus.windows.net:443/$hc/endpoint1?sb-hc-action=listen&sb-hc-id=E2E_TRACKING_ID" var webSocketUri = HybridConnectionUtility.BuildUri( this.address.Host, this.address.Port, this.address.AbsolutePath, this.address.Query, HybridConnectionConstants.Actions.Listen, trackingId); await webSocket.ConnectAsync(webSocketUri, cancellationToken).ConfigureAwait(false); #if NET45 // TODO: Flow Response Headers in NETSTANDARD ClientWebSocket trackingId = webSocket.ResponseHeaders[TrackingContext.TrackingIdName]; if (!string.IsNullOrEmpty(trackingId)) { // Update to the flown trackingId (which has _GX suffix) this.listener.TrackingContext = TrackingContext.Create(trackingId, this.listener.TrackingContext.SubsystemId); } #endif this.OnOnline(); return(webSocket); } catch (WebSocketException wsException) { throw RelayEventSource.Log.ThrowingException(WebSocketExceptionHelper.ConvertToRelayContract(wsException), this.listener); } finally { RelayEventSource.Log.RelayClientConnectStop(); } }
async Task <WebSocket> ConnectAsync(CancellationToken cancellationToken) { this.listener.ThrowIfDisposed(); var webSocket = ClientWebSocketFactory.Create(this.listener.UseBuiltInClientWebSocket); try { var connectDelay = ConnectDelayIntervals[this.connectDelayIndex]; if (connectDelay != TimeSpan.Zero) { await Task.Delay(connectDelay, cancellationToken).ConfigureAwait(false); } RelayEventSource.Log.ObjectConnecting(this.listener); webSocket.Options.SetBuffer(this.bufferSize, this.bufferSize); DefaultWebProxy.ConfigureProxy(webSocket.Options, this.listener.Proxy); webSocket.Options.KeepAliveInterval = HybridConnectionConstants.KeepAliveInterval; webSocket.Options.SetRequestHeader(HybridConnectionConstants.Headers.RelayUserAgent, HybridConnectionConstants.ClientAgent); var token = await this.tokenRenewer.GetTokenAsync().ConfigureAwait(false); webSocket.Options.SetRequestHeader(RelayConstants.ServiceBusAuthorizationHeaderName, token.TokenString); // When we reconnect we need to remove the "_GXX" suffix otherwise trackingId gets longer after each reconnect string trackingId = TrackingContext.RemoveSuffix(this.listener.TrackingContext.TrackingId); // Build the websocket uri, e.g. "wss://contoso.servicebus.windows.net:443/$hc/endpoint1?sb-hc-action=listen&sb-hc-id=E2E_TRACKING_ID" var webSocketUri = HybridConnectionUtility.BuildUri( this.address.Host, this.address.Port, this.address.AbsolutePath, this.address.Query, HybridConnectionConstants.Actions.Listen, trackingId); await webSocket.ConnectAsync(webSocketUri, cancellationToken).ConfigureAwait(false); this.OnOnline(); RelayEventSource.Log.ObjectConnected(this.listener); return(webSocket.WebSocket); } catch (Exception exception) when(!WebSocketExceptionHelper.IsRelayContract(exception)) { throw RelayEventSource.Log.ThrowingException( WebSocketExceptionHelper.ConvertToRelayContract(exception, this.listener.TrackingContext, webSocket.Response), this.listener); } }