I have concern about how to properly stop/close SignalR stream. It is server-to-client streaming.
I'll try to explain the context of streaming. We have web service that connects to IP camera and opens stream with it. Later in service we biometrically process that images(frames) and than stream that biometrically processed images to clients that are connected to SignalR stream. There aren't many connected client, maybe few 2, 3 or 4 max.
So, my concern is, if one of that client wants to disconnect from stream, does it need to call CancelationToken.Cancel()
or we could just let him break from while(true)
loop on server? When that client disconnects from stream, stream should continue to work as other clients are still connected.
Some brief example on how could client break from while(true)
loop.
public async IAsyncEnumerable<PreviewImage> StartStreamingPreviewImages(string deviceID, [EnumeratorCancellation] CancellationToken cancellationToken)
{
while(true)
{
if(client.WantToLeaveStream)
{
break;
}
}
...
or
public async IAsyncEnumerable<PreviewImage> StartStreamingPreviewImages(string deviceID, [EnumeratorCancellation] CancellationToken cancellationToken)
{
while(true)
{
if(cancellationToken.IsCancellationRequested)
{
cancellationToken.ThrowIfCancellationRequested();
}
}
...
I went through docs on Microsoft site SignalR stream, and they say that we should use CancelationToken
to stop stream, but i also tried stopping it by just leaving while(true)
loop and nothing bad happened, stream just stopped. Is that ok? :/
Breaking the loop is fine. The method is the one producing stream items, or more specifically the one providing stream items to SignalR, so if you break the loop, you stop producing the items from SignalRs perspective. The docs mention the token because the client can disconnect or cancel listening to the stream and your server probably wants to stop doing extra work in that case.