I want to detect a disconnection from SSE (Server Send Events) on ASP.NET Core. Old way Response.IsClientConnected
not working. Do you have solution for this problem?
I found temporary solution to my question. Temporary because IsCancellationRequested
is detected when trying to send request and it failed, not when user disconnected before send request
if (HttpContext.RequestAborted.IsCancellationRequested == true)
{
break;
}
In .NET Core 2.0:
This method creates an endpoint that will return null
after 60 seconds. If the request is cancelled during those 60 seconds the method will return null
immediately.
[HttpGet]
public async Task<string> Get()
{
Request.HttpContext.RequestAborted.WaitHandle.WaitOne(60000);
return null;
}
This method does multiple units of work while the request is connected and until the work is completed.
[HttpGet]
public async Task<Items[]> Get()
{
Item item = null;
List<Items> items = new List<Items>();
do
{
item = DoRepitiousWork();
Items.Add(item);
}
while (!Request.HttpContext.RequestAborted.IsCancellationRequested && !item.IsLast);
return items.ToArray();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With