forked from zhllxt/asio2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket_server.cpp
More file actions
98 lines (76 loc) · 2.4 KB
/
websocket_server.cpp
File metadata and controls
98 lines (76 loc) · 2.4 KB
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
#include <asio2/http/ws_server.hpp>
int main()
{
#if defined(WIN32) || defined(_WIN32) || defined(_WIN64) || defined(_WINDOWS_)
// Detected memory leaks on windows system
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif
std::string_view host = "0.0.0.0";
std::string_view port = "8039";
std::srand((unsigned int)time(nullptr));
for(;;)
{
asio2::ws_server server;
server.bind_accept([&server](std::shared_ptr<asio2::ws_session>& session_ptr)
{
// how to set custom websocket response data :
session_ptr->ws_stream().set_option(websocket::stream_base::decorator(
[](websocket::response_type& rep)
{
rep.set(http::field::authorization, " websocket-server-coro");
}));
server.post_event([]()
{
//printf("def\n");
});
session_ptr->post_event([]()
{
//printf("xyz\n");
});
}).bind_recv([&server](auto & session_ptr, std::string_view s)
{
asio2::detail::ignore_unused(server, session_ptr);
//printf("recv : %u %.*s\n", (unsigned)s.size(), (int)s.size(), s.data());
session_ptr->async_send(std::string(s), [](std::size_t) {});
}).bind_connect([](auto & session_ptr)
{
printf("client enter : %s %u %s %u\n",
session_ptr->remote_address().c_str(), session_ptr->remote_port(),
session_ptr->local_address().c_str(), session_ptr->local_port());
}).bind_disconnect([](auto & session_ptr)
{
printf("client leave : %s %u %s\n", session_ptr->remote_address().c_str(),
session_ptr->remote_port(), asio2::last_error_msg().c_str());
}).bind_upgrade([](auto & session_ptr, asio::error_code ec)
{
asio2::detail::ignore_unused(session_ptr);
printf(">> upgrade %d %s\n", ec.value(), ec.message().c_str());
}).bind_start([&](asio::error_code ec)
{
printf("start websocket server : %s %u %d %s\n",
server.listen_address().c_str(), server.listen_port(),
ec.value(), ec.message().c_str());
}).bind_stop([&](asio::error_code ec)
{
printf("stop : %d %s\n", ec.value(), ec.message().c_str());
});
server.post_event([]()
{
//printf("abc\n");
});
server.start(host, port);
server.post_event([]()
{
//printf("abc\n");
});
server.start(host, port);
auto sec = 1 + std::rand() % 2;
printf("enter %d\n", sec);
std::this_thread::sleep_for(std::chrono::seconds(sec));
printf("leave %d\n", sec);
server.start(host, port);
server.stop();
}
while (std::getchar() != '\n');
return 0;
}