# WebSocket.hpp
This File On Github
Ask A Question
API: latest
module: oatpp-websocket
#include "oatpp-websocket/WebSocket.hpp"
# WebSocket
WebSocket.
namespace oatpp { namespace websocket {
class WebSocket : public oatpp::base::Countable {}
}}
# Methods
Return Type | Name | Summary |
---|---|---|
void | readPayload | if(shortMessageStream == nullptr) - read call readMessage() method of listener |
[none] | WebSocket | Multiple implementations:
|
std::shared_ptr<WebSocket> | createShared | Multiple implementations:
|
void | setConfig | Set WebSocket config. |
provider::ResourceHandle<oatpp::data::stream::IOStream> | getConnection | Get WebSocket connection. |
void | setListener | Set WebSocket events listener. |
std::shared_ptr<Listener> | getListener | Get socket event listener |
void | iterateFrame | Read one frame from connection and call corresponding methods of listener. |
void | listen | Blocks until stopListening() is called or error occurred |
void | stopListening | Break listen loop. See WebSocket::listen(). |
void | writeFrameHeader | Send custom frame to peer. |
void | sendFrameHeader | Send default frame to peer with fin, opcode and messageSize set. |
bool | sendOneFrame | Send one frame message with custom fin and opcode. |
void | sendClose | Multiple implementations:
|
void | sendPing | Send ping frame. |
void | sendPong | Send pong message. |
void | sendOneFrameText | Send one-frame text message. |
void | sendOneFrameBinary | Send one-frame binary message. |
# WebSocket::readPayload
if(shortMessageStream == nullptr) - read call readMessage() method of listener if(shortMessageStream) - read message to shortMessageStream. Don't call listener
void readPayload(const Frame::Header& frameHeader, oatpp::data::stream::BufferOutputStream* shortMessageStream) const
# WebSocket::WebSocket
- Constructor with Config.
- @param connection - oatpp::data::stream::IOStream.
- @param config - oatpp::websocket::Config.
WebSocket(const provider::ResourceHandle<oatpp::data::stream::IOStream>& connection, const Config& config)
- Constructor.
- @param connection - oatpp::data::stream::IOStream.
- @param maskOutgoingMessages - for servers should be
false
. For clients should betrue
.
WebSocket(const provider::ResourceHandle<oatpp::data::stream::IOStream>& connection, bool maskOutgoingMessages)
- Deleted copy-constructor.
WebSocket(const WebSocket&) = delete
# WebSocket::createShared
- Create shared WebSocket with Config.
- @param connection - oatpp::data::stream::IOStream.
- @param config - oatpp::websocket::Config.
- @return -
std::shared_ptr
to WebSocket.
static std::shared_ptr<WebSocket> createShared(const provider::ResourceHandle<oatpp::data::stream::IOStream>& connection, const Config& config)
- Create shared WebSocket.
- @param connection - oatpp::data::stream::IOStream.
- @param maskOutgoingMessages - for servers should be
false
. For clients should betrue
. - @return
static std::shared_ptr<WebSocket> createShared(const provider::ResourceHandle<oatpp::data::stream::IOStream>& connection, bool maskOutgoingMessages)
# WebSocket::setConfig
Set WebSocket config.
- @param config - oatpp::websocket::Config.
void setConfig(const Config& config)
# WebSocket::getConnection
Get WebSocket connection.
- @return - oatpp::data::stream::IOStream.
provider::ResourceHandle<oatpp::data::stream::IOStream> getConnection() const
# WebSocket::setListener
Set WebSocket events listener.
- @param listener - WebSocket::Listener.
void setListener(const std::shared_ptr<Listener>& listener) const
# WebSocket::getListener
Get socket event listener
- @return - WebSocket::Listener.
std::shared_ptr<Listener> getListener() const
# WebSocket::iterateFrame
Read one frame from connection and call corresponding methods of listener.
Use this method if you know what you are doing.
- @param frameHeader - oatpp::websocket::Frame::Header.
void iterateFrame(Frame::Header& frameHeader)
# WebSocket::listen
Blocks until stopListening() is called or error occurred Read incoming frames and call corresponding methods of listener. See WebSocket::setListener().
void listen()
# WebSocket::stopListening
Break listen loop. See WebSocket::listen().
void stopListening() const
# WebSocket::writeFrameHeader
Send custom frame to peer.
Use this method if you know what you are doing.
- @param frameHeader - oatpp::websocket::Frame::Header.
void writeFrameHeader(const Frame::Header& frameHeader) const
# WebSocket::sendFrameHeader
Send default frame to peer with fin, opcode and messageSize set.
Use this method if you know what you are doing.
- @param frameHeader - oatpp::websocket::Frame::Header.
- @param fin - FIN bit.
- @param opcode - operation code.
- @param messageSize - coming message size.
void sendFrameHeader(Frame::Header& frameHeader, bool fin, v_uint8 opcode, v_int64 messageSize) const
# WebSocket::sendOneFrame
Send one frame message with custom fin and opcode.
- @param fin - FIN bit.
- @param opcode - operation code.
- @param message - message text. oatpp::String.
- @return -
true
on success,false
on error. iffalse
returned socket should be closed manually.
bool sendOneFrame(bool fin, v_uint8 opcode, const oatpp::String& message) const
# WebSocket::sendClose
- Send close frame.
- @param code - close message code.
- @param message - message text. oatpp::String.
- @throws -
runtime_error
.
void sendClose(v_uint16 code, const oatpp::String& message) const
- Send close frame without message.
- @throws -
runtime_error
.
void sendClose() const
- @throws -
# WebSocket::sendPing
Send ping frame.
- @param message - message text. oatpp::String.
- @throws -
runtime_error
.
void sendPing(const oatpp::String& message) const
# WebSocket::sendPong
Send pong message.
- @param message - message text. oatpp::String.
- @throws -
runtime_error
.
void sendPong(const oatpp::String& message) const
# WebSocket::sendOneFrameText
Send one-frame text message.
- @param message - message text. oatpp::String.
- @throws -
runtime_error
.
void sendOneFrameText(const oatpp::String& message) const
# WebSocket::sendOneFrameBinary
Send one-frame binary message.
- @param message - message text. oatpp::String.
- @throws -
runtime_error
.
void sendOneFrameBinary(const oatpp::String& message) const
# WebSocket::Listener
Listener for websocket events.
namespace oatpp { namespace websocket {
class WebSocket : public oatpp::base::Countable {
class Listener {}
};
}}
# Typedefs
Type | Name | Summary |
---|---|---|
oatpp::websocket::WebSocket | WebSocket | Convenience typedef fo oatpp::websocket::WebSocket. |
# Methods
Return Type | Name | Summary |
---|---|---|
[none] | ~Listener | Default virtual destructor. |
void | onPing | Called when "ping" frame received |
void | onPong | Called when "pong" frame received |
void | onClose | Called when "close" frame received |
void | readMessage | Called when "text" or "binary" frame received. |
# WebSocket::Listener::WebSocket
Convenience typedef fo oatpp::websocket::WebSocket.
typedef oatpp::websocket::WebSocket WebSocket
# WebSocket::Listener::~Listener
Default virtual destructor.
virtual ~Listener() = default
# WebSocket::Listener::onPing
Called when "ping" frame received
- @param socket - oatpp::websocket::WebSocket.
- @param message - message text; oatpp::String.
virtual void onPing(const WebSocket& socket, const oatpp::String& message) = 0
# WebSocket::Listener::onPong
Called when "pong" frame received
- @param socket - oatpp::websocket::WebSocket.
- @param message - message text; oatpp::String.
virtual void onPong(const WebSocket& socket, const oatpp::String& message) = 0
# WebSocket::Listener::onClose
Called when "close" frame received
- @param socket - oatpp::websocket::WebSocket.
- @param code - close frame message code.
- @param message - message text; oatpp::String.
virtual void onClose(const WebSocket& socket, v_uint16 code, const oatpp::String& message) = 0
# WebSocket::Listener::readMessage
Called when "text" or "binary" frame received.
When all data of message is read, readMessage is called again with size == 0 to indicate end of the message.
- @param socket - oatpp::websocket::WebSocket.
- @param opcode - oatpp::websocket::Frame::OPCODE_TEXT or oatpp::websocket::Frame::OPCODE_BINARY.
- @param data - pointer to message data.
- @param size - data size.
virtual void readMessage(const WebSocket& socket, v_uint8 opcode, p_char8 data, oatpp::v_io_size size) = 0