forked from ithewei/libhv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket_server_test.cpp
115 lines (102 loc) · 3.07 KB
/
websocket_server_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
/*
* websocket server
*
* @build make examples
* @server bin/websocket_server_test 9999
* @client bin/websocket_client_test ws://127.0.0.1:9999/
* @python scripts/websocket_server.py
* @js html/websocket_client.html
*
*/
#include "WebSocketServer.h"
#include "EventLoop.h"
#include "htime.h"
using namespace hv;
/*
* #define TEST_WSS 1
*
* @build ./configure --with-openssl && make clean && make
*
* @server bin/websocket_server_test 9999
*
* @client bin/websocket_client_test ws://127.0.0.1:9999/
* bin/websocket_client_test wss://127.0.0.1:10000/
*
*/
#define TEST_WSS 0
using namespace hv;
class MyContext {
public:
MyContext() {
printf("MyContext::MyContext()\n");
timerID = INVALID_TIMER_ID;
}
~MyContext() {
printf("MyContext::~MyContext()\n");
}
int handleMessage(const std::string& msg, enum ws_opcode opcode) {
printf("onmessage(type=%s len=%d): %.*s\n", opcode == WS_OPCODE_TEXT ? "text" : "binary",
(int)msg.size(), (int)msg.size(), msg.data());
return msg.size();
}
TimerID timerID;
};
int main(int argc, char** argv) {
if (argc < 2) {
printf("Usage: %s port\n", argv[0]);
return -10;
}
int port = atoi(argv[1]);
HttpService http;
http.GET("/ping", [](const HttpContextPtr& ctx) {
return ctx->send("pong");
});
WebSocketService ws;
// ws.setPingInterval(10000);
ws.onopen = [](const WebSocketChannelPtr& channel, const HttpRequestPtr& req) {
printf("onopen: GET %s\n", req->Path().c_str());
auto ctx = channel->newContextPtr<MyContext>();
// send(time) every 1s
ctx->timerID = setInterval(1000, [channel](TimerID id) {
if (channel->isConnected() && channel->isWriteComplete()) {
char str[DATETIME_FMT_BUFLEN] = {0};
datetime_t dt = datetime_now();
datetime_fmt(&dt, str);
channel->send(str);
}
});
};
ws.onmessage = [](const WebSocketChannelPtr& channel, const std::string& msg) {
auto ctx = channel->getContextPtr<MyContext>();
ctx->handleMessage(msg, channel->opcode);
};
ws.onclose = [](const WebSocketChannelPtr& channel) {
printf("onclose\n");
auto ctx = channel->getContextPtr<MyContext>();
if (ctx->timerID != INVALID_TIMER_ID) {
killTimer(ctx->timerID);
ctx->timerID = INVALID_TIMER_ID;
}
// channel->deleteContextPtr();
};
WebSocketServer server;
server.port = port;
#if TEST_WSS
server.https_port = port + 1;
hssl_ctx_opt_t param;
memset(¶m, 0, sizeof(param));
param.crt_file = "cert/server.crt";
param.key_file = "cert/server.key";
param.endpoint = HSSL_SERVER;
if (server.newSslCtx(¶m) != 0) {
fprintf(stderr, "new SSL_CTX failed!\n");
return -20;
}
#endif
server.registerHttpService(&http);
server.registerWebSocketService(&ws);
server.start();
// press Enter to stop
while (getchar() != '\n');
return 0;
}