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

UserDataWebSocketManager - no always getting first events #71

Closed
zeldomar opened this issue Feb 26, 2018 · 7 comments
Closed

UserDataWebSocketManager - no always getting first events #71

zeldomar opened this issue Feb 26, 2018 · 7 comments

Comments

@zeldomar
Copy link

Hello,

I'm using the UserDataWebSocketManager like this (api 0.2.0-alpha28) :

UserDataWebSocketManager webSocket = new UserDataWebSocketManager();

webSocket.OrderUpdate += OnBinanceOrderUpdate;
webSocket.TradeUpdate += OnBinanceTradeUpdate;

CancellationTokenSource webSocketCancellationToken = new CancellationTokenSource();
Task webSocketTask = Task.Run(() => WebSocket.SubscribeAndStreamAsync(ApiUser, _webSocketCancellationToken.Token));

And immediately after I send an order, and I only receive the order update (with status = New) one time out of three.

Is that because I don't leave enough time to the SubscribeAndStreamAsync method to really establish the connection ?

Thanks a lot

@pingu2k4
Copy link

Is that because I don't leave enough time to the SubscribeAndStreamAsync method to really establish the connection ?

Most likely, yes. Although I would note that rarely I have seen an order complete, but the completed order never came through the user data socket. This was a problem from Binance rather than this API wrapper though, the log files didnt show it being recieved anywhere. That was a rare occurence though.

@zeldomar
Copy link
Author

@pingu2k4

Ok. Is there a wordaround please ?
Do a "sleep" ?

@sonvister
Copy link
Owner

sonvister commented Feb 26, 2018

@zeldomar, I appreciate you mentioning the use case and issue.

The StreamAsync methods combine a web socket connect with a receive loop where the first await is on the connect operation (using a Task.Run wrapper would return control even sooner). A sufficient delay should work, but waiting for the IWebSocketClient.Open event should be more deterministic.

In 0.2.0-alpha37 the IWebSocketClient is at IUserDataWebSocketManager.Controller.Stream.WebSocket.

@sonvister
Copy link
Owner

The IWebSocketClient.IsStreaming flag is set before connecting, so that would not help in this case.

@zeldomar
Copy link
Author

@sonvister

I'm sorry, I thought that I give enougt details.
Otherwise, using the IWebSocketClient.Open event is perfect.

Here my code for others :

WebSocket.Client.Open += OnWebSocketOpen;
ManualResetEvent _signalEvent = new ManualResetEvent(false);

_webSocketTask = Task.Run(() => WebSocket?.SubscribeAndStreamAsync(ApiUser, _webSocketCancellationToken.Token));
_signalEvent.WaitOne();


private void OnWebSocketOpen(object sender, EventArgs e)
{
   _signalEvent.Set();
}

Thanks all for your help.

@sonvister
Copy link
Owner

@zeldomar, I thought you gave enough information. My comment about the IsStreaming flag was a correction to my previous (edited, but you never know when it is read) comment where I had mentioned it as an alternative to the event (could have used a Markdown strike-through if such a thing exists).

I'd recommend ManualResetEventSlim over ManualResetEvent here. Also, I'll likely be adding a Task based event wait extension soon that can be awaited for an even lighter alternative.

@zeldomar
Copy link
Author

@sonvister

Thanks for the ManuelResetEventSlim tip.

I close this thread because the issue is solved.

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

3 participants