Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect tcp disconnection #690

Open
mattheys opened this issue Jan 10, 2025 · 0 comments
Open

Detect tcp disconnection #690

mattheys opened this issue Jan 10, 2025 · 0 comments

Comments

@mattheys
Copy link

Hi, I'm using System.MonitorEventsAsync and connecting to a tcp socket.

Sometimes the machine I'm connecting to is restarted and I lose connection, how can I detect this and reconnect?

    private async Task StartDockerClient(CancellationToken cancellationToken)
    {
        Credentials credentials = null;

        if (dockerHostSettings.Username is not null && dockerHostSettings.Password is not null)
        {
            credentials = new Docker.DotNet.BasicAuth.BasicAuthCredentials(dockerHostSettings.Username, dockerHostSettings.Password, dockerHostSettings.UseTls);
        }

        client = new DockerClientConfiguration(
            endpoint: new Uri(dockerHostSettings.Endpoint),
            credentials: credentials
            ).CreateClient();

        var progress = new Progress<Message>();
        progress.ProgressChanged += Progress_ProgressChanged;

        IList<ContainerListResponse> containers = await client.Containers.ListContainersAsync(
            new ContainersListParameters()
            {
                Filters = new Dictionary<string, IDictionary<string, bool>>()
                {
                    {
                        "status",
                        new Dictionary<string, bool>()
                        {
                            { "running", true }
                        }
                    },
                }
            }
        );

        foreach (var item in containers)
        {
            var container = await GetContainer(item);
            if (container is null) { continue; }
            await (ContainerStarted?.Invoke(container) ?? Task.CompletedTask);
        }

        client.System.MonitorEventsAsync(new ContainerEventsParameters(), progress, cancellationToken);
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant