Add-Ons & Services

Server-Sent Events using Poco::Net::HTTPRequestHandler

Please post support and help requests here.
Posts: 1
Joined: 02 Apr 2013, 04:02

Server-Sent Events using Poco::Net::HTTPRequestHandler

Postby M4X » 02 Apr 2013, 04:34


I'm trying to "stream" data to an HTML5 page.
This tutorial http://www.html5rocks.com/en/tutorials/ ... ce/basics/ was quite helpful to get the client side working.
But for the server side, I'm doing something similar to the HTTPServer example in http://pocoproject.org/slides/200-Network.pdf
The html5rocks.com tutorial gave me the following idea for the request handler's code:

Code: Select all

void MyRequestHandler::handleRequest (HTTPServerRequest &req, HTTPServerResponse &resp)
   resp.add("Content-Type", "text/event-stream");
   resp.add("Cache-Control", "no-cache");
   ostream& out = resp.send();
   while (out.good())
      out << "data: " << "some data" << "\n\n";


and the HTML5 page's source:

Code: Select all

<!DOCTYPE html>
      <p id="demo">hello</p>
         var msgCounter = 0;
         var source;
         var data;
         if(typeof(EventSource) !== "undefined")
            source = new EventSource('/stream');
            document.getElementById("demo").innerHTML = "Event source created";
            document.getElementById("demo").innerHTML = "Are you using IE ?";

         source.addEventListener('message', function(e)
            document.getElementById("demo").innerHTML = "Message received (" + msgCounter + ") !<br/>"+ e.data;
         }, false);

The good thing is that, when opening the html page, the data gets streamed and I get a correct outpout (the text between the <p id="demo"> tag gets updated as expected.
The problem is that when I close the page in the browser, the POCO program crashes, and I get the following message in the console:

Code: Select all

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

Process returned 3 (0x3)   execution time : 22.234 s
Press any key to continue.

(I'm using Code::Blocks, that's why the return value and the execution time are displayed)
Event when I put the while() loop between try{ }catch(...){} the program still crashes without entering the catch (same thing happens when I put the entire main()'s content in between try/catch )
The main program contains only these instructions:

Code: Select all

int main(int argc, char* argv[])
   MyServerApp myServer;
   myServer.run(argc, argv);

   return 0;

I want to know what could cause that crash and how I can fix it, please.
Thank you in advance for your help :)

Return to “Support”

Who is online

Users browsing this forum: No registered users and 1 guest