-
Notifications
You must be signed in to change notification settings - Fork 0
/
DemoWebsocketServer.cc
64 lines (54 loc) · 1.87 KB
/
DemoWebsocketServer.cc
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
//
// Created by zaxtyson on 2022/3/22.
//
#include <logger/Logger.h>
#include <proto/ws/WebsocketServer.h>
using namespace jerry;
using namespace jerry::proto::ws;
class DemoWebsocketServer : public WebsocketServer {
public:
void OnConnected(jerry::net::TcpConn* conn, const jerry::DateTime& time) override {
WebsocketServer::OnConnected(conn, time);
}
void OnRequest(const WsReq& req,
WsResp& resp,
net::TcpConn* conn,
const DateTime& time) override {
LOG_INFO("[%s] [%zu] %s",
req.GetRequestUri().data(),
req.GetPayloadLength(),
req.GetPayload().data())
if (req.GetFrameType() == WsFrameType::kPing) {
// no pyload to send
resp.SetFrameType(WsFrameType::kPong);
} else {
// default frame type is `WsFrameType::kText`
resp.AppendData("Received: ");
resp.AppendData(req.GetPayload());
}
}
void OnDisConnected(jerry::net::TcpConn* conn, const jerry::DateTime& time) override {
WebsocketServer::OnDisConnected(conn, time);
}
};
int main() {
Logger::SetAppender(new logger::StderrAppender());
Logger::SetLogLevel(LogLevel::kDebug);
net::ServerConfig config;
config.acceptor.listen_ip = "127.0.0.1";
config.acceptor.listen_port = 8080;
config.workgroup.workers = 1;
config.threadpool.workers = 1;
config.threadpool.pending_size = 0;
// ssl config
config.ssl.use_ssl = true;
config.ssl.disable_old_ssl_version = true;
config.ssl.enable_validation = true;
config.ssl.cert_file = "./pem/cert.pem";
config.ssl.private_key_file = "./pem/key.pem";
DemoWebsocketServer server;
server.Config(config);
server.Serve();
// you can test websocket here:
// http://livepersoninc.github.io/ws-test-page/
}