[Cppcms-users] How to combine SSE with the url dispatcher
Brought to you by:
artyom-beilis
|
From: eric m. <mid...@gm...> - 2014-02-22 10:20:26
|
Hello, I'm trying to combine the features of the ticker example ( https://svn.code.sf.net/p/cppcms /code/framework/trunk/contrib/server_side/sse/), i.e. streaming data from the server to update a webpage with url dispatching instead of using the default main function and a static html page. So instead of the following code in ticker.cpp: void main(std::string /*url*/) { stream_->accept(release_context()); } I would like to use something like: MyApp::MyApp(cppcms::service &service) : cppcms::application(service), tm_(service.get_io_service()), price_(1.0) { dispatcher().assign("/camerasettings",&MyApp::cameraSettings,this); mapper().assign("Camera Settings","/camerasettings"); mapper().root("/robot"); stream_ = sse::state_stream::create(service.get_io_service()); wait(); } void MyApp::cameraSettings() { string page = FileSystem().readFileIntoString("the_ticker.html"); response().out() << page.c_str(); stream_->accept(release_context()); } While having all the rest the same as in the ticker example. I open the page with 127.0.0.1:8080/robot/camerasettings and the page is displayed correctly but the stream keeps loading and is not being displayed. After stopping the server, the data is displayed and looks like this: Stock Price Price: id:8 data:2.54208 id:9 data:2.97668 id:10 data:3.19061 id:11 data:2.67639 id:12 data:3.28474 id:13 data:3.08663 id:14 data:2.30425 id:15 data:1.74076 id:16 data:2.41898 id:17 data:2.01105 id:18 data:2.05962 id:19 data:3.00517 id:20 data:3.54789 id:21 data:4.08772 id:22 data:4.87077 id:23 data:4.57569 id:24 data:5.41374 id:25 data:6.3124 If on the other hand the stream is accepted before the response is sent, like this void MyApp::cameraSettings() { string page = FileSystem().readFileIntoString("the_ticker.html"); stream_->accept(release_context()); response().out() << page.c_str(); } an exception is thrown: cppcms, error: Caught exception [Access to unassigned context] which seems to be kind of logical, the page does not show, but after stopping the server, the browser displays the streamed data as follows: id:15 data:1.74076 id:16 data:2.41898 id:17 data:2.01105 id:18 data:2.05962 id:19 data:3.00517 id:20 data:3.54789 So I have two questions: 1) How do I combine server side events with url dispatching in cppcms? As I would like to create and send the page to the browser from a method and start to stream events after the page has loaded. 2) How do I stop the stream if the user switches to another page or does it stop itself? I can provide full sourcecode if necessary. Sofar I couldn't find other comparable examples. My guess is that it shouldn't be all that difficult once you know, but currently I do not (-; Any help is welcome here. Best regards, Eric |