如何从字符串创建一个web::uri以放入client.connect()
How can I create a web::uri from a string to put into client.connect()?
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;
}
快速浏览一下uri
的源代码就会发现有一个接受字符串参数的构造函数,但它不是一个简单明了的 C 类型字符串:
_ASYNCRTIMP uri(const utility::char_t *uri_string);
如果你在 windows 下构建它,你可能会发现 utility::char_t
实际上是wchar_t
,所以你可以在 URI 字符串前面加上 L 以将其标记为宽字符 unicode,如下所示:
uri link = uri(L"ws://echo.websocket.org");
我相信库提供了一个方便的跨平台字符串宏,U()
.查看常见问题解答。我认为它的工作原理有点像这样:
uri link = uri(U("ws://echo.websocket.org"));
相关文章:
- WinSock2:connect() 提供"连接被拒绝"
- 如何使用connect将qml按钮与同一类的cpp函数连接起来
- 在 QTextEdit 中使用指针或在 Qt-Creator 上使用 connect()
- gnuradio c++ connect self() throw bad_weak_ptr
- 如何将函数指针传递给方法,以便它可以在Qt的connect()中使用?
- 在CTOR vs Connect方法中提升套接字与stream_socket端点
- Make zmqpp::socket::connect a std::future
- C-Free and MySQL connect
- 当我使用Connect信号和插槽时,会出现此分解错误
- 为什么 connect() 函数看不到插槽?
- 如何确定2个URI(路径)是否属于同一分区或卷
- QT 库 QObject::connect: 无法连接 (null)::done() 到 MainWindow::upd
- QObject::connect 有太多的参数
- 可以在 QObject::connect() C++中连接 QML 对象现有信号?
- 使用QT Connect时,视觉工作室警告
- connect(QObject*, SIGNAL(signal()), functor) 在 qt5 中未连接
- SDL_Init失败并显示'SDL_Error: Failed to connect to the Mir Server'
- 如何从字符串创建一个web::uri以放入client.connect()
- 我可以调用 bind() 然后在同一个套接字描述符上调用 connect() 吗?
- 将QMETAMETHOD作为参数传递到使用新的Qobject :: Connect语法的函数