C++ 如何从字符串创建web::uri以放入client.connect()中?
需要C++ 如何从字符串创建web::uri以放入client.connect()中?,c++,websocket,cpprest-sdk,C++,Websocket,Cpprest Sdk,需要client.connect(web::uri),但在查看web::uri后,它将不接受字符串。api似乎说它可以接受字符串,但它不会,我也不明白为什么 #include <iostream> #include <stdio.h> #include <cpprest/ws_client.h> using namespace std; using namespace web; using namespace web::websockets::client;
client.connect(web::uri)
,但在查看web::uri
后,它将不接受字符串。api似乎说它可以接受字符串,但它不会,我也不明白为什么
#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;
}
#包括
#包括
#包括
使用名称空间std;
使用命名空间web;
使用名称空间web::websockets::client;
int main(int argc,char**args){
uri link=uri(“ws://echo.websocket.org”);//问题行
websocket_客户端;
client.connect(link.wait();
websocket\u传出消息\u消息;
输出消息设置utf8消息(“测试”);
client.send(out_msg.wait();
client.receive()。然后([](websocket\u消息中的传入消息){
返回_msg.extract_string();
})。然后([](字符串体){
cout快速浏览一下uri
的函数,可以发现有一个接受字符串参数的构造函数,但它不是简单的C类型字符串:
_ASYNCRTIMP uri(const utility::char_t *uri_string);
如果您是在windows下构建的,您可能会发现实用工具::char\u t
实际上是wchar\u t
,因此您可以在URI字符串前面加一个L,将其标记为宽字符unicode,如下所示:
uri link = uri(L"ws://echo.websocket.org");
uri link = uri(U("ws://echo.websocket.org"));
我相信库提供了一个方便的跨平台字符串宏,U()
。请看一下。我假设它的工作原理有点像这样:
uri link = uri(L"ws://echo.websocket.org");
uri link = uri(U("ws://echo.websocket.org"));
您会收到什么错误消息?它需要一个实用工具::char\u t*
而不是一个c字符串。如果您的问题得到满意的回答,您最好将答案标记为“已接受”,如果您觉得它特别有用,请向上投票。谢谢!