1 Star 0 Fork 0

jialh/websocket

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
example-client-cpp11.cpp 1.18 KB
一键复制 编辑 原始数据 按行查看 历史
core1011 提交于 2017-11-27 14:10 . first commit
// 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;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jialh7302/websocket.git
git@gitee.com:jialh7302/websocket.git
jialh7302
websocket
websocket
master

搜索帮助