-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathchat_server.cpp
244 lines (228 loc) · 8.36 KB
/
chat_server.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <co_async/co_async.hpp>
#include <co_async/std.hpp>
using namespace co_async;
using namespace std::literals;
static auto index_html = R"html(<!DOCTYPE html>
<html>
<head>
<title>小彭老师在线聊天室 2.0</title>
<meta charset="UTF-8">
<style>
.message p {
margin: 3px;
}
div.message {
margin: 0px;
padding: 6px;
}
div.input-bar {
display: flex;
flex-direction: row;
margin-top: 10px;
}
.input-user {
flex: 1;
}
.input-content {
flex: 1;
}
.message-user {
color: #666666;
font-weight: 600;
}
.message-user.message-current-user {
color: #559966;
font-weight: 600;
}
.message-content {
color: #333333;
font-weight: 300;
}
.brief {
color: #444444;
font-weight: 300;
}
body {
display: flex;
justify-content: center;
}
#container {
max-width: 800px;
}
</style>
</head>
<body>
<div id="container">
<h1>小彭老师在线聊天室 2.0</h1>
<p class="brief">小彭老师自主研发的一款基于 WebSocket 的现代 C++ 全栈聊天服务器 | <a href="https://github.com/archibate/co_async">GitHub 仓库</a></p>
<div class="message-list" id="messages"></div>
<div class="input-bar">
<input class="input-user" type="text" id="user" placeholder="你的昵称"/>
<button class="login-button" id="login">登录</button>
<label class="message-user" hidden id="user-label" for="content"></label> <input hidden class="input-content" type="text" id="content" placeholder="输入你的消息..." autocomplete="off"/>
<button hidden class="send-button" id="send">发送</button>
</div>
</div>
<script src="https://unpkg.com/[email protected]/dist/jquery.min.js"></script>
<script>
var current_user = $("#user").val().trim();
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
$(document).ready(function() {
var ws = new WebSocket("ws://127.0.0.1:8080/");
$("#login").click(function() {
var user = $("#user").val().trim();
if (user == '') {
alert("昵称不能为空");
return;
}
ws.send(JSON.stringify({type: 'LoginParams', object: {user}}));
$("#send").show();
$("#content").show();
$("#user-label").show();
$("#login").hide();
$("#user").hide();
$("#user-label").text(`${escapeHtml(user)}:`);
});
$("#send").click(function() {
var content = $("#content").val().trim();
if (content == '') {
alert("消息不能为空");
return;
}
$("#content").val('');
ws.send(JSON.stringify({type: 'SendParams', object: {content}}));
});
$("#content").keypress(function(event) {
if (event.keyCode === 13) {
$("#send").click();
}
});
ws.onerror = function (evt) {
console.log('发生错误:', evt);
$("#messages").append('<div class="message"><p class="message-user">系统提示:</p><p class="message-content">连接发生了错误<p></div>');
};
ws.onmessage = function (evt) {
console.log('收到消息:', evt.data);
var recvParams = JSON.parse(evt.data);
var message = recvParams.message;
var extra_class = '';
var user = $("#user").val().trim();
if (message.user == user)
extra_class = ' message-current-user';
$("#messages").append(`<div class="message"><p class="message-user${extra_class}">${escapeHtml(message.user)}:</p><p class="message-content">${escapeHtml(message.content)}<p></div>`);
}
ws.onclose = function (evt) {
console.log('关闭了:', evt);
$("#messages").append('<div class="message"><p class="message-user">系统提示:</p><p class="message-content">连接已关闭,请刷新重新登录<p></div>');
};
});
</script>
</body>
</html>)html"sv;
struct Message {
std::string user;
std::string content;
REFLECT(user, content);
};
struct SendParams {
std::string content;
REFLECT(content);
static constexpr const char *name = "SendParams";
};
struct RecvParams {
Message message;
REFLECT(message);
static constexpr const char *name = "RecvParams";
};
struct LoginParams {
std::string user;
REFLECT(user);
static constexpr const char *name = "LoginParams";
};
struct Room {
std::vector<Message> mMessages;
ConditionVariable mMessageUpdated;
};
static Room room;
static Task<Expected<>> roomPoller(WebSocket &ws) {
size_t lastSize = 0;
while (true) {
while (room.mMessages.size() <= lastSize) {
co_await co_await room.mMessageUpdated.wait();
}
for (size_t i = lastSize; i < room.mMessages.size(); ++i) {
if (ws.is_closing())
break;
RecvParams recvParams = {
.message = room.mMessages[i],
};
co_await co_await ws.send(json_encode(std::move(recvParams)));
}
lastSize = room.mMessages.size();
}
co_return {};
}
static Task<Expected<>> amain(std::string addr) {
co_await co_await stdio().putline("正在监听: "s + addr);
auto listener = co_await co_await listener_bind(co_await AddressResolver()
.host(addr).resolve_one());
HTTPServer server;
server.route("GET", "/", [](HTTPServer::IO &io) -> Task<Expected<>> {
if (auto ws = co_await websocket_server(io)) {
// 是升级的 ws:// 请求
co_await co_await stdio().putline("连接成功"sv);
std::string loginUser;
ws->on_message([&] (std::string const &data) -> Task<Expected<>> {
co_await co_await stdio().putline("收到消息: "s + data);
std::visit(overloaded{
[&] (SendParams &&sendParams) {
room.mMessages.push_back(Message{
.user = loginUser,
.content = std::move(sendParams.content),
});
room.mMessageUpdated.notify_all();
},
[&] (LoginParams &&loginParams) {
loginUser = loginParams.user;
},
}, co_await json_decode<std::variant<LoginParams, SendParams>>(data));
co_return {};
});
ws->on_close([&] () -> Task<Expected<>> {
co_await co_await stdio().putline("正在关闭连接"sv);
co_return {};
});
ws->on_pong([&] (std::chrono::steady_clock::duration dt) -> Task<Expected<>> {
co_await co_await stdio().putline("网络延迟: "s + to_string(
std::chrono::duration_cast<std::chrono::milliseconds>(dt).count()) + "ms"s);
co_return {};
});
co_await (co_await when_any_common(ws->start(), roomPoller(*ws))).value;
co_return {};
} else {
// 是普通 http:// 请求
co_await co_await HTTPServerUtils::make_ok_response(io, index_html);
co_return {};
}
});
while (true) {
if (auto income = co_await listener_accept(listener)) {
co_spawn(server.handle_http(std::move(*income)));
}
}
}
int main(int argc, char **argv) {
std::string addr = "http://127.0.0.1:8080";
if (argc > 1) {
addr = argv[1];
}
co_main(amain(addr));
return 0;
}