Wolfringo Wolfringo
Wolfringo Wolfringo
Wolfringo (c) 2020 TehGM
DocFX, DiscordFX theme.
Search Results for

    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.

    • Without Wolfringo.Hosting (Normal Bot)
    • With Wolfringo.Hosting (.NET Generic Host/ASP.NET Core)

    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.

    Enabling auto-connection

    HostedWolfClient, which Wolfringo.Hosting wrapper for WolfClient, includes reconnection behaviour and it's enabled by default. You don't need to perform any additional steps to turn it on.

    Configuring auto-reconnection

    Reconnection in Wolfringo.Hosting can be customized by changing properties of HostedWolfClientOptions. You can do it either using appsettings.json, or configuration methods in ConfigureServices:

    services.AddWolfClient()
        .SetAutoReconnectAttempts(5)
        .SetAutoReconnectDelay(TimeSpan.FromSeconds(0.5));
    
    • AutoReconnectAttempts - 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.
    • AutoReconnectDelay - a @System.TimeSpan that will be waited between reconnection attempts. 0 or negative values disable the wait time. Defaults to half second.

    Auto-reconnection errors

    HostedWolfClient will automatically use configured logging to log each error that happens when reconnecting.

    If client makes AutoReconnectAttempts to reconnect and each attempt fails, the client will invoke ErrorRaised event. Its @System.UnhandledExceptionEventArgs contains @System.UnhandledExceptionEventArgs.ExceptionObject property, which 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.
    After raising the error, the client will act accordingly to HostedWolfClientOptions.CloseOnCriticalError setting - if it's set to true, the application will be terminated; otherwise, application will keep running, but bot won't reconnect unless reconnection is triggered manually.

    Note

    HostedWolfClientOptions.CloseOnCriticalError setting is used by more than just reconnection attempts - it also determines how the application will behave when initial connection fails, and when bot has failed to log in automatically.

    Disabling auto-reconnection

    To disable auto-reconnection, simply set AutoReconnectAttempts to 0. Alternatively, you can disable it in ConfigureServices:

    services.AddWolfClient()
        .DisableAutoReconnect();
    
    Tip

    Unlike WolfClientReconnector, HostedWolfClient is aware if the disconnection was manual - if so, it will not attempt to reconnect. Because of this, you don't need to disable reconnection if you're calling DisconnectAsync.