Skip to content

Commit

Permalink
Add publishPrepared
Browse files Browse the repository at this point in the history
  • Loading branch information
uNetworkingAB committed Jan 28, 2025
1 parent c2927ff commit 70ad816
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
10 changes: 7 additions & 3 deletions examples/Precompress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ int main() {
}
});

uWS::App().ws<PerSocketData>("/*", {
uWS::App app;

app.ws<PerSocketData>("/*", {
/* You must only use SHARED_COMPRESSOR with precompression (can't use dedicated_compressor) */
.compression = uWS::CompressOptions(uWS::SHARED_COMPRESSOR | uWS::DEDICATED_DECOMPRESSOR),
/* Handlers */
Expand All @@ -33,7 +35,7 @@ int main() {
/* Open event here, you may access ws->getUserData() which points to a PerSocketData struct */

},
.message = [&m, &preparedMessage](auto *ws, std::string_view message, uWS::OpCode opCode) {
.message = [&m, &preparedMessage, &app](auto *ws, std::string_view message, uWS::OpCode opCode) {

/* First respond by echoing what they send us, without compression */
ws->send(message, opCode, false);
Expand All @@ -43,7 +45,9 @@ int main() {
ws->sendPrepared(preparedMessage);

/* Using publish should also take preparedMessage */

ws->subscribe("test");
app.publishPrepared("test", preparedMessage);
ws->unsubscribe("test");

m.unlock();
},
Expand Down
13 changes: 13 additions & 0 deletions src/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,19 @@ struct TemplatedApp {
return std::move(static_cast<TemplatedApp &&>(*this));
}

/* Same as publish, but takes a prepared message */
bool publishPrepared(std::string_view topic, PreparedMessage &preparedMessage) {
/* It is assumed by heuristics that a prepared message ought to be big,
* and so there is no fast path for small messages (yet?) as preparing a small message is unlikely */

return reinterpret_cast<TopicTree<TopicTreeMessage, PreparedMessage *> *>(topicTree)->publishBig(nullptr, topic, &preparedMessage, [](Subscriber *s, PreparedMessage *preparedMessage) {
auto *ws = (WebSocket<SSL, true, int> *) s->user;

/* Send will drain if needed */
ws->sendPrepared(*preparedMessage);
});
}

/* Publishes a message to all websocket contexts - conceptually as if publishing to the one single
* TopicTree of this app (technically there are many TopicTrees, however the concept is that one
* app has one conceptual Topic tree) */
Expand Down

0 comments on commit 70ad816

Please sign in to comment.