-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.cpp
More file actions
40 lines (32 loc) · 1.19 KB
/
Copy pathhttp.cpp
File metadata and controls
40 lines (32 loc) · 1.19 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
#include <print>
#include "HttpServer.hpp"
HttpServer g_Server{ true, HttpVersion::HTTP_1_1 };
int main(int argc, char* argv[])
{
g_Server.use("/", HttpMethod::GET,
[](const HttpRequest& request, HttpResponse& response, const NextFn& next) {
response.setHeader("Content-Type", "text/plain");
response.send("Hello, world!");
}
);
g_Server.websocket("/ws", {
.onRequest = [](const HttpRequest& request, HttpResponse& response) {
std::println("New request!");
},
.onOpen = [](const WebSocket& ws) {
std::println("New WebSocket connection");
ws.send("Hello, world!");
ws.send(std::vector<uint8_t>{ 0x01, 0x02, 0x03 });
},
.onText = [](const WebSocket& ws, const std::string& msg) {
std::println("Received message: '{}'", msg);
},
.onBinary = [](const WebSocket& ws, std::span<const uint8_t> msg) {
std::println("Received binary message!");
},
.onClose = [](const WebSocket& ws, uint16_t code, const std::string& reason) {
std::println("WebSocket closed");
},
});
g_Server.listen(8000);
};