Skip to content

Detect tcp disconnection #690

@mattheys

Description

@mattheys

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);
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions