Auto-Reconnecting
When it comes to networking, disconnection can happen at any moment. Additionally, WOLF server force disconnects after an hour of a connection.
To address this, Wolfringo offers some built-in reconnection solutions.
Enabling auto-connection
Auto-reconnection without Wolfringo.Hosting needs to be enabled manually. WolfClient does not automatically reconnect on its own.
Wolfringo.Utilities (installed by Wolfringo metapackage automatically) includes WolfClientReconnector helper class that can be used for easy auto-reconnection.
WolfClientReconnector is a separate class to adhere to SOLID principles, and to make its reconnection logic independent on implementation of IWolfClient. Its constructor takes a IWolfClient that it'll handle reconnection for, and it'll listen to Disconnected event. Once disconnection happens, it'll trigger a reconnection.
To use it, create instance of ReconnectorConfig, and create a new instance of WolfClientReconnector using these options and instance of your IWolfClient.
_client = new WolfClientBuilder()
.WithAutoReconnection(reconnector =>
{
// ... configure reconnector here ...
})
.Build();
If you wish, you can also create reconnector separately - this way you can handle its errors, or manually disable it.
_client = new WolfClientBuilder().Build();
ReconnectorConfig options = new ReconnectorConfig();
WolfClientReconnector reconnector = new WolfClientReconnector(_client, options);
// do something with reconnector
Warning
Do NOT use WolfClientReconnector with Wolfringo.Hosting!
Configuring auto-reconnection
ReconnectorConfig class has a few properties which can be used to customize reconnection behaviour:
- ReconnectAttempts - number of reconnection attempts to make. If set to 0, reconnection will be disabled. If set to negative number, infinite amount of attempts will be made. Defaults to 5.
- ReconnectionDelay - a @System.TimeSpan that will be waited between reconnection attempts. 0 or negative values disable the wait time. Defaults to half second.
- Log - an @Microsoft.Extensions.Logging.ILogger to log any messages with. See Logging guide for more info. If null, logging will be disabled. Using with
WolfClientBuilder.WithAutoReconnection
will make it automatically use configured logging. Defaults to null. - CancellationToken - a @System.Threading.CancellationToken that will cancel any reconnections.
Auto-reconnection errors
If Log property of ReconnectorConfig is not null, all error messages will be logged using that @Microsoft.Extensions.Logging.ILogger.
If reconnector makes ReconnectorConfig.ReconnectAttempts to reconnect and each attempt fails, the reconnector will invoke FailedToReconnect event - you can listen to that event to handle reconnection failed in your code. FailedToReconnect event provides @System.UnhandledExceptionEventArgs, which in turn has @System.UnhandledExceptionEventArgs.ExceptionObject. This object will be set to an @System.AggregateException, which contains all exceptions that occured when trying to reconnect. See Microsoft docs to see how to handle that exception.
Warning
If ReconnectAttempts are set to infinite (negative value), no error event will be raised!
Disabling auto-reconnection
WolfClientReconnector will attempt to reconnect no matter what was the cause of disconnection. If you want to disable this behaviour, simply call Dispose(). Once disposed, reconnector will be disabled permanently - to re-enable, create a new WolfClientReconnector instance.
Tip
If WolfClientReconnector is not good enough for your needs, you can create your own reconnection with your own logic. See WolfClientReconnector source code if you need guidance or ideas.