.net Cometd/bayeux客户端问题
我们创建了.Net核心windows服务,用于从Salesforce消费平台事件(每当Salesforce中创建/更新的特定对象想要获取信息时)。我们使用Comed/bayeux客户端订阅Salesforce平台事件 最初,一切正常。每当Salesforce对象发生更改时,我们都会获取数据,但在几个空闲小时(约1-2小时)后,没有数据获取。检查bayeux客户端状态,它显示为已连接,但订阅没有发生。当我们重新启动服务时,它开始工作。使用以下代码进行连接和订阅。 谁能帮我一下吗.net Cometd/bayeux客户端问题,.net,cometd,bayeux,.net,Cometd,Bayeux,我们创建了.Net核心windows服务,用于从Salesforce消费平台事件(每当Salesforce中创建/更新的特定对象想要获取信息时)。我们使用Comed/bayeux客户端订阅Salesforce平台事件 最初,一切正常。每当Salesforce对象发生更改时,我们都会获取数据,但在几个空闲小时(约1-2小时)后,没有数据获取。检查bayeux客户端状态,它显示为已连接,但订阅没有发生。当我们重新启动服务时,它开始工作。使用以下代码进行连接和订阅。 谁能帮我一下吗 public v
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);
}
}
public void CheckAndSubscribe()
{
如果(!\u bayeuxClient.IsConnected())
{
_logger.LogInformation(“Bayeux客户端未连接。正在尝试连接…”);
尝试
{
salesforceSessionData=\u sfSessionAdapter.GetSalesforceSession();
_连接(salesforcessiondata.Url,salesforcessiondata.SessionId);
列出sfChannels=\u syncSalesforceConfiguration.BayeuxClient配置.ExternalChannel;
foreach(sfChannels中的字符串通道)
{
_订阅(频道,_messageListener);
}
_logger.LogInformation(“Bayeux客户端已连接,频道已订阅…”);
}
捕获(例外情况除外)
{
_logger.LogException(ex);
}
}
}
公共类BayeuxClientAdapter:IBayeuxClientAdapter
{
BayeuxClient _BayeuxClient=null;
专用只读SyncSalesforceConfiguration\u SyncSalesforceConfiguration;
公共BayeuxClient适配器(IOOptions syncSalesforceConfiguration)
{
_syncSalesforceConfiguration=syncSalesforceConfiguration.Value;
}
公共广播已断开连接()
{
返回_bayeuxClient?.Connected??false;
}
public void Connect(字符串instanceUrl、字符串authToken)
{
int readTimeOut=120*1000;
字符串streamingEndpointURI=\u syncSalesforceConfiguration.BayeuxClientConfiguration.streamingEndpointURI;
IDictionary options=新字典(StringComparer.OrdinalIgnoreCase)
{
{ClientTransport.TIMEOUT_选项,readTimeOut},
{ClientTransport.MAX_NETWORK_DELAY_OPTION,120000}
};
var headers=new NameValueCollection{{HttpRequestHeader.Authorization.ToString(),$“OAuth{authToken}”};
var clientTransport=new LongPollingTransport(选项、标题);
var serverUri=新的Uri(instanceUrl);
String endpoint=String.Format(“{0}://{1}{2}”、serverUri.Scheme、serverUri.Host、streamingEndpointURI);
_bayeuxClient=newbayeuxclient(端点,new[]{clientTransport});
}
公共空间断开连接()
{
如果(断开连接())
{
_bayeuxClient?.ResetSubscriptions();
_bayeuxClient?.Disconnect();
_bayeuxClient?.WaitFor(1000,新[]{bayeuxClient.State.DISCONNECTED});
}
}
public void Subscribe(字符串通道、IMessageListener侦听器)
{
_bayeuxClient.Handshake();
_bayeuxClient.WaitFor(1000,新[]{bayeuxClient.State.CONNECTED});
var sfChannel=\u bayeuxClient.GetChannel(通道);
sfChannel.Subscribe(监听器);
}
}
只要频道上没有活动,服务器就会在特定时间后关闭连接
在此期间,客户端收到一个403(未知客户端)状态代码,客户端必须在110秒内再次握手
默认情况下,Comed尝试在没有任何用户交互的情况下重新连接,如果客户端未在预期时间内重新连接,服务器将删除客户端的Comed会话
一旦连接重新连接,所有频道订阅都将被COMDD删除,我们必须再次订阅该频道才能接收事件
为了做到这一点,我们必须利用元/握手回调再次重新订阅该频道