forked from yhirose/cpp-httplib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwsecho.cc
More file actions
135 lines (114 loc) · 3.61 KB
/
Copy pathwsecho.cc
File metadata and controls
135 lines (114 loc) · 3.61 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
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
#include <httplib.h>
#include <iostream>
using namespace httplib;
const auto html = R"HTML(
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>WebSocket Demo</title>
<style>
body { font-family: monospace; margin: 2em; }
#log { height: 300px; overflow-y: scroll; border: 1px solid #ccc; padding: 8px; }
.controls { margin: 8px 0; }
button { margin-right: 4px; }
</style>
</head>
<body>
<h1>WebSocket Demo</h1>
<p>Server accepts subprotocols: <b>echo</b>, <b>chat</b> (or none)</p>
<div class="controls">
<label>Subprotocols: </label>
<input id="protos" type="text" value="echo, chat" placeholder="leave empty for none" />
<button onclick="doConnect()">Connect</button>
<button onclick="doDisconnect()">Disconnect</button>
</div>
<div class="controls">
<input id="msg" type="text" placeholder="Type a message..." />
<button onclick="doSend()">Send</button>
</div>
<div class="controls">
<button onclick="startAuto()">Start Auto (1s)</button>
<button onclick="stopAuto()">Stop Auto</button>
<span id="auto-status"></span>
</div>
<pre id="log"></pre>
<script>
var sock = null;
var logEl = document.getElementById("log");
var statusEl = document.getElementById("auto-status");
var timer = null;
var seq = 0;
function appendLog(text) {
logEl.textContent += text + "\n";
logEl.scrollTop = logEl.scrollHeight;
}
function doConnect() {
if (sock && sock.readyState <= 1) { sock.close(); }
var input = document.getElementById("protos").value.trim();
var protocols = input ? input.split(/\s*,\s*/).filter(Boolean) : [];
sock = new WebSocket("ws://" + location.host + "/ws", protocols);
appendLog("[connecting] proposed: " + (protocols.length ? protocols.join(", ") : "(none)"));
sock.onopen = function() { appendLog("[connected] subprotocol: " + (sock.protocol || "(none)")); };
sock.onclose = function() { appendLog("[disconnected]"); stopAuto(); };
sock.onmessage = function(e) { appendLog("< " + e.data); };
}
function doDisconnect() {
if (sock) { sock.close(); }
}
function doSend() {
var input = document.getElementById("msg");
if (!sock || sock.readyState !== 1 || input.value === "") return;
sock.send(input.value);
appendLog("> " + input.value);
input.value = "";
}
function startAuto() {
if (timer || !sock || sock.readyState !== 1) return;
seq = 0;
statusEl.textContent = "running...";
timer = setInterval(function() {
if (!sock || sock.readyState !== 1) { stopAuto(); return; }
var msg = "auto #" + seq++;
sock.send(msg);
appendLog("> " + msg);
}, 1000);
}
function stopAuto() {
if (timer) { clearInterval(timer); timer = null; }
statusEl.textContent = "";
}
document.getElementById("msg").addEventListener("keydown", function(e) {
if (e.key === "Enter") doSend();
});
doConnect();
</script>
</body>
</html>
)HTML";
int main(void) {
Server svr;
svr.Get("/", [&](const Request & /*req*/, Response &res) {
res.set_content(html, "text/html");
});
svr.WebSocket(
"/ws",
[](const Request &req, ws::WebSocket &ws) {
std::cout << "WebSocket connected from " << req.remote_addr
<< std::endl;
std::string msg;
while (ws.read(msg)) {
std::cout << "Received: " << msg << std::endl;
ws.send("echo: " + msg);
}
std::cout << "WebSocket disconnected" << std::endl;
},
[](const std::vector<std::string> &protocols) -> std::string {
for (const auto &p : protocols) {
if (p == "echo" || p == "chat") { return p; }
}
return "";
});
std::cout << "Listening on http://localhost:8080" << std::endl;
svr.listen("localhost", 8080);
}