drogon
C++14/17-based HTTP application framework
Loading...
Searching...
No Matches
drogon::WebSocketConnection Class Referenceabstract

The WebSocket connection abstract class. More...

#include <drogon/WebSocketConnection.h>

Public Member Functions

virtual void send (const char *msg, uint64_t len, const WebSocketMessageType type=WebSocketMessageType::Text)=0
 Send a message to the peer.
virtual void send (std::string_view msg, const WebSocketMessageType type=WebSocketMessageType::Text)=0
 Send a message to the peer.
virtual void sendJson (const Json::Value &json, const WebSocketMessageType type=WebSocketMessageType::Text)=0
 Send a message to the peer.
virtual const trantor::InetAddress & localAddr () const =0
 Return the local IP address and port number of the connection.
virtual const trantor::InetAddress & peerAddr () const =0
 Return the remote IP address and port number of the connection.
virtual bool connected () const =0
 Return true if the connection is open.
virtual bool disconnected () const =0
 Return true if the connection is closed.
virtual void shutdown (const CloseCode code=CloseCode::kNormalClosure, const std::string &reason="")=0
 Shut down the write direction, which means that further send operations are disabled.
virtual void forceClose ()=0
 Close the connection.
void setContext (const std::shared_ptr< void > &context)
 Set custom data on the connection.
void setContext (std::shared_ptr< void > &&context)
 Set custom data on the connection.
template<typename T>
std::shared_ptr< T > getContext () const
 Get custom data from the connection.
template<typename T>
T & getContextRef () const
 Get the custom data reference from the connection.
bool hasContext ()
 Return true if the context is set by user.
void clearContext ()
 Clear the context.
virtual void setPingMessage (const std::string &message, const std::chrono::duration< double > &interval)=0
 Set the heartbeat(ping) message sent to the peer.
virtual void disablePing ()=0
 Disable sending ping messages to the peer.

Detailed Description

The WebSocket connection abstract class.

Member Function Documentation

◆ getContext()

template<typename T>
std::shared_ptr< T > drogon::WebSocketConnection::getContext ( ) const
inline

Get custom data from the connection.

Template Parameters
TThe type of the data
Returns
std::shared_ptr<T> The smart pointer to the data object.

◆ getContextRef()

template<typename T>
T & drogon::WebSocketConnection::getContextRef ( ) const
inline

Get the custom data reference from the connection.

Note
Please make sure that the context is available.
Template Parameters
TThe type of the data stored in the context.
Returns
T&

◆ send() [1/2]

virtual void drogon::WebSocketConnection::send ( const char * msg,
uint64_t len,
const WebSocketMessageType type = WebSocketMessageType::Text )
pure virtual

Send a message to the peer.

Parameters
msgThe message to be sent.
lenThe message length.
typeThe message type.

◆ send() [2/2]

virtual void drogon::WebSocketConnection::send ( std::string_view msg,
const WebSocketMessageType type = WebSocketMessageType::Text )
pure virtual

Send a message to the peer.

Parameters
msgThe message to be sent.
typeThe message type.

◆ sendJson()

virtual void drogon::WebSocketConnection::sendJson ( const Json::Value & json,
const WebSocketMessageType type = WebSocketMessageType::Text )
pure virtual

Send a message to the peer.

Parameters
jsonThe JSON message to be sent.
typeThe message type.

◆ setContext() [1/2]

void drogon::WebSocketConnection::setContext ( const std::shared_ptr< void > & context)
inline

Set custom data on the connection.

Parameters
contextThe custom data.

◆ setContext() [2/2]

void drogon::WebSocketConnection::setContext ( std::shared_ptr< void > && context)
inline

Set custom data on the connection.

Parameters
contextThe custom data.

◆ setPingMessage()

virtual void drogon::WebSocketConnection::setPingMessage ( const std::string & message,
const std::chrono::duration< double > & interval )
pure virtual

Set the heartbeat(ping) message sent to the peer.

Parameters
messageThe ping message.
intervalThe 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.

◆ shutdown()

virtual void drogon::WebSocketConnection::shutdown ( const CloseCode code = CloseCode::kNormalClosure,
const std::string & reason = "" )
pure virtual

Shut down the write direction, which means that further send operations are disabled.

Parameters
codePlease refer to the enum class CloseCode. (RFC6455 7.4.1)
reasonThe reason for closing the connection.

The documentation for this class was generated from the following file: