Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting a disconnection from SSE on ASP.NET Core

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?

like image 951
Obin Avatar asked Jul 03 '16 02:07

Obin


2 Answers

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;
} 
like image 53
Obin Avatar answered Nov 14 '22 23:11

Obin


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();
}
like image 41
N-ate Avatar answered Nov 14 '22 23:11

N-ate