-
Notifications
You must be signed in to change notification settings - Fork 122
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Canceling the ConnectAsync() with a CancellationToken #377
Comments
Hi @Aspher0, thanks for your feedback. make a new overload to let user pass a CancellationToken is a good idea. but currently we have an option: ConnectionTimeout, the lib will create CancellationToken based on ConnectionTimeout. What will happen if we have the following code? using var io = new SocketIO(new SocketIOOptions
{
ConnectionTimeout = TimeSpan.FromSeconds(1)
});
using var cts = new CancellationTokenSource();
await io.ConnectAsync(cts.Token);
...
cts.Cancel(); Questions:
|
Hello there, thanks for your answer and interest. First of all, if you want context, I use your library in my project named "GlamMaster" which can be found here, in SocketManager.cs and SocketOnEventsManager.cs : https://github.com/Aspher0/GlamMaster/blob/main/GlamMaster/Socket/ So, I do have a ConnectionTimeout already set, but it doesn't fixes anything. Let me explain. I did end up finding a way to avoid every problems, but still, the events still being fired even after being unsubscribe and the client disposed, as well as the other issues I mentionned, makes my solution very messy and dirty. Now, let's say you make an overload of the ConnectAsync function, you could let the user store their cancellationtoken on their side (you wouldn't need to worry about taking care of it, the user has to take care of that) and then you would just pass the token in the overloaded function and all of its tasks and stuff ? Basically : try
{
await io.ConnectAsync(cts.Token);
}
catch (OperationCanceledException e)
{
// The socket connection has been aborted, do stuff
}
// Other catches here if needed
finally
{
cts.Dispose();
} I saw that you have a while loop that basically check if the token is cancelled or not, maybe have a boolean variable that specifies if it's a user-specified token or the automatically generated one. |
Hello @doghappy |
Hi, Sorry for the slow response. My boss gave me too much work 馃ぃ and I have to take care of my baby馃懅, so I can only do maintenance no more than 8 hours a week. Thanks for your explanation, by default the lib will always try to connect to the server when you calling And here is a solution: https://stackoverflow.com/a/29623382/7771913 Please leave a message if if (useful)
{
Thread.Sleep(TimeSpan.FromDays(7));
}
DoItAsap(); |
Hey @doghappy I haven't tried the Also, the stackoverflow link you linked doesn't seem relevant in our case, I don't really see how it could help since your project does not let the user pass a cancellation token to the If the solution does not work, I will retry forking your project again and add an overload of the ConnectAsync function and maybe open a pull request, if I can get it to work properly this time 馃槃 Take care ! |
Hello dog 馃槃
First of all, thanks for the project, it helped me a lot.
However, I'm having struggle to cancel an ongoing connection attempt initiated with ConnectAsync, would it be possible to maybe add an overload to the ConnectAsync() method with a CancellationToken parameter so we can cancel it anytime please ?
I tried forking the project but I ended up breaking everything, I'm sadly not that talented ahah
I tried disposing the client in both 3.1.1 version and 3.0.8 as someone had suggested in the issues, but no luck, it will keep trying to connect even after disposing.
Thanks in advance for your answer!
The text was updated successfully, but these errors were encountered: