private void InitBayeuxClient() { if (_isDisposed) { throw new ObjectDisposedException("Cannot create connection when disposed"); } _logger.LogDebug("Initializing {name} ...", nameof(BayeuxClient)); if (!_authenticationClient.IsAuthenticated) { Reauthenticate(); } _tokenInfo = _authenticationClient.AuthenticationClient.AccessInfo; // Salesforce socket timeout during connection(CometD session) = 110 seconds var options = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase) { { ClientTransport.TIMEOUT_OPTION, _options.ReadTimeOut ?? ReadTimeOut }, { ClientTransport.MAX_NETWORK_DELAY_OPTION, _options.ReadTimeOut ?? ReadTimeOut } }; var headers = new NameValueCollection { { HttpRequestHeader.Authorization.ToString(), $"OAuth {_tokenInfo.AccessToken}" } }; _clientTransport = new LongPollingTransport(options, headers); // only need the scheme and host, strip out the rest var serverUri = new Uri(_tokenInfo.InstanceUrl); var endpoint = $"{serverUri.Scheme}://{serverUri.Host}{_options.CometDUri}"; _bayeuxClient = new BayeuxClient(endpoint, _clientTransport); // adds logging and also raises an event to process reconnection to the server. _errorExtension = new ErrorExtension(); _errorExtension.ConnectionError += ErrorExtension_ConnectionError; _errorExtension.ConnectionException += ErrorExtension_ConnectionException; _errorExtension.ConnectionMessage += ErrorExtension_ConnectionMessage; _bayeuxClient.AddExtension(_errorExtension); _replayIdExtension = new ReplayExtension(); _bayeuxClient.AddExtension(_replayIdExtension); _logger.LogDebug("{name} initializing completed...", nameof(BayeuxClient)); }
static void Main(string[] args) { var longPollingTransport = new LongPollingTransport(null) { HeaderCollection = new WebHeaderCollection { new NameValueCollection { { "Content-Type", "application/json" }, { "Authorization", $"Bearer {accessToken}" } }, }, CookieCollection = new CookieCollection(), }; var client = new BayeuxClient(url, new List <ClientTransport> { longPollingTransport }); // Save the newReplayId in a reliable way. This is basically the last message processed // So when your application recovers a crash the next subscription should start from the last replay id processed var replayExtension = new ReplayExtension((changedChannel, newReplayId) => Console.WriteLine($"{changedChannel}: {newReplayId}")); replayExtension.SetReplayId(channelName, replayId); client.AddExtension(replayExtension); client.Handshake(new Dictionary <string, object> { { MessageFields.ReplayField, true } }); var result = client.WaitFor(6000, new List <BayeuxClient.State> { BayeuxClient.State.Connected }); // Subscription to channels IClientSessionChannel channel = client.GetChannel(channelName); var listener = new SimpleListener(); channel.Subscribe(listener); //channel.Unsubscribe(listener); //replayExtension.SetReplayId(channelName, 100); //channel.Subscribe(listener); Thread.Sleep(Timeout.Infinite); }