-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathws.cpp
More file actions
65 lines (46 loc) · 1.21 KB
/
ws.cpp
File metadata and controls
65 lines (46 loc) · 1.21 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
// an example of using websockets
#include <cstdio>
#include "../lambda.hpp"
using namespace Lambda;
void handler_fn(Request& req, ResponseWriter& wrt) {
if (req.url.path != "/ws") {
wrt.write_header(Status::NotFound);
return;
}
auto ws = Websocket(req, wrt);
ws.write("Redy to 'sign' your messages");
while (ws.is_open()) {
auto next = ws.next();
if (!next.has_value()) {
continue;
}
auto msg = next.value();
switch (msg.code) {
case Ws::Opcode::Ping: {
puts("--> Pong");
ws.write(Frame {
.code = Ws::Opcode::Pong,
.data = msg.data
});
} break;
case Ws::Opcode::Text: {
auto signed_message = "Signed message: '" + std::string(msg.data.begin(), msg.data.end()) + "'; Signature: lambda";
ws.write(signed_message);
puts("--> 'Signed' client message");
} break;
case Ws::Opcode::Close: {
puts("--> See you later aligator!");
return;
} break;
default: {
puts("--> Received an unsupported (by the example app) message; Ignoring");
} break;
}
}
}
int main() {
auto server = Lambda::Server(handler_fn, { .debug = true });
printf("Listening at: http://localhost:%i/\n", server.options.port);
server.serve();
return 0;
}