TwitchLib / TwitchLib.PubSub

PubSub component of TwitchLib.
38 stars 52 forks source link

[BUG] OnPubSubServiceConnected is called when disconnecting from PubSub #121

Open ArcadeArchie opened 1 year ago

ArcadeArchie commented 1 year ago

i have a asp.net core hosted service that looks like this, when i stop the service at runtime(i.e. stream went down so pubsub is no longer needed) the disconnect works as expected but for some reason OnPubSubServiceConnected is triggered which it shouldnt

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
    _logger.LogInformation("Starting pubsub connection");
    try
    {
        _logger.LogInformation("Getting userId for {userName}", _options.TargetChannel);
        var res = await _apiClient.Helix.Users.GetUsersAsync(logins: new List<string> { _options.TargetChannel });

        if (!res.Users.Any())
            return;
        var user = res.Users[0];
        _pubSubClient.OnChannelPointsRewardRedeemed += _pubSubClient_OnChannelPointsRewardRedeemed;
        _pubSubClient.OnLog += _pubSubClient_OnLog;
        _pubSubClient.OnListenResponse += _pubSubClient_OnListenResponse;
        _pubSubClient.OnPubSubServiceConnected += async (o, e) => _pubSubClient.SendTopics(await RequestToken());
        _pubSubClient.ListenToChannelPoints(user.Id);
        _pubSubClient.ListenToUserModerationNotifications(user.Id, user.Id);

        _pubSubClient.Connect();

        _logger.LogInformation("Connected to pubsub");
        while (!stoppingToken.IsCancellationRequested) ;
    }
    catch (TaskCanceledException) { }
    catch (Exception e)
    {
        _logger.LogCritical(e, "PubSub worker failed unexpectly.");
    }
    finally
    {
        _logger.LogInformation("Stopping pubsub");
        _pubSubClient.Disconnect();
    }
}

image

ArcadeArchie commented 1 year ago

im using

Bukk94 commented 8 months ago

@ArcadeArchie Kinda late reply, but current (stable) version of TwitchLib.PubSub is using TwitchLib.Communication version 1.0.4 as an underlying client. This version of the communication library had issues with reconnection and proper disconnects. So instead of truly disconnecting, it just automatically reconnected, causing new OnPubSubServiceConnected to be fired again (you should also see active connection for that service).

This is already (mostly) fixed by new 2.0 communication version (there is still one more pending bug) and PubSub should be updated to 4.0 version. If you don't want to wait for stable, you can try using preview versions of the packages.