代码拉取完成,页面将自动刷新
// Compile with:
// g++ -std=gnu++0x example-client-cpp11.cpp -o example-client-cpp11
#include "easywsclient.hpp"
//#include "easywsclient.cpp" // <-- include only if you don't want compile separately
#ifdef _WIN32
#pragma comment( lib, "ws2_32" )
#include <WinSock2.h>
#endif
#include <assert.h>
#include <stdio.h>
#include <string>
#include <memory>
int main()
{
using easywsclient::WebSocket;
#ifdef _WIN32
INT rc;
WSADATA wsaData;
rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (rc) {
printf("WSAStartup Failed.\n");
return 1;
}
#endif
std::unique_ptr<WebSocket> ws(WebSocket::from_url("ws://localhost:8126/foo"));
assert(ws);
ws->send("goodbye");
ws->send("hello");
while (ws->getReadyState() != WebSocket::CLOSED) {
WebSocket::pointer wsp = &*ws; // <-- because a unique_ptr cannot be copied into a lambda
ws->poll();
ws->dispatch([wsp](const std::string & message) {
printf(">>> %s\n", message.c_str());
if (message == "world") { wsp->close(); }
});
}
#ifdef _WIN32
WSACleanup();
#endif
// N.B. - unique_ptr will free the WebSocket instance upon return:
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。