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

Allow to await ConnectAsync() #117

Closed

Conversation

DmitriySalnikov
Copy link

This PR does not change the behavior of this function in the already existing code.
But it allows you to wait for the connection attempt to complete.

@BarRaider
Copy link
Owner

I don't think this is a good update as it will confuse people to think that after an await they can immediately call the websocket methods, instead of listening to the connect event

@DmitriySalnikov
Copy link
Author

DmitriySalnikov commented Oct 24, 2022

Yes, I forgot that there is still a need for authorization. But one more NOTE can be added or <returns></returns>.

At the moment I'm using await ConnectAsync() to do auto-reconnection in an infinite loop. How to do it simply, without errors and without await I don't know.

@DmitriySalnikov
Copy link
Author

Added a clarification.

@BarRaider
Copy link
Owner

Sorry, I still don't understand why I would want to push this. It's confusing.
What's your use-case for putting this in await?

@DmitriySalnikov
Copy link
Author

What's your use-case for putting this in await?

I have already written this

At the moment I'm using await ConnectAsync() to do auto-reconnection in an infinite loop. How to do it simply, without errors and without await I don't know.

Simplified version of my code:

async void ReconnectionThread()
{
    while (true)
    {
        try
        {
            await obs.ConnectAsync("ws://localhost:4455", "pass");
        }
        catch {}

        if (obs.IsConnected || reconnectCancellationToken.IsCancellationRequested) return;

        Thread.Sleep(4000);
    }
}

It's just a Task that runs when disconnected from OBS.

It is as simple and convenient as possible. If you do the same without await, you may have problems trying to connect during another connection attempt or reconnecting when the connection is already established.

@BarRaider
Copy link
Owner

Your code would work EXACTLY the same without that await, given there is nothing blocking in the ConnectAsync function.
Overall, the right way to do this is to listen to the Connect and Disconnect events and use them to decide when to call ConnectAsync again.

@BarRaider BarRaider closed this Oct 25, 2022
@DmitriySalnikov
Copy link
Author

DmitriySalnikov commented Oct 25, 2022

@BarRaider Will #100 be ignored?

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

Successfully merging this pull request may close these issues.

2 participants