#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <cpprest/ws_client.h>
#include <cpprest/json.h>
#include <cpprest/asyncrt_utils.h>
#include <windows.h>
#include <opencv2/opencv.hpp>
using namespace web; // Common features like URIs.
using namespace web::websockets::client;
using namespace utility; // Common utilities like string conversions
using namespace utility::conversions;
using namespace concurrency::streams; // Asynchronous streams
int main(int argc, char* argv[])
{
//websocket_client client;
websocket_callback_client client;
try
{
t.get();
}
catch (const websocket_exception& ex)
{
std::cout << ex.what();
}});
Sleep(100);
std::cout << "sending message to rosbridge" << std::endl;
websocket_outgoing_message out_msg;
web::json::value out_obj;
out_obj[U("op")] = json::value::string(U("subscribe"));
out_obj[U("topic")] = json::value::string(U("/usb_cam/image_raw"));
out_obj[U("type")] = json::value::string(U("sensor_msgs/Image"));
/*obj[U("throttle_rate")] = json::value::number(10);
obj[U("queue_length")] = json::value::number(10);
obj[U("fragment_size")] = json::value::number(10);*/
out_obj[U("compression")] = json::value::string(U("png"));
out_msg.set_utf8_message(to_utf8string(out_obj.serialize()));
auto send_task = client.send(out_msg).then([](pplx::task<void> t){
try
{
t.get();
}
catch (const websocket_exception& ex)
{
std::cout << ex.what();
}});
client.send(out_msg).then([](pplx::task<void> t){
try
{
t.get();
}
catch (const websocket_exception& ex)
{
std::cout << ex.what();
}});
send_task.wait();
//////////////////////////////////////////////////////////////////////////
//// WebSocket Message Callback
//////////////////////////////////////////////////////////////////////////
client.set_message_handler([](websocket_incoming_message msg)
{
std::cout << "receiving message to rosbridge" << std::endl;
json::value obj = json::value::parse(conversions::to_string_t(msg.extract_string().get()));
json::value imgValue = obj[U("data")];
json::value topicValue = obj[U("topic")];
std::vector<unsigned char>imgdata = conversions::from_base64(imgValue.as_string());
cv::Mat buf_obj = cv::Mat(480, 640, CV_8U, imgdata.data());
cv::Mat img = imdecode(buf_obj, CV_LOAD_IMAGE_COLOR);
cv::imshow("png", img);
cv::waitKey(1);
});
while (true)
{
// client.receive().then([](websocket_incoming_message msg) {
// return msg.extract_string();
// }).then([](std::string body) {
// std::cout << body << std::endl;
// });
}
}