|
| 1 | +#include "App.h" |
| 2 | + |
| 3 | +int main() { |
| 4 | + /* ws->getUserData returns one of these */ |
| 5 | + struct PerSocketData { |
| 6 | + /* Fill with user data */ |
| 7 | + }; |
| 8 | + |
| 9 | + /* Keeping track of last precompressed message both in original and compressed format */ |
| 10 | + std::string originalMessage; |
| 11 | + std::string compressedMessage; |
| 12 | + std::mutex m; |
| 13 | + |
| 14 | + /* For demo, we create a thread that will update the precompressed message every second */ |
| 15 | + std::thread t2([&originalMessage, &compressedMessage, &m]() { |
| 16 | + uWS::ZlibContext zlibContext; |
| 17 | + uWS::DeflationStream compressor(uWS::DEDICATED_COMPRESSOR); |
| 18 | + int counter = 0; |
| 19 | + |
| 20 | + while (true) { |
| 21 | + counter++; |
| 22 | + |
| 23 | + m.lock(); |
| 24 | + originalMessage = "Hello you are looking at message number " + std::to_string(counter) + " and this text should be precompressed"; |
| 25 | + compressedMessage = compressor.deflate(&zlibContext, {originalMessage.data(), originalMessage.length()}, true); |
| 26 | + m.unlock(); |
| 27 | + |
| 28 | + std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 29 | + } |
| 30 | + |
| 31 | + }); |
| 32 | + |
| 33 | + uWS::App().ws<PerSocketData>("/*", { |
| 34 | + /* You must only use SHARED_COMPRESSOR with precompression (can't use dedicated_compressor) */ |
| 35 | + .compression = uWS::CompressOptions(uWS::SHARED_COMPRESSOR | uWS::DEDICATED_DECOMPRESSOR), |
| 36 | + /* Handlers */ |
| 37 | + .upgrade = nullptr, |
| 38 | + .open = [](auto */*ws*/) { |
| 39 | + /* Open event here, you may access ws->getUserData() which points to a PerSocketData struct */ |
| 40 | + |
| 41 | + }, |
| 42 | + .message = [&originalMessage, &compressedMessage, &m](auto *ws, std::string_view message, uWS::OpCode opCode) { |
| 43 | + |
| 44 | + /* First respond by echoing what they send us, without compression */ |
| 45 | + ws->send(message, opCode, false); |
| 46 | + |
| 47 | + /* This should be wrapped up into ws->sendPrepared(PreparedMessage) in the future, experimental for now */ |
| 48 | + m.lock(); |
| 49 | + if (ws->hasNegotiatedCompression() && compressedMessage.length() < originalMessage.length()) { |
| 50 | + std::cout << "Responding with precompressed message saving " << (originalMessage.length() - compressedMessage.length()) << " bytes" << std::endl; |
| 51 | + ws->send({compressedMessage.data(), compressedMessage.length()}, uWS::OpCode::TEXT, uWS::CompressFlags::ALREADY_COMPRESSED); |
| 52 | + } else { |
| 53 | + ws->send({originalMessage.data(), originalMessage.length()}, uWS::OpCode::TEXT); |
| 54 | + } |
| 55 | + m.unlock(); |
| 56 | + }, |
| 57 | + .dropped = [](auto */*ws*/, std::string_view /*message*/, uWS::OpCode /*opCode*/) { |
| 58 | + /* A message was dropped due to set maxBackpressure and closeOnBackpressureLimit limit */ |
| 59 | + }, |
| 60 | + .drain = [](auto */*ws*/) { |
| 61 | + /* Check ws->getBufferedAmount() here */ |
| 62 | + }, |
| 63 | + .ping = [](auto */*ws*/, std::string_view) { |
| 64 | + /* Not implemented yet */ |
| 65 | + }, |
| 66 | + .pong = [](auto */*ws*/, std::string_view) { |
| 67 | + /* Not implemented yet */ |
| 68 | + }, |
| 69 | + .close = [](auto */*ws*/, int /*code*/, std::string_view /*message*/) { |
| 70 | + /* You may access ws->getUserData() here */ |
| 71 | + } |
| 72 | + }).listen(9001, [&t2](auto *listen_socket) { |
| 73 | + if (listen_socket) { |
| 74 | + std::cout << "Listening on port " << 9001 << std::endl; |
| 75 | + } |
| 76 | + }).run(); |
| 77 | +} |
0 commit comments