Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# HttpWebResponse Comet problem

I am wondering how I would go about reading a persistent connection with HttpWebRequest and HttpWebResponse. The problem seems to be that the GetResponseStream() function waits for the server connection to be closed before returning.

Is there an alternative easy way to read a comet connection? Example that doesn't work.

// get the response stream
        Stream resStream = response.GetResponseStream();

        string tempString = null;
        int count = 0;

        do
        {
            // fill our buffer
            count = resStream.Read(buf, 0, buf.Length);

            // as long as we read something we want to print it
            if (count != 0)
            {
                tempString = Encoding.ASCII.GetString(buf, 0, count);
                Debug.Write(tempString);
            }
        }
        while (true); // any more data to read?
like image 669
Kosaki Avatar asked Sep 18 '10 17:09

Kosaki


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

There is little reason to use HttpWebRequest if you can use WebClient instead. Have a look at the WebClient.OpenRead Method. I'm successfully using it to read from an infinite HTTP response like this:

using (var client = new WebClient())
using (var reader = new StreamReader(client.OpenRead(uri), Encoding.UTF8, true))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        Console.WriteLine(line);
    }
}

Note, however, that the point of "long-polling" is usually not to send a continuous stream of data, but to delay the response until some event occurs, in which case the response is sent and the connection closed. So what you're seeing might simply be Comet working as intended.

like image 200
dtb Avatar answered Sep 19 '22 23:09

dtb