forked from dhbaird/easywsclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-secure-client.cpp
46 lines (41 loc) · 961 Bytes
/
example-secure-client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#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>
using easywsclient::WebSocket;
static WebSocket::pointer ws = NULL;
void handle_message(const std::string & message)
{
printf(">>> %s\n", message.c_str());
if (message == "goodbye") { ws->close(); }
}
int main()
{
#ifdef _WIN32
INT rc;
WSADATA wsaData;
rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (rc) {
printf("WSAStartup Failed.\n");
return 1;
}
#endif
ws = WebSocket::from_url("wss://echo.websocket.org");
assert(ws);
ws->send("hello");
ws->send("goodbye");
while (ws->getReadyState() != WebSocket::CLOSED) {
ws->poll();
ws->dispatch(handle_message);
}
delete ws;
#ifdef _WIN32
WSACleanup();
#endif
return 0;
}