I am trying to implement Websocket client using Cpprest sdk (Casablanca). I am successful in establishing the Web Socket connection and able to send/recieve messages. The response is received as an instance of websocket_incoming_message, which has a method, extract_string() whose definition is,
_ASYNCRTIMP pplx::task<std::string> web::websockets::client::websocket_incoming_message::extract_string ( ) const
Hence it returns, const string.
I am trying to assign this string to a function local string variable, so that I can return it to the calling method. However, I receive the below error,
error: passing ‘const string {aka const std::basic_string<char>}’ as ‘this’ argument of ‘std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ discards qualifiers [-fpermissive]
Below is my code block,
std::string WebSocketUtility::send_command(const char* command){
websocket_outgoing_message o_msg;
std::string ws_response;
o_msg.set_utf8_message(command);
client->send(o_msg).then([](){
log("Message Sent!!!\n");
});
client->receive().then([ws_response](websocket_incoming_message i_msg){
return i_msg.extract_string();
}).then([ws_response](std::string ws_body){
ws_response = ws_body;
});
log("WS_RESPONSE:%s:\n",ws_response.c_str());
return ws_response;
}
I have tried
1.Declaring ws_response as a reference
2.Capturing ws_response as a reference in lambda
3.Declaring ws_response as cpprest json, and assigning the response as a json field (ws_response[U("output")] = json::value::string(ws_body.c_str()))
with no luck.
I do not have much coding experience in C++ and struggling with this.
Could someone please help me on how to capture this response outside the lambda so that I can return the value to the calling method?
Thanks & Regards,
Swathi Desai