From 59eadc025af107d42669899b3e9ad80f3dccc787 Mon Sep 17 00:00:00 2001 From: Alex Hultman <3397140+alexhultman@users.noreply.github.com> Date: Wed, 29 Aug 2018 00:12:25 +0200 Subject: [PATCH] Update README.md --- README.md | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 6626cfb..1134d6c 100644 --- a/README.md +++ b/README.md @@ -9,35 +9,33 @@ #### Express yourself briefly. ```c++ -uWS::App a; // or uWS::SSLApp(options) for SSL +uWS::App app; // or uWS::SSLApp(options) for SSL -a.onGet("/", [](auto *s, auto *req, auto *args) { +app.onGet("/", [](auto *res, auto *req, auto *args) { - s->writeStatus("200 OK")->writeHeader("Hello", "World")->end(buffer); + /* Respond with the web app on default route */ + res->writeStatus("200 OK") + ->writeHeader("Content-Type", "text/html; charset=utf-8") + ->end(indexHtmlBuffer); -}).onGet("/largefile", [](auto *s, auto *req, auto *args) { +}).onWebSocket("/ws/chat", [&](auto *ws, auto *req, auto *args) { - s->write([](int offset) { - return largeFile.substr(offset); - }, fileSize); + /* Subscribe to topic /chat */ + ws->subscribe("/chat"); -}).onPost("/upload/:filename", [](auto *s, auto *req, auto *args) { +}).onMessage([&](auto *ws, auto message, auto opCode) { - s->read([std::ofstream fout = args[0]](int offset, auto chunk) { - fout << chunk; - }); + /* Parse incoming message according to some protocol & publish it */ + if (seemsReasonable(message)) { + ws->publish("/chat", message); + } else { + ws->close(); + } -}).onWebSocket("/wsApi", [](auto *ws, auto *req, auto *args) { +}).onClose([&](auto *ws, int code, auto message) { - std::cout << "WebSocket connected to /wsApi" << std::endl; - -}).onMessage([](auto *ws, auto message, auto opCode) { - - ws->send(message, opCode); - -}).onClose([](auto *ws, int code, auto message) { - - std::cout << "WebSocket disconnected from /wsApi" << std::endl; + /* Remove websocket from this topic */ + ws->unsubscribe("/chat"); }).listen("localhost", 3000, 0); ```