Reputation: 1105
UPDATE: Thank you for the help so far. I've just tested the program connecting directly to it from the browser, instead of thru an XMLHttpRequest. Going straight from the browser is working flawlessly.
However, this connection must be handled via an XMLHTTPRequest. According to FireBug, it's receiving the full response (31 bytes in this case). It closes the connection, sets the readyState to 4. But the responseText is completely empty.
I'm creating a C++ app that accepts connections and responds as if it were an HTTP Server. My goal is to create a real-time chat server by opening connections to this C++ app, and responding with a "page" that continues to load as new messages are sent. I am currently sending the following back:
HTTP/1.1 200 OK\r\n
Transfer-Encoding: chunked\r\n
Content-Type: text/plain\r\n
\r\n
Up to this point, everything works. Using FireBug, I can see that it is properly receiving and interpreting headers. However, I cannot figure out how to forward response text. I know that in plain text, it would be read as follows:
5
Hello
8
Good bye
But every iteration I've tried (with \r\n, without \r\n, counting \r\n as 2 additional bytes) so far does not get properly read by the browser as response text. Can somebody help with crafting a proper string to send as response text?
Upvotes: 1
Views: 3304
Reputation: 33655
You're trying to implement "HTTP Push" or HTTP streaming or whatever, the issue is that not all browsers will support this correctly, for browsers such as firefox/opera etc, you could try the mime-type multipart/x-mixed-replace
, so as long as you keep the connection live and send stuff down, firefox should read, but this will not work in IE...
Upvotes: 1
Reputation: 17320
Are you using hex for your lengths? The \r\n after the chunk length should not be counted in the length.
Also, try closing out the page with a 0 length. That will let you know if the browser is just buffering before parsing.
Upvotes: 0
Reputation: 5143
You should end the transfer with a zero-length chunk:
5
Hello
8
Good bye
0
Otherwise the browser does not know you are finished.
Upvotes: 2