首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cometd/bayeux客户端问题

Cometd/bayeux客户端问题
EN

Stack Overflow用户
提问于 2019-11-24 13:41:59
回答 1查看 458关注 0票数 1

我们已经创建了用于从Salesforce消费平台事件的.Net核心windows服务(每当在Salesforce中创建/更新的特定对象想要获取信息时).We正在使用Cometd/bayeux客户端来订阅Salesforce平台事件。

最初一切工作正常,每当Salesforce对象发生变化时,我们都会获得数据,但在几个空闲小时(大约1-2小时)后,没有数据是getting.Check的bayeux客户端状态,它显示为已连接,但订阅不是happening.When我们重新启动服务,它已经开始工作。使用下面的代码进行连接和订阅。有没有人能帮个忙。

代码语言:javascript
复制
 public void CheckAndSubscribe()
    {
        if (!_bayeuxClient.IsConnected())
        {
            _logger.LogInformation("Bayeux client not connected. trying to connect...");
            try
            {
                SalesforceSession salesforceSessionData = _sfSessionAdapter.GetSalesforceSession();
                _bayeuxClient.Connect(salesforceSessionData.Url, salesforceSessionData.SessionId);

                List<string> sfChannels = _syncSalesforceConfiguration.BayeuxClientConfiguration.ExternalChannels;

                foreach (string channel in sfChannels)
                {
                    _bayeuxClient.Subscribe(channel, _messageListener);
                }
                _logger.LogInformation("Bayeux client connected and channels subscribed...");
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
            }

        }
    }
 public class BayeuxClientAdapter : IBayeuxClientAdapter
{
    BayeuxClient _bayeuxClient = null;
    private readonly SyncSalesforceConfiguration _syncSalesforceConfiguration;
    public BayeuxClientAdapter(IOptions<SyncSalesforceConfiguration> syncSalesforceConfiguration)
    {
        _syncSalesforceConfiguration = syncSalesforceConfiguration.Value;
    }
    public bool IsConnected()
    {
        return _bayeuxClient?.Connected ?? false;
    }

    public void Connect(string instanceUrl, string authToken)
    {
        int readTimeOut = 120 * 1000;
        string streamingEndpointURI = _syncSalesforceConfiguration.BayeuxClientConfiguration.StreamingEndpointUri;

        IDictionary<string, object> options = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase)
        {
            { ClientTransport.TIMEOUT_OPTION, readTimeOut },
            { ClientTransport.MAX_NETWORK_DELAY_OPTION, 120000 }
        };

        var headers = new NameValueCollection { { HttpRequestHeader.Authorization.ToString(), $"OAuth {authToken}" } };

        var clientTransport = new LongPollingTransport(options, headers);

        var serverUri = new Uri(instanceUrl);
        String endpoint = String.Format("{0}://{1}{2}", serverUri.Scheme, serverUri.Host, streamingEndpointURI);

        _bayeuxClient = new BayeuxClient(endpoint, new[] { clientTransport });
    }

    public void DisConnect()
    {
        if (IsConnected())
        {
            _bayeuxClient?.ResetSubscriptions();
            _bayeuxClient?.Disconnect();
            _bayeuxClient?.WaitFor(1000, new[] { BayeuxClient.State.DISCONNECTED });
        }
    }

    public void Subscribe(string channel, IMessageListener listener)
    {
        _bayeuxClient.Handshake();
        _bayeuxClient.WaitFor(1000, new[] { BayeuxClient.State.CONNECTED });

        var sfChannel = _bayeuxClient.GetChannel(channel);
        sfChannel.Subscribe(listener);
    }
}
EN

回答 1

Stack Overflow用户

发布于 2020-09-11 20:19:01

只要通道上没有活动,服务器就会在特定时间后关闭连接。

在该时间内,客户端接收到403 (未知客户端)状态码,且客户端必须在110秒内再次握手。

默认情况下,CometD尝试在没有任何用户交互的情况下重新连接,如果客户端没有在预期时间内重新连接,服务器将删除客户端的CometD会话。

一旦连接重新连接,所有频道订阅都将被ComedD删除,我们必须再次订阅频道才能接收事件。

为此,我们必须使用meta/Handshake回调来重新订阅通道。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59014983

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档