Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long Polling from Server Side C#

I'm more of a php programmer, not c#, but bear with me.

I have an application that connects with an API that sends push messages from time to time. It uses long polling as the mechanism. I've looked into using signalR for this, but all the examples show the server pushing the messages. I want the server to recieve the messages via longpolling. Does anyone know how to do this?

like image 599
user3413723 Avatar asked Mar 18 '23 18:03

user3413723


1 Answers

Yes. Just start request to the remote endpoint and wait for the response. When you have have processed the response, start again. It's as simple as that.

public async Task LongPoll(Uri remoteEndPoint)
{
    for(;;)
    {
        string data;
        using(var wc=new WebClient())
        {
            data = await wc.DownloadStringTaskAsync(remoteEndPoint);
        }
        Process(data);  
    }
}

I've ignored cancellation here, but you'll need to think about how polling ends if you ever want to terminate your app gracefully.

like image 180
spender Avatar answered Mar 30 '23 08:03

spender