Re: [Cppcms-users] How to combine SSE with the url dispatcher
Brought to you by:
artyom-beilis
|
From: eric m. <mid...@gm...> - 2014-02-22 16:48:03
|
Hi Marcel,
I did read your emails thoroughly, although missed the link to the
applications_pool pages, as I thought you meant some piece of code by [0]
at first. I do understand that static ticker example. That is not the
problem, anymore.
What I indeed want is what you mentioned in your last response: I want the
streamed data to be displayed on a webpage that is not produced statically
(from the filesytem) but dynamically. I assumed that that could be done
from the same application but apparently that is not the case. You mention
multiple applications. Could it be as simple as registering both an
asynchronous application and a synchronous application in main() like this
or is that not possible at all?
cppcms::service service(argc, argv);
booster::intrusive_ptr<AsyncApp> asyncServer = new
AsyncApp(service);
service.applications_pool().mount(asyncServer);
service.applications_pool().mount(cppcms::applications_factory<SyncApp>());
service.run();
In the AsyncApp class I have implemented the ticker example and in the
SynApp class a variant of the tut_url_mapping example.
The constructor of AsynApp has mapper().root("/ticker"); and that of
SyncApp has mapper().root("/robot");
The config file is as follows:
{
"service" : {
"api" : "http",
"port" : 8080
},
"http" : {
"script_names" : [ "/robot", "/ticker" ]
},
"file_server" : {
"enable" : true,
"document_root" : "."
},
}
I added a method virtual string report() to cppcms::application that is
called every time application::main() is called and reports which
application calls it.
If I only mount the SyncApp and access the page by
127.0.0.1:8080/robot/showstatus than showstatus.html is displayed and the
following output follows on the console:
Sync::Sync() called
SyncApp->CppCMS::application::main(/showstatus) called
SyncApp::showStatus() called
SyncApp->CppCMS::application::main() called
If I only mount the AsyncApp and access the stream via
127.0.0.1:8080/showstatus.html (with var stream = new
EventSource('/ticker');) then the showstatus.html is displayed and the
stream of tickers updates the page as expected. The following output
follows on the console:
Async::Async() called
AsyncApp::main called
If I mount both SyncApp and AsyncApp and access
127.0.0.1:8080/showstatus.html than the page is displayed but no streaming
data. The following output follows on the console:
Async::Async() called
Sync::Sync() called
SyncApp->CppCMS::application::main() called
SyncApp->CppCMS::application::main() called
Which means to me that SyncApp gets to handle the request and not AsyncApp.
If I mount both and access 127.0.0.1:8080/robot/showstatus that the page is
displayed but no streaming data. The output on the console is slightly
different but the result is the same:
Async::Async() called
Sync::Sync() called
SyncApp->CppCMS::application::main(/showstatus) called
SyncApp::showStatus() called
SyncApp->CppCMS::application::main() called
Here also SyncApp::main is called and not AsyncApp::main.
Can you explain why the SyncApp supersedes and how I can make the AsyncApp
react on the streamrequest? It must be possible to implement the ticker
example using a dynamically created page right because that's what I'm
after.
Anyone who has a clue, please react.
Kind regards,
Eric
If I don't mount the SyncApp it works exactly as the ticker example,
updating the static page with the streamed data. If I don't mount the
AsyncApp it works exactly as the tut_url_mapping example. I if mount both
and send the same page to the browser but now dynamically from SyncApp,
there is no streaming data, which I would have expected as the page is
exactly the same page and there is an asynchronous app registered that
proved to respond when the page was loaded statically. Any ideas?
The server will be approached by only a single user so multiuser aspects
are no issue here.
Greetz,
Eric
On Sat, Feb 22, 2014 at 2:11 PM, Marcel Hellwig <ke...@co...> wrote:
> Hi Eric,
>
> I have the feeling that you don't read my mails... I apologize, that I'm
> not a native speaker but I think, that you can understand what I write.
>
> I'd like to mention it once again.
>
> ***A stream is not something that you view in your browser!***
>
> It's not a webpage, it's a stream of information that can be access and
> evaluated by a javascript/cpp/perl/python/... programm.
> There are two major problems in your approach.
>
> 1. You view the stream in your browser. That's not how it works. Please
> adapt your 'showstatus.html' in line 14 to this:
>
> > var stream = new EventSource('/robot/showstatus');
>
> and replace the showStatus method with this
>
> > void MyApp::showStatus()
> > {
> > stream_->accept(release_context());
> > }
>
> Then you can access it via:
>
> > http://localhost:8080/showstatus.html
>
> and see the promised result. I also attached the 'corrected' version.
>
> I think, that your problem is, that you want both - stream and page - in
> on method, but that won't work. You must have a synchronous app that
> will deliver the page content [0] and a asynchronous app that will
> deliver the data via sse.
> The static content will be delivered via the internal file server of
> cppcms in this way, but you should not rely on that in a productive
> environment [1].
> So. You need one application for the sse part and another application
> for the webpage part.
>
>
> The second major problem in your current version is, that you cannot
> open that (wrong) page twice. Try to open another tab and view that page
> again. It won't work. The can and should read the reason why [2][3].
>
> So I hope that you won't be angry. Read the given pages and then I hope
> you can answer your questions that you have. Of course, feel free to ask
> more questions.
>
> Greetings,
> Marcel
>
>
> [0] http://cppcms.com/wikipp/en/page/cppcms_1x_tut_hello
> [1] http://cppcms.com/wikipp/en/page/cppcms_1x_serving_static_files
> [2] http://cppcms.com/wikipp/en/page/cppcms_1x_application_lifetime
> [3] http://cppcms.com/wikipp/en/page/cppcms_1x_event_loop
>
--
Groetjes,
Eric
http://www.lightandmagicphotography.nl
http://www.lightandmagicphotography.com
|