Receiving streaming data over socket within Wt Application
Added by Rajeev Gajbhiye over 11 years ago
Hi,
First of all I am new to socket programming. Hence, I may be making some obvious mistake in my implementation.
There is some information periodically being streamed over the socket. My requirement is to receive the information and update the page accordingly, say update a plot or display in a text box as metered value.
I have attached an example code. I have built this by modifying hello world example and few examples from boost asio library. Also attached is a makefile. The command 'make all' will generate three executables namely sender, receiver and receiver_app. The sender sends message 'Message count' after each second over the port 8085. The receiver receives the message and prints it in the console. On the other hand, receiver_app is Wt version of this console based application. It is expected to do display a page with button 'Start' which on being pressed. I have used boost::asio library for all socket communication purpose.
Now the problem is as follows. The text based application 'receiver' works fine, however, receiver_app does not seem to receive any message. However, sender is successfully sending the messages. Please suggest what is going wrong? Is the problem with inbuilt websocket server? Can this requirement be met, may be some other better way? I also tried using WIOService instance from WServer as io_service. This actually crashed the application.
If I enable c++11 flag, then either the application crashes, or if server manages to start then page doesn't load i.e. browser waits infinitely for response.
Regards,
Rajeev.
Replies (7)
RE: Receiving streaming data over socket within Wt Application - Added by Koen Deforche over 11 years ago
Hey,
Without any change, App.cpp seems to be missing code that runs the service (i.e. io_.run() is not called anywhere).
However, the best way forward is to use the WServer io service. That this crashes is probably a good sign, at least something is happening... have you tried to investigate where exactly it is crashing?
Regards,
koen
RE: Receiving streaming data over socket within Wt Application - Added by Rajeev Gajbhiye over 11 years ago
Hi,
I forgot to include that part of code. However, it doesn't even run with _io.poll(). I do not want to use _io.run() as it becomes blocking. Even if I do, the page simply shows 'Loading indicator'. I have included modified code with _io.poll() inside start() method of MessageApplication. Even with this change, the message is not updated over the browser. I have tried WServer's ioservice, but no improvement. In fact, it crashes in this case. I am attaching the code for the same as well. I could extract following information from core dump.
Program terminated with signal 11, Segmentation fault.
#0 0x00007fb00e74ded9 in Wt::WWebWidget::canOptimizeUpdates () at /home/rajeev/Downloads/Softwares/Wt/wt-3.3.0/src/Wt/WWebWidget.C:2347
Regards,
Rajeev.
RE: Receiving streaming data over socket within Wt Application - Added by Koen Deforche over 11 years ago
Hey,
Using Wt's I/O service is almost getting you there.
The core dump indicates you are trying to manipulate a Wt session without having it's update lock, which is probably happening in this line:
receiver->received_message().connect(boost::bind(&WText::setText, message_, _1));
(i.e. the call to WText::setText())
Inside your callback you should first get an update lock, or post to the session. You also will need to enable server push.
See the serverpush and broadcast feature examples.
Regards,
koen
RE: Receiving streaming data over socket within Wt Application - Added by Rajeev Gajbhiye over 11 years ago
Hi,
I tried both the approaches:
- Using WServer's post method along with triggerUpdate()
- acquiring update lock access.
It is possible that I have not done it correctly.
With first approach, application is still crashing. From core dump, following information is retrieved.
#0 0x00007f0ef3cb4370 in Wt::WApplication::triggerUpdate (this=0x7f0ed8001dd0) at /home/rajeev/Downloads/Softwares/Wt/wt-3.3.0/src/Wt/WApplication.C:1334
If I omit triggerUpdate(), the application doesn't crash. However UI doesn't get updated.
If I update WText by first acquiring lock and then calling method setText, the application is not crashing but UI is not getting updated.
May be because it is not inside event processing loop.
I am attaching related code.
Regards,
Rajeev.
RE: Receiving streaming data over socket within Wt Application - Added by Koen Deforche over 11 years ago
Hey Rajeev,
Please carefully review the broadcast and serverpush examples. The concepts of server push are well illustrated there.
This is how to do it:
- using WServer::post(), you need to post a function that contains:
<!-- -->
message_->setText(message);
trigerUpdate();
- using update lock, you need this:
<!-- -->
UpdateLock lock(this);
message_->setText(message);
trigerUpdate();
Regards,
koen
RE: Receiving streaming data over socket within Wt Application - Added by Rajeev Gajbhiye over 11 years ago
Hi Koen,
Thanks for the resolution. Its working fine now. I missed on that finer detail and should have actually figured out myself. The only problem now is that if I compile the program with -std=c+11 flag (with gcc 4.8.1), the program crashes immediately, i.e. without even opening the browser. Then I rebuilt Wt with c+11 flag enabled. Now the program works fine. But now this time if I compile my application without c+11 flag program crashes immediately. Core dump shows following.
Program terminated with signal 11, Segmentation fault.
#0 0x0000000000411b29 in boost::asio::detail::task_io_service::stop_all_threads(boost::asio::detail::scoped_lockboost::asio::detail::posix_mutex&) ()
However, I can live with this problem as I can always compile my application with the flag irrespective of whether c++11 features are used or not.
Attached is the final working program.
Regards,
Rajeev.
RE: Receiving streaming data over socket within Wt Application - Added by Koen Deforche over 11 years ago
Hey,
Indeed, depending on the compiler version the C+11 and C+03 compiled files are not compatible! This is something that is going to cause alot of frustration and we are still in the process of finding out how we should deal with this for installed wt versions (e.g. have libwt-11.so and libwt-03.so naming conventions).
Regards,
koen