forked from ithewei/libhv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket_client_test.cpp
118 lines (101 loc) · 3.17 KB
/
websocket_client_test.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* websocket client
*
* @build make examples
* @server bin/websocket_server_test 8888
* @client bin/websocket_client_test ws://127.0.0.1:8888/test
* @clients bin/websocket_client_test ws://127.0.0.1:8888/test 100
* @python scripts/websocket_server.py
* @js html/websocket_client.html
*
*/
#include "WebSocketClient.h"
using namespace hv;
class MyWebSocketClient : public WebSocketClient {
public:
MyWebSocketClient(EventLoopPtr loop = NULL) : WebSocketClient(loop) {}
~MyWebSocketClient() {}
int connect(const char* url) {
// set callbacks
onopen = [this]() {
const HttpResponsePtr& resp = getHttpResponse();
printf("onopen\n%s\n", resp->body.c_str());
// printf("response:\n%s\n", resp->Dump(true, true).c_str());
};
onmessage = [this](const std::string& msg) {
printf("onmessage(type=%s len=%d): %.*s\n", opcode() == WS_OPCODE_TEXT ? "text" : "binary",
(int)msg.size(), (int)msg.size(), msg.data());
};
onclose = []() {
printf("onclose\n");
};
// ping
setPingInterval(10000);
// reconnect: 1,2,4,8,10,10,10...
reconn_setting_t reconn;
reconn_setting_init(&reconn);
reconn.min_delay = 1000;
reconn.max_delay = 10000;
reconn.delay_policy = 2;
setReconnect(&reconn);
/*
auto req = std::make_shared<HttpRequest>();
req->method = HTTP_POST;
req->headers["Origin"] = "http://example.com";
req->json["app_id"] = "123456";
req->json["app_secret"] = "abcdefg";
printf("request:\n%s\n", req->Dump(true, true).c_str());
setHttpRequest(req);
*/
http_headers headers;
headers["Origin"] = "http://example.com/";
return open(url, headers);
};
};
typedef std::shared_ptr<MyWebSocketClient> MyWebSocketClientPtr;
int TestMultiClientsRunInOneEventLoop(const char* url, int nclients) {
auto loop_thread = std::make_shared<EventLoopThread>();
loop_thread->start();
std::map<int, MyWebSocketClientPtr> clients;
for (int i = 0; i < nclients; ++i) {
MyWebSocketClient* client = new MyWebSocketClient(loop_thread->loop());
client->connect(url);
clients[i] = MyWebSocketClientPtr(client);
}
// press Enter to stop
while (getchar() != '\n');
loop_thread->stop();
loop_thread->join();
return 0;
}
int main(int argc, char** argv) {
if (argc < 2) {
printf("Usage: %s url\n", argv[0]);
return -10;
}
const char* url = argv[1];
int nclients = 0;
if (argc > 2) {
nclients = atoi(argv[2]);
}
if (nclients > 0) {
return TestMultiClientsRunInOneEventLoop(url, nclients);
}
MyWebSocketClient ws;
ws.connect(url);
std::string str;
while (std::getline(std::cin, str)) {
if (str == "close") {
ws.close();
} else if (str == "open") {
ws.connect(url);
} else if (str == "stop") {
ws.stop();
break;
} else {
if (!ws.isConnected()) break;
ws.send(str);
}
}
return 0;
}