Skip to content

Commit

Permalink
Add app.publish
Browse files Browse the repository at this point in the history
  • Loading branch information
uNetworkingAB committed Oct 17, 2019
1 parent de68886 commit 49f460e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
7 changes: 7 additions & 0 deletions src/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ struct TemplatedApp {
httpContext->filter(std::move(filterHandler));
}

/* Publishes a message to all websocket contexts */
void publish(std::string_view topic, std::string_view message, OpCode opCode, bool compress = false) {
for (auto *webSocketContext : webSocketContexts) {
webSocketContext->getExt()->publish(topic, message, opCode, compress);
}
}

~TemplatedApp() {
/* Let's just put everything here */
if (httpContext) {
Expand Down
9 changes: 2 additions & 7 deletions src/WebSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,8 @@ struct WebSocket : AsyncSocket<SSL> {
WebSocketContextData<SSL> *webSocketContextData = (WebSocketContextData<SSL> *) us_socket_context_ext(SSL,
(us_socket_context_t *) us_socket_context(SSL, (us_socket_t *) this)
);

/* We frame the message right here and only pass raw bytes to the pub/subber */
char *dst = (char *) malloc(protocol::messageFrameSize(message.size()));
size_t dst_length = protocol::formatMessage<true>(dst, message.data(), message.length(), opCode, message.length(), false);

webSocketContextData->topicTree.publish(topic, std::string_view(dst, dst_length));
free(dst);
/* Is the same as publishing per websocket context */
webSocketContextData->publish(topic, message, opCode, compress);
}
};

Expand Down
10 changes: 10 additions & 0 deletions src/WebSocketContextData.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ struct WebSocketContextData {
topicTree.drain();
});
}

/* Helper for topictree publish, common path from app and ws */
void publish(std::string_view topic, std::string_view message, OpCode opCode, bool compress) {
/* We frame the message right here and only pass raw bytes to the pub/subber */
char *dst = (char *) malloc(protocol::messageFrameSize(message.size()));
size_t dst_length = protocol::formatMessage<true>(dst, message.data(), message.length(), opCode, message.length(), false);

topicTree.publish(topic, std::string_view(dst, dst_length));
::free(dst);
}
};

}
Expand Down

0 comments on commit 49f460e

Please sign in to comment.