From 886de25e14dc115abd46ac3606ff6d74993a5ce9 Mon Sep 17 00:00:00 2001 From: Jonas Bauer Date: Mon, 1 Aug 2022 02:24:05 +0200 Subject: [PATCH] added string_view support for sending message --- websocketpp/connection.hpp | 15 +++++++++++++++ websocketpp/impl/connection_impl.hpp | 9 +++++++++ websocketpp/message_buffer/message.hpp | 10 ++++++++++ 3 files changed, 34 insertions(+) diff --git a/websocketpp/connection.hpp b/websocketpp/connection.hpp index f1e311ea2..e7294a5e8 100644 --- a/websocketpp/connection.hpp +++ b/websocketpp/connection.hpp @@ -655,6 +655,21 @@ class connection lib::error_code send(std::string const & payload, frame::opcode::value op = frame::opcode::text); + /// Create a message and then add it to the outgoing send queue + /** + * Convenience method to send a message given a payload string and + * optionally an opcode. Default opcode is utf8 text. + * + * This method locks the m_write_lock mutex + * + * @param payload The payload string to generated the message with + * + * @param op The opcode to generated the message with. Default is + * frame::opcode::text + */ + lib::error_code send(std::string_view payload, frame::opcode::value op = + frame::opcode::text); + /// Send a message (raw array overload) /** * Convenience method to send a message given a raw array and optionally an diff --git a/websocketpp/impl/connection_impl.hpp b/websocketpp/impl/connection_impl.hpp index 6ab7414af..c061a237f 100644 --- a/websocketpp/impl/connection_impl.hpp +++ b/websocketpp/impl/connection_impl.hpp @@ -90,6 +90,15 @@ lib::error_code connection::send(std::string const & payload, return send(msg); } +template +lib::error_code connection::send(std::string_view payload, frame::opcode::value op) +{ + message_ptr msg = m_msg_manager->get_message(op,payload.size()); + msg->append_payload(payload); + msg->set_compressed(true); + return send(msg); +} + template lib::error_code connection::send(void const * payload, size_t len, frame::opcode::value op) diff --git a/websocketpp/message_buffer/message.hpp b/websocketpp/message_buffer/message.hpp index da36e2001..9376135d8 100644 --- a/websocketpp/message_buffer/message.hpp +++ b/websocketpp/message_buffer/message.hpp @@ -288,6 +288,16 @@ class message { m_payload.append(payload); } + /// Append payload data + /** + * Append data to the message buffer's payload. + * + * @param payload A string_view containing the data array to append. + */ + void append_payload(std::string_view payload) { + m_payload.append(payload); + } + /// Append payload data /** * Append data to the message buffer's payload.