2

A client.connect(web::uri) is required, but after looking into web::uri it won't accept a string. The api seems to say it can accept a string, but it won't and I can't figure out why.

#include <iostream>
#include <stdio.h>
#include <cpprest/ws_client.h>

using namespace std;
using namespace web;
using namespace web::websockets::client;

int main(int argc, char **args) {

    uri link = uri("ws://echo.websocket.org"); //PROBLEM LINE
    websocket_client client;    
    client.connect(link).wait(); 

    websocket_outgoing_message out_msg;
    out_msg.set_utf8_message("test");
    client.send(out_msg).wait();

    client.receive().then([](websocket_incoming_message in_msg) {
        return in_msg.extract_string();
    }).then([](string body) {
        cout << body << endl;
    }).wait();

    client.close().wait();

    return 0;
}
Thomas Kühn
  • 9,412
  • 3
  • 47
  • 63

1 Answers1

4

A quick peek at the source of uri shows there's a constructor that takes a string argument, but it isn't a plain and simple C-type string:

_ASYNCRTIMP uri(const utility::char_t *uri_string);

If you're building this under windows, you may find that utility::char_t is in fact wchar_t, so you could prefix your URI string with an L to mark it as wide-character unicode, like this:

uri link = uri(L"ws://echo.websocket.org");

I believe there's a convenience cross-platform string macro provided by the library, U(). Have a look at the FAQ. I assume it works a bit like this:

uri link = uri(U("ws://echo.websocket.org"));
Rook
  • 5,734
  • 3
  • 34
  • 43