SlackAPI
SlackAPI copied to clipboard
No way to know if connection failed?
Maybe I'm just not seeing it, but it seems to me that there is no way to know if calling SlackSocketClient.Connect(...) fails.
For example, calling the method with an invalid token will not raise any exception or emit any Event. Could this be added? Seems like quite an important part of such a library.
I found this issue too. I found a workaround in the following way.
Create a ManualResetEventSlim that only continues after a succesful connection, and let it fail after a timeout.
ManualResetEventSlim ClientReady = new ManualResetEventSlim( false );
Client = new SlackSocketClient( SlackSettings.SlackAPIOAuthToken );
Task.Run( ( ) =>
{
try {
Client.Connect((LoginResponse) =>
{
if ( LoginResponse.ok )
{
ClientReady.Set( );
}
});
}
catch ( Exception )
{
Enabled = false;
Error( "Slack data source could not connect to slack and will be disabled." );
return;
}
} );
And then somewhere else in your code:
//Ensures the client is ready to be used
if (!ClientReady.Wait(TimeSpan.FromSeconds(SlackSettings.SlackConnectionTimeoutSeconds)))
{
Error("Slack did not succesfully connect within " + SlackSettings.SlackConnectionTimeoutSeconds + " seconds.");
Error("Slack will not be polled");
return;
}
Enabled = true;