It looks like you're new here. If you want to get involved, click one of these buttons!
callback = async t =>
{
try
{
byte[] tempBuff = new byte[_bufferLength];
int offset = t.Result.Count;
bool endOfMessage = t.Result.EndOfMessage;
// if chunk has even more data yet to recieve do that synchronously
while (!endOfMessage)
{
WebSocketReceiveResult result = _ws.ReceiveAsync(new ArraySegment<byte>(tempBuff), CancellationToken.None).Result;
Array.Copy(tempBuff, 0, buffer, offset, result.Count);
offset += result.Count;
endOfMessage = result.EndOfMessage;
}
// send data to process
OnData?.Invoke(buffer, offset, t.Result.MessageType.ToString());
// Again try to receive data
await _ws.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None).ContinueWith(callback);
}
catch (Exception e)
{
if (IsConnected())
{
OnError?.Invoke("Error while recieving data. Message: " + e.Message);
}
else
{
OnError?.Invoke("Lost ticker connection.");
}
}
};
// To start the receive loop in the beginning
await _ws.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None).ContinueWith(callback);
I apologize but i think async has cast a spell on you. and you are effectively bewitched by it.
by the looks of it and i could be wrong but your control will never pass ahead of
await _ws.ReceiveAsync(new ArraySegment(buffer), CancellationToken.None).ContinueWith(callback);
I could be wrong because this is based on the glance over the code you presented.
await should return the control to previous function. but wont ever reach the next line.
but since there is nothing after that line so you might think your code is working on account of
control left. but actually it is stuck there for ever.
And websocket does not allow 2 simultaneous receives or sends while previous is still in progress.
it can be
and there is a good reason for that.
Above code is happening Asynchronously only because it is being initiated by RecieveAsync. it is just that inside callback. order is maintained.
putting of async in front of t => should have no effect ideally. because it is allready
pursuing async operation. and will continue to work as it is.
although your putting of await in front on _ws.ReceiveAsync
is causing suspicion. try by adding Console.WriteLine( "reached 1" ) ; after that line to see if control ever reaches there.
once again i could be wrong about all of it. because i use Tasks directly instead async.
Thanks
Regards
Thanks for your patient reply. I'm able to receive the ticks, but after a few minutes, the UI freezes, with or without async execution. That's why I'm trying to optimize the code and examining it from the ground up. I am neither a software professional nor formally trained in these things. This is my pet project I'm trying to develop during my spare time. I have a full time job that is neither related to software nor the stock market. So, as would happen with any self trained person, it should be no surprise if I pick up some bad habits on the way. I'm willing to mend my ways...
The whole task/async-await thing is new to me and my obsession for async-await pattern stems from the fact that this is a mobile app for android (I'm using Xamarin Forms) and I want my code to run optimally efficient. There's nothing ahead of this line for execution in that method. And, it kickstarts the loop with a callback and I'm receiving the ticks for about a few minutes, say 5-10 minutes after which the UI just blanks out. And the problem is not really about not being able to start it but about making it continue to work efficiently. I agree and I have now removed the async ahead of t (for the callback) and await inside the loop. I think that ensures synchronisation and order while still making the method awaitable. Could you please give a few pointers on how to achieve seamless tick processing? I'll try to explain what I'm trying to do. Please pardon me if any of this doesn't make any sense.
I'm subscribing to websocket with about 600 instruments in 'quote' mode. And trying to build a intraday candle from these ticks. Snippet below: I know this is far from being efficient, but I'm unable to wrap my head around this. pic when it is working:
pic when the list blanks out after a few minutes:
kindly see this post. if it is of any relevance to you.
then we can proceed further.
i have never programmed for mobile but few things come to mind.
Freeze could be because of limited resources on
mobile. that too if you are aggregating data on your set.
and remove await from _ws.ReceiveAsync outside of callback also . it serves no purpose.
asyncing of websocket is not dependent on that.
Thanks
Regards
So basically the issue is about tracking too many ticks for the processing power available? I thought my smartphone is more powerful than my laptop.
Ok, will check it out and revert.
Thanks for your kind support. Thumbs up!
so that hardware capabilities are known.
Thanks
Regards
I'm glad to inform that the ticks are processed just fine. It was the android environment that is killing the websocket connection. I have subscribed to 660 instruments today and concluded this in two ways:
btw, my phone specs
Poco F1 with Qualcomm Snapdragon 845 (2.8 GHz quad core + 1.8 GHz quad core) 64 bit 6GB RAM
Cool
Thanks
Regards