Table of Contents
cpprestsdk WebSocket
MS 에서 만든 MIT 라이센스의 SDK 이다.
여러 기능이 있는데 그중 WebSocket 관련 라이브러리도 있다.
뭐… 더이상의 기능추가 없이 보안 이슈만 해결한다고 하는데…
WebSocket 은 더이상의 기능추가는 없을 듯 하니… 써도 괜찮을 듯 하다.
클라이언트 부분만 지원하고, 서버 부분은 라이브러리에서 제공하지 않는다.
다른 라이브러리를 찾아야 한다.
install
sudo apt install libcpprest-dev
source
vi client.cpp
#include <cpprest/ws_client.h>
using namespace web;
using namespace web::websockets::client;
int main() {
websocket_client client;
client.connect(U("ws://localhost:30001")).wait();
std::cout << "connect" << std::endl;
auto receivedTast = client.receive().then([](websocket_incoming_message ret_msg) {
log_info("websocket callback");
});
websocket_outgoing_message msg;
msg.set_ping_message("");
client.send(msg).wait();
msg.set_pong_message("");
client.send(msg).wait();
// websocket_outgoing_message msg;
for (int i = 0; i < 5; i++) {
msg.set_utf8_message("hello");
client.send(msg);
sleep(1);
}
std::cout << "send" << std::endl;
const char *filename = "./log.h";
std::ifstream ifs(filename);
std::istream_iterator<uint8_t> start(ifs), end;
std::vector<uint8_t> msgbuf(start, end);
auto is = streams::container_stream<std::vector<uint8_t>>::open_istream(std::move(msgbuf));
// websocket_outgoing_message msg;
msg.set_binary_message(is);
client.send(msg).wait();
client.close().wait();
std::cout << "close" << std::endl;
return 0;
}
g++ client.cpp -lpthread -lcpprest -lcrypto
./a.out
https://stackoverflow.com/questions/56884986/how-to-setup-websocket-ssl-connect-using-cpprestsdk