Fix fuzzing build, make a more interesting example of pubsub
This commit is contained in:
@@ -3,51 +3,73 @@
|
|||||||
struct us_listen_socket_t *global_listen_socket;
|
struct us_listen_socket_t *global_listen_socket;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
/* ws->getUserData returns one of these */
|
/* ws->getUserData returns one of these */
|
||||||
struct PerSocketData {
|
struct PerSocketData {
|
||||||
|
/* Fill with user data */
|
||||||
|
std::vector<std::string> topics;
|
||||||
|
int nr = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Very simple WebSocket broadcasting echo server */
|
/* Keep in mind that uWS::SSLApp({options}) is the same as uWS::App() when compiled without SSL support.
|
||||||
uWS::App().ws<PerSocketData>("/*", {
|
* You may swap to using uWS:App() if you don't need SSL */
|
||||||
|
uWS::SSLApp *app = new uWS::SSLApp({
|
||||||
|
/* There are example certificates in uWebSockets.js repo */
|
||||||
|
.key_file_name = "../misc/key.pem",
|
||||||
|
.cert_file_name = "../misc/cert.pem",
|
||||||
|
.passphrase = "1234"
|
||||||
|
});
|
||||||
|
|
||||||
|
app->ws<PerSocketData>("/*", {
|
||||||
/* Settings */
|
/* Settings */
|
||||||
.compression = uWS::DEDICATED_COMPRESSOR_3KB,
|
.compression = uWS::DISABLED,
|
||||||
.maxPayloadLength = 16 * 1024 * 1024,
|
.maxPayloadLength = 16 * 1024 * 1024,
|
||||||
.idleTimeout = 10,
|
.idleTimeout = 60,
|
||||||
.maxBackpressure = 1 * 1024 * 1024,
|
.maxBackpressure = 16 * 1024 * 1024,
|
||||||
|
.closeOnBackpressureLimit = false,
|
||||||
|
.resetIdleTimeoutOnSend = true,
|
||||||
|
.sendPingsAutomatically = false,
|
||||||
/* Handlers */
|
/* Handlers */
|
||||||
.upgrade = nullptr,
|
.upgrade = nullptr,
|
||||||
.open = [](auto *ws) {
|
.open = [](auto *ws) {
|
||||||
/* Let's make every connection subscribe to the "broadcast" topic */
|
/* Open event here, you may access ws->getUserData() which points to a PerSocketData struct */
|
||||||
ws->subscribe("broadcast");
|
|
||||||
},
|
|
||||||
.message = [](auto *ws, std::string_view message, uWS::OpCode opCode) {
|
|
||||||
/* Exit gracefully if we get a closedown message (ASAN debug) */
|
|
||||||
if (message == "closedown") {
|
|
||||||
/* Bye bye */
|
|
||||||
us_listen_socket_close(0, global_listen_socket);
|
|
||||||
ws->close();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Simply broadcast every single message we get */
|
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
|
||||||
ws->publish("broadcast", message, opCode, true);
|
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
std::string topic = std::to_string((uintptr_t)ws) + "-" + std::to_string(i);
|
||||||
|
perSocketData->topics.push_back(topic);
|
||||||
|
ws->subscribe(topic);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.message = [&app](auto *ws, std::string_view message, uWS::OpCode opCode) {
|
||||||
|
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
|
||||||
|
|
||||||
|
app->publish(perSocketData->topics[++perSocketData->nr % 100], message, opCode);
|
||||||
},
|
},
|
||||||
.drain = [](auto */*ws*/) {
|
.drain = [](auto */*ws*/) {
|
||||||
/* Check getBufferedAmount here */
|
/* Check ws->getBufferedAmount() here */
|
||||||
|
//std::cout << "drain" << std::endl;
|
||||||
},
|
},
|
||||||
.ping = [](auto */*ws*/, std::string_view) {
|
.ping = [](auto */*ws*/, std::string_view ) {
|
||||||
|
/* Not implemented yet */
|
||||||
},
|
},
|
||||||
.pong = [](auto */*ws*/, std::string_view) {
|
.pong = [](auto */*ws*/, std::string_view ) {
|
||||||
|
/* Not implemented yet */
|
||||||
},
|
},
|
||||||
.close = [](auto */*ws*/, int /*code*/, std::string_view /*message*/) {
|
.close = [](auto */*ws*/, int /*code*/, std::string_view /*message*/) {
|
||||||
/* We automatically unsubscribe from any topic here */
|
/* You may access ws->getUserData() here */
|
||||||
}
|
}
|
||||||
}).listen(9001, [](auto *listen_socket) {
|
}).listen(9001, [](auto *listen_s) {
|
||||||
global_listen_socket = listen_socket;
|
if (listen_s) {
|
||||||
if (listen_socket) {
|
|
||||||
std::cout << "Listening on port " << 9001 << std::endl;
|
std::cout << "Listening on port " << 9001 << std::endl;
|
||||||
|
//listen_socket = listen_s;
|
||||||
}
|
}
|
||||||
}).run();
|
});
|
||||||
|
|
||||||
|
app->run();
|
||||||
|
|
||||||
|
delete app;
|
||||||
|
|
||||||
|
uWS::Loop::get()->free();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ void test() {
|
|||||||
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
|
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
|
||||||
|
|
||||||
for (int i = 0; i < 100; i++) {
|
for (int i = 0; i < 100; i++) {
|
||||||
std::string topic = std::to_string(ws);
|
std::string topic = std::to_string((uintptr_t)ws) + "-" + std::to_string(i);
|
||||||
perSocketData->topics.push_back(topic);
|
perSocketData->topics.push_back(topic);
|
||||||
ws->subscribe(topic);
|
ws->subscribe(topic);
|
||||||
}
|
}
|
||||||
@@ -51,7 +51,7 @@ void test() {
|
|||||||
.message = [&app](auto *ws, std::string_view message, uWS::OpCode opCode) {
|
.message = [&app](auto *ws, std::string_view message, uWS::OpCode opCode) {
|
||||||
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
|
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
|
||||||
|
|
||||||
app->publish(perSocketData->topics[++nr % 100], message, opCode);
|
app->publish(perSocketData->topics[++perSocketData->nr % 100], message, opCode);
|
||||||
},
|
},
|
||||||
.drain = [](auto */*ws*/) {
|
.drain = [](auto */*ws*/) {
|
||||||
/* Check ws->getBufferedAmount() here */
|
/* Check ws->getBufferedAmount() here */
|
||||||
|
|||||||
Reference in New Issue
Block a user