Skip to content

Commit 70ad816

Browse files
committed
Add publishPrepared
1 parent c2927ff commit 70ad816

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

examples/Precompress.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ int main() {
2424
}
2525
});
2626

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

3537
},
36-
.message = [&m, &preparedMessage](auto *ws, std::string_view message, uWS::OpCode opCode) {
38+
.message = [&m, &preparedMessage, &app](auto *ws, std::string_view message, uWS::OpCode opCode) {
3739

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

4547
/* Using publish should also take preparedMessage */
46-
48+
ws->subscribe("test");
49+
app.publishPrepared("test", preparedMessage);
50+
ws->unsubscribe("test");
4751

4852
m.unlock();
4953
},

src/App.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,19 @@ struct TemplatedApp {
147147
return std::move(static_cast<TemplatedApp &&>(*this));
148148
}
149149

150+
/* Same as publish, but takes a prepared message */
151+
bool publishPrepared(std::string_view topic, PreparedMessage &preparedMessage) {
152+
/* It is assumed by heuristics that a prepared message ought to be big,
153+
* and so there is no fast path for small messages (yet?) as preparing a small message is unlikely */
154+
155+
return reinterpret_cast<TopicTree<TopicTreeMessage, PreparedMessage *> *>(topicTree)->publishBig(nullptr, topic, &preparedMessage, [](Subscriber *s, PreparedMessage *preparedMessage) {
156+
auto *ws = (WebSocket<SSL, true, int> *) s->user;
157+
158+
/* Send will drain if needed */
159+
ws->sendPrepared(*preparedMessage);
160+
});
161+
}
162+
150163
/* Publishes a message to all websocket contexts - conceptually as if publishing to the one single
151164
* TopicTree of this app (technically there are many TopicTrees, however the concept is that one
152165
* app has one conceptual Topic tree) */

0 commit comments

Comments
 (0)