Add broadcasting example

This commit is contained in:
Alex Hultman
2019-08-11 19:04:56 +02:00
parent 057093ca5e
commit 1ddb5c0d1c
2 changed files with 42 additions and 1 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
EXAMPLE_FILES := HelloWorld EchoServer
EXAMPLE_FILES := HelloWorld EchoServer BroadcastingEchoServer
THREADED_EXAMPLE_FILES := HelloWorldThreaded EchoServerThreaded
override CXXFLAGS += -std=c++17 -Isrc -IuSockets/src
override LDFLAGS += uSockets/*.o -lz
+41
View File
@@ -0,0 +1,41 @@
#include "App.h"
int main() {
/* ws->getUserData returns one of these */
struct PerSocketData {
};
/* Very simple WebSocket broadcasting echo server */
uWS::App().ws<PerSocketData>("/*", {
/* Settings */
.compression = uWS::SHARED_COMPRESSOR,
.maxPayloadLength = 16 * 1024,
.idleTimeout = 10,
/* Handlers */
.open = [](auto *ws, auto *req) {
/* Let's make every connection subscribe to the "broadcast" topic */
ws->subscribe("broadcast");
},
.message = [](auto *ws, std::string_view message, uWS::OpCode opCode) {
/* Simply broadcast every single message we get */
ws->publish("broadcast", message/*, opCode*/);
},
.drain = [](auto *ws) {
/* Check getBufferedAmount here */
},
.ping = [](auto *ws) {
},
.pong = [](auto *ws) {
},
.close = [](auto *ws, int code, std::string_view message) {
/* We automatically unsubscribe from any topic here */
}
}).listen(9001, [](auto *token) {
if (token) {
std::cout << "Listening on port " << 9001 << std::endl;
}
}).run();
}