web socket client

Dec 17, 2014 at 3:34 AM
I want to receive data from a web socket server in C++. Its address is "ws://localhost:6002/pullRank". No need for input arguments. The receive message is text. But so much error takes place in ws_msg.h, which troubles me a lot.
The code is as follow:
#include <cpprest/ws_client.h>
using namespace web;
using namespace web::websockets::client;

int main(int argc, char* argv[])
{
    websocket_client client;
    client.connect(U("ws://localhost:1234")).then([](){ /* We've finished connecting. */ });

    websocket_outgoing_message msg;
    msg.set_utf8_message("I am a UTF-8 string! (Or close enough...)");
    client.send(msg).then([](){ /* Successfully sent the message. */ });

    client.receive().then([](websocket_incoming_message msg) {
        return msg.extract_string();
    }).then([](std::string body) {
        std::cout << body << std::endl;
    });

    client.close().then([](){ /* Successfully closed the connection. */ });
}
Dec 17, 2014 at 3:35 AM
And the error is as follow:
``` Error 56 error C1506: unrecoverable block scoping error e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 203 1 2048
Error 52 error C2027: use of undefined type 'Concurrency::streams::basic_istream<_CharType>' e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 200 1 2048
Error 15 error C2039: 'container_buffer' : is not a member of 'Concurrency::streams' e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 159 1 2048
Error 22 error C2039: 'container_buffer' : is not a member of 'Concurrency::streams' e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 86 1 2048
Error 25 error C2039: 'container_buffer' : is not a member of 'Concurrency::streams' e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 95 1 2048
Error 45 error C2039: 'container_buffer' : is not a member of 'Concurrency::streams' e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 240 1 2048
Error 1 error C2039: 'istream' : is not a member of 'Concurrency::streams' e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 103 1 2048
Error 4 error C2039: 'istream' : is not a member of 'Concurrency::streams' e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 113 1 2048
Error 8 error C2039: 'istream' : is not a member of 'Concurrency::streams' e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 123 1 2048
Error 11 error C2039: 'istream' : is not a member of 'Concurrency::streams' e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 133 1 2048
Error 18 error C2039: 'istream' : is not a member of 'Concurrency::streams' e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 166 1 2048
Error 40 error C2039: 'istream' : is not a member of 'Concurrency::streams' e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 196 1 2048
Error 49 error C2039: 'istream' : is not a member of 'Concurrency::streams' e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 198 1 2048
Error 50 error C2061: syntax error : identifier 'istream' e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 199 1 2048
Error 34 error C2065: 'buffer' : undeclared identifier e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 162 1 2048
Error 36 error C2065: 'buffer' : undeclared identifier e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 163 1 2048
Error 23 error C2065: 'container_buffer' : undeclared identifier e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 86 1 2048
Error 26 error C2065: 'container_buffer' : undeclared identifier e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 95 1 2048
Error 29 error C2065: 'len' : undeclared identifier e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 115 1 2048
Error 31 error C2065: 'len' : undeclared identifier e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 125 1 2048
Error 38 error C2065: 'len' : undeclared identifier e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 169 1 2048
Error 55 error C2065: 'm_body' : undeclared identifier e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 202 1 2048
Error 37 error C2065: 'msg_type' : undeclared identifier e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 168 1 2048
Error 3 error C2143: syntax error : missing ',' before '&' e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 103 1 2048
Error 6 error C2143: syntax error : missing ',' before '&' e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 113 1 2048
Error 10 error C2143: syntax error : missing ',' before '&' e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 123 1 2048
Error 13 error C2143: syntax error : missing ',' before '&' e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 133 1 2048
Error 20 error C2143: syntax error : missing ',' before '&' e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 166 1 2048
Error 17 error C2143: syntax error : missing ',' before '<' e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 159 1 2048
Error 51 error C2143: syntax error : missing ';' before '{' e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 199 1 2048
Error 46 error C2143: syntax error : missing ';' before '<' e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 240 1 2048
Error 41 error C2146: syntax error : missing ';' before identifier 'body' e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 196 1 2048
Error 35 error C2228: left of '.size' must have class/struct/union e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 162 1 2048
Error 39 error C2228: left of '.streambuf' must have class/struct/union e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 170 1 2048
Error 48 error C2238: unexpected token(s) preceding ';' e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 240 1 2048
Error 24 error C2275: 'std::string' : illegal use of this type as an expression e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 86 1 2048
Error 27 error C2275: 'std::string' : illegal use of this type as an expression e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 95 1 2048
Error 53 error C2440: 'return' : cannot convert from 'Concurrency::streams::basic_istream<_CharType>' to 'int' e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 200 1 2048
Error 14 error C2535: 'void web::websockets::client::websocket_outgoing_message::set_binary_message(const int)' : member function already defined or declared e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 134 1 2048
Error 21 error C2535: 'void web::websockets::client::websocket_outgoing_message::set_message(const int)' : member function already defined or declared e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 167 1 2048
Error 7 error C2535: 'void web::websockets::client::websocket_outgoing_message::set_utf8_message(const int)' : member function already defined or declared e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 114 1 2048
Error 28 error C2660: 'web::websockets::client::websocket_outgoing_message::set_message' : function does not take 3 arguments e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 105 1 2048
Error 30 error C2660: 'web::websockets::client::websocket_outgoing_message::set_message' : function does not take 3 arguments e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 115 1 2048
Error 32 error C2660: 'web::websockets::client::websocket_outgoing_message::set_message' : function does not take 3 arguments e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 125 1 2048
Error 33 error C2660: 'web::websockets::client::websocket_outgoing_message::set_message' : function does not take 3 arguments e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 135 1 2048
Error 54 error C3536: 'to_uint8_t_stream': cannot be used before it is initialized e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 202 1 2048
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 103 1 2048
Error 5 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 113 1 2048
Error 9 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 123 1 2048
Error 12 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 133 1 2048
Error 16 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 159 1 2048
Error 19 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int e:\visual studio 2013\final\2048\packages\cpprestsdk.2.3.0\build\native\include\cpprest\ws_msg.h 166 1 2048
Error 42 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int e:\visual studi
Dec 17, 2014 at 5:37 PM
Hi jinkuncai,

We actually have a bug in the last release where a header file include is missing and can cause compilation errors, like the ones you're seeing, if you only include ws_client.h. This has already been fixed in the development branch and will be in the next release, 2.4.0. The problem is really easy to work around. Add the following includes to your sources before including <cpprest/ws_client.h>:
#include <cpprest/streams.h>
#include <cpprest/containerstream.h>
This will get your program compiling. However I also noticed in your code that you are never waiting on any of the asynchronous operations before exiting your main function. This means your program will not run as expected since you will be exiting the process with asynchronous tasks still executing. What you need to do is setup some signaling to wait for when to exit or you need to call wait()/get() on each of the tasks. If you are unfamiliar about programming with tasks I recommend you take a look at this msdn article.

Steve