Skip to content
This repository has been archived by the owner on Jun 12, 2018. It is now read-only.

Commit

Permalink
Added Connection::send convenience function
Browse files Browse the repository at this point in the history
  • Loading branch information
eidheim committed Jul 28, 2017
1 parent 9a8d9e5 commit 5a0c95a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
7 changes: 7 additions & 0 deletions client_ws.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ namespace SimpleWeb {
});
}

/// Convenience function for sending an std::string message
void send(const std::string &message, const std::function<void(const error_code &)> &callback = nullptr, unsigned char fin_rsv_opcode = 129) {
auto send_stream = std::make_shared<SendStream>();
send_stream->write(message.c_str(), message.size());
send(send_stream, callback, fin_rsv_opcode);
}

void send_close(int status, const std::string &reason = "", const std::function<void(const error_code &)> &callback = nullptr) {
// Send close only once (in case close is initiated by client)
if(closed)
Expand Down
7 changes: 7 additions & 0 deletions server_ws.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,13 @@ namespace SimpleWeb {
});
}

/// Convenience function for sending an std::string message
void send(const std::string &message, const std::function<void(const error_code &)> &callback = nullptr, unsigned char fin_rsv_opcode = 129) {
auto send_stream = std::make_shared<SendStream>();
send_stream->write(message.c_str(), message.size());
send(send_stream, callback, fin_rsv_opcode);
}

void send_close(int status, const std::string &reason = "", const std::function<void(const error_code &)> &callback = nullptr) {
// Send close only once (in case close is initiated by server)
if(closed)
Expand Down
3 changes: 3 additions & 0 deletions ws_examples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ int main() {
"Error: " << ec << ", error message: " << ec.message() << endl;
}
});

// Alternatively, using a convenience function:
// connection->send(message_str, [](const SimpleWeb::error_code & /*ec*/) { /*handle error*/ });
};

echo.on_open = [](shared_ptr<WsServer::Connection> connection) {
Expand Down
3 changes: 3 additions & 0 deletions wss_examples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ int main() {
"Error: " << ec << ", error message: " << ec.message() << endl;
}
});

// Alternatively, using a convenience function:
// connection->send(message_str, [](const SimpleWeb::error_code & /*ec*/) { /*handle error*/ });
};

echo.on_open = [](shared_ptr<WssServer::Connection> connection) {
Expand Down

0 comments on commit 5a0c95a

Please sign in to comment.