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:
  1. Constructor with Config.
  2. Constructor.
  3. Deleted copy-constructor.
std::shared_ptr<WebSocket> createShared Multiple implementations:
  1. Create shared WebSocket with Config.
  2. Create shared WebSocket.
void setConfig Set WebSocket config.
std::shared_ptr<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:
  1. Send close frame.
  2. Send close frame without message.
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::ChunkedBuffer* shortMessageStream) const

WebSocket::WebSocket

  1. Constructor with Config.
    WebSocket(const std::shared_ptr<oatpp::data::stream::IOStream>& connection, const Config& config)
    
  2. Constructor.
    WebSocket(const std::shared_ptr<oatpp::data::stream::IOStream>& connection, bool maskOutgoingMessages)
    
  3. Deleted copy-constructor.
    WebSocket(const WebSocket&) = delete
    

WebSocket::createShared

  1. Create shared WebSocket with Config.
    static std::shared_ptr<WebSocket> createShared(const std::shared_ptr<oatpp::data::stream::IOStream>& connection, const Config& config)
    
  2. Create shared WebSocket.
    • @param connection - oatpp::data::stream::IOStream.
    • @param maskOutgoingMessages - for servers should be false. For clients should be true.
    • @return
    static std::shared_ptr<WebSocket> createShared(const std::shared_ptr<oatpp::data::stream::IOStream>& connection, bool maskOutgoingMessages)
    

WebSocket::setConfig

Set WebSocket config.

void setConfig(const Config& config)

WebSocket::getConnection

Get WebSocket connection.

std::shared_ptr<oatpp::data::stream::IOStream> getConnection() const

WebSocket::setListener

Set WebSocket events listener.

void setListener(const std::shared_ptr<Listener>& listener) const

WebSocket::getListener

Get socket event listener

std::shared_ptr<Listener> getListener()

WebSocket::iterateFrame

Read one frame from connection and call corresponding methods of listener.
Use this method if you know what you are doing.

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.

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.

void sendFrameHeader(Frame::Header& frameHeader, bool fin, v_word8 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. if false returned socket should be closed manually.

bool sendOneFrame(bool fin, v_word8 opcode, const oatpp::String& message) const

WebSocket::sendClose

  1. Send close frame.
    • @param code - close message code.
    • @param message - message text. oatpp::String.
    • @throws - runtime_error.
    void sendClose(v_word16 code, const oatpp::String& message) const
    
  2. Send close frame without message.
    • @throws - runtime_error.
    void sendClose() const
    

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

virtual void onPing(const WebSocket& socket, const oatpp::String& message) = 0

WebSocket::Listener::onPong

Called when "pong" frame received

virtual void onPong(const WebSocket& socket, const oatpp::String& message) = 0

WebSocket::Listener::onClose

Called when "close" frame received

virtual void onClose(const WebSocket& socket, v_word16 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.

virtual void readMessage(const WebSocket& socket, v_word8 opcode, p_char8 data, data::v_io_size size) = 0