Class WebSocketConnection
Synopsis
#include <lib/inc/drogon/WebSocketConnection.h>
class WebSocketConnection
Description
The WebSocket connection abstract class.
Mentioned in
- Controller Introduction / Controller WebSocketController / WebSocketConnection
Methods
WebSocketConnection | ||
~WebSocketConnection | ||
clearContext | Clear the context. | |
connected | Return true if the connection is open. | |
disablePing | Disable sending ping messages to the peer. | |
disconnected | Return true if the connection is closed. | |
forceClose | Close the connection. | |
getContext | Get custom data from the connection. | |
getContextRef | Get the custom data reference from the connection. | |
hasContext | Return true if the context is set by user. | |
localAddr | Return the local IP address and port number of the connection. | |
peerAddr | Return the remote IP address and port number of the connection. | |
send overload | Send a message to the peer. | |
setContext overload | Set custom data on the connection. | |
setPingMessage | Set the heartbeat(ping) message sent to the peer. | |
shutdown | Shut down the write direction, which means that further send operations are disabled. |
Source
Lines 88-219 in lib/inc/drogon/WebSocketConnection.h.
class WebSocketConnection
{
public:
WebSocketConnection() = default;
virtual ~WebSocketConnection(){};
/**
* @brief Send a message to the peer
*
* @param msg The message to be sent.
* @param len The message length.
* @param type The message type.
*/
virtual void send(
const char *msg,
uint64_t len,
const WebSocketMessageType type = WebSocketMessageType::Text) = 0;
/**
* @brief Send a message to the peer
*
* @param msg The message to be sent.
* @param type The message type.
*/
virtual void send(
const std::string &msg,
const WebSocketMessageType type = WebSocketMessageType::Text) = 0;
/// Return the local IP address and port number of the connection
virtual const trantor::InetAddress &localAddr() const = 0;
/// Return the remote IP address and port number of the connection
virtual const trantor::InetAddress &peerAddr() const = 0;
/// Return true if the connection is open
virtual bool connected() const = 0;
/// Return true if the connection is closed
virtual bool disconnected() const = 0;
/**
* @brief Shut down the write direction, which means that further send
* operations are disabled.
*
* @param code Please refer to the enum class CloseCode. (RFC6455 7.4.1)
* @param reason The reason for closing the connection.
*/
virtual void shutdown(const CloseCode code = CloseCode::kNormalClosure,
const std::string &reason = "") = 0;
/// Close the connection
virtual void forceClose() = 0;
/**
* @brief Set custom data on the connection
*
* @param context The custom data.
*/
void setContext(const std::shared_ptr<void> &context)
{
contextPtr_ = context;
}
/**
* @brief Set custom data on the connection
*
* @param context The custom data.
*/
void setContext(std::shared_ptr<void> &&context)
{
contextPtr_ = std::move(context);
}
/**
* @brief Get custom data from the connection
*
* @tparam T The type of the data
* @return std::shared_ptr<T> The smart pointer to the data object.
*/
template <typename T>
std::shared_ptr<T> getContext() const
{
return std::static_pointer_cast<T>(contextPtr_);
}
/**
* @brief Get the custom data reference from the connection.
* @note Please make sure that the context is available.
* @tparam T The type of the data stored in the context.
* @return T&
*/
template <typename T>
T &getContextRef() const
{
return *(static_cast<T *>(contextPtr_.get()));
}
/// Return true if the context is set by user.
bool hasContext()
{
return (bool)contextPtr_;
}
/// Clear the context.
void clearContext()
{
contextPtr_.reset();
}
/**
* @brief Set the heartbeat(ping) message sent to the peer.
*
* @param message The ping message.
* @param interval The sending interval.
* @note
* Both the server and the client in Drogon automatically send the pong
* message after receiving the ping message.
* An empty ping message is sent every 30 seconds by default. The method
* overrides the default behavior.
*/
virtual void setPingMessage(
const std::string &message,
const std::chrono::duration<double> &interval) = 0;
/**
* @brief Disable sending ping messages to the peer.
*/
virtual void disablePing() = 0;
private:
std::shared_ptr<void> contextPtr_;
};