59 lines
2.1 KiB
C++
59 lines
2.1 KiB
C++
/* We simply call the root header file "App.h", giving you uWS::App and uWS::SSLApp */
|
|
#include "App.h"
|
|
|
|
/* This is a simple WebSocket echo server example.
|
|
* You may compile it with "WITH_OPENSSL=1 make" or with "make" */
|
|
|
|
int main() {
|
|
/* ws->getUserData returns one of these */
|
|
struct PerSocketData {
|
|
/* Fill with user data */
|
|
int something;
|
|
};
|
|
|
|
/* Keep in mind that uWS::SSLApp({options}) is the same as uWS::App() when compiled without SSL support.
|
|
* You may swap to using uWS:App() if you don't need SSL */
|
|
uWS::SSLApp({
|
|
/* There are example certificates in uWebSockets.js repo */
|
|
.key_file_name = "../misc/key.pem",
|
|
.cert_file_name = "../misc/cert.pem",
|
|
.passphrase = "1234"
|
|
}).ws<PerSocketData>("/*", {
|
|
/* Settings */
|
|
.compression = uWS::SHARED_COMPRESSOR,
|
|
.maxPayloadLength = 16 * 1024,
|
|
.idleTimeout = 10,
|
|
.maxBackpressure = 1 * 1024 * 1204,
|
|
/* Handlers */
|
|
.upgrade = [](auto *res, atuo *req) {
|
|
std::cout << "Upgrade now!" << std::endl;
|
|
|
|
/* Pass user data from upgrade to open handler here */
|
|
//res.upgrade({.something = 15}, req);
|
|
},
|
|
.open = [](auto *ws, auto *req) {
|
|
/* Open event here, you may access ws->getUserData() which points to a PerSocketData struct */
|
|
std::cout << "Something is: " << ws->getUserData().something << std::endl;
|
|
},
|
|
.message = [](auto *ws, std::string_view message, uWS::OpCode opCode) {
|
|
ws->send(message, opCode);
|
|
},
|
|
.drain = [](auto *ws) {
|
|
/* Check ws->getBufferedAmount() here */
|
|
},
|
|
.ping = [](auto *ws) {
|
|
/* Not implemented yet */
|
|
},
|
|
.pong = [](auto *ws) {
|
|
/* Not implemented yet */
|
|
},
|
|
.close = [](auto *ws, int code, std::string_view message) {
|
|
/* You may access ws->getUserData() here */
|
|
}
|
|
}).listen(9001, [](auto *token) {
|
|
if (token) {
|
|
std::cout << "Listening on port " << 9001 << std::endl;
|
|
}
|
|
}).run();
|
|
}
|