This class represents a WebSocket RFC 6455 connection.
More...
#include <staticlib/pion/websocket.hpp>
Public Member Functions |
| | websocket (std::unique_ptr< http_request > req, std::shared_ptr< tcp_connection > conn, std::function< void(std::unique_ptr< websocket >)> open_handler, std::function< void(std::unique_ptr< websocket >)> message_handler, std::function< void(std::unique_ptr< websocket >)> close_handler, size_t max_receive_cache_size_bytes=(1024 *1024), size_t max_cached_frames_count=1024) |
| | Constructor.
|
| | ~websocket () STATICLIB_NOEXCEPT |
| | Destructor, closes the TCP connection.
|
| | websocket (const websocket &) |
| | Deleted copy constructor.
|
| websocket & | operator= (const websocket &) |
| | Deleted copy-assignment operator.
|
| const std::string & | get_id () |
| | ID for this WebSocket connection.
|
| http_request & | get_request () |
| | Reference to handshake request.
|
| tcp_connection & | get_connection () |
| | Reference to TCP connection.
|
| sl::websocket::frame_type | message_type () |
| | Type (opcode) of the currently cached frame.
|
| websocket_detail::msg_data_src | message_data () |
| | Source that can be used to read unmasked payload data from one or more (for continuation) received frames.
|
| void | write (sl::io::span< const char > data) |
| | Write payload content.
|
| void | write_nocopy (sl::io::span< const char > data) |
| | Write payload content; the data written is not copied, and therefore must persist until the message has finished sending.
|
Static Public Member Functions |
| static bool | is_websocket_upgrade (const http_request &req) |
| | Checks whether specified request is a WebSocket handshake.
|
| static void | start (std::unique_ptr< websocket > self) |
| | Starts the processing of WebSocker connection: asynchronously send handshake response and reads incoming messages.
|
| static void | receive (std::unique_ptr< websocket > self) |
| | Asynchronously receives the incoming messages.
|
| static void | send (std::unique_ptr< websocket > self, sl::websocket::frame_type msg_type=sl::websocket::frame_type::text) |
| | Asynchronously sends the buffered payload using the specified frame type.
|
| template<typename Handler > |
| static void | send_message (std::unique_ptr< websocket > self, sl::websocket::frame_type msg_type, Handler handler) |
| | Asynchronously sends the buffered payload using the specified frame type.
|
| static void | broadcast (std::vector< std::shared_ptr< tcp_connection >> &connections, sl::io::span< const char > msg, sl::websocket::frame_type msg_type=sl::websocket::frame_type::text) |
| | Broadcasts the specified message as a WebSocket frame to every specified connection.
|
| static void | close (std::unique_ptr< websocket > self, const close_status &status=close_status::normal) |
| | Sends close frame to client and closes the underlying TCP connection.
|
Detailed Description
This class represents a WebSocket RFC 6455 connection.
Constructor & Destructor Documentation
| staticlib::pion::websocket::websocket |
( |
std::unique_ptr< http_request > |
req, |
|
|
std::shared_ptr< tcp_connection > |
conn, |
|
|
std::function< void(std::unique_ptr< websocket >)> |
open_handler, |
|
|
std::function< void(std::unique_ptr< websocket >)> |
message_handler, |
|
|
std::function< void(std::unique_ptr< websocket >)> |
close_handler, |
|
|
size_t |
max_receive_cache_size_bytes = (1024*1024), |
|
|
size_t |
max_cached_frames_count = 1024 |
|
) |
| |
|
inline |
Constructor.
- Parameters
-
| req | HTTP request with handshake |
| conn | TCP connection used for handshake |
| open_handler | handler for open events |
| message_handler | handler for message events |
| close_handler | handler for closeevents, MUST NOT be used to send or receive messages |
| max_receive_cache_size_bytes | max allowed size of the receive buffer, 1MB by default |
| max_cached_frames_count | max allowed number of continuation frames, 1024 by default |
| staticlib::pion::websocket::~websocket |
( |
| ) |
|
|
inline |
Destructor, closes the TCP connection.
| staticlib::pion::websocket::websocket |
( |
const websocket & |
| ) |
|
Deleted copy constructor.
Member Function Documentation
| static void staticlib::pion::websocket::broadcast |
( |
std::vector< std::shared_ptr< tcp_connection >> & |
connections, |
|
|
sl::io::span< const char > |
msg, |
|
|
sl::websocket::frame_type |
msg_type = sl::websocket::frame_type::text |
|
) |
| |
|
inlinestatic |
Broadcasts the specified message as a WebSocket frame to every specified connection.
Thread-safe: may be called concurrently from any thread.
May cause the broadcast message to interleave with other frames (put in the middle of continuation sequence, or in the middle to TCP fragmented sequence), if target connection is used for outbound traffic at the the time of broadcast.
- Parameters
-
| connections | list of connections to broadcast |
| msg | message to broadcast |
| msg_type | frame type (opcode) to use |
| static void staticlib::pion::websocket::close |
( |
std::unique_ptr< websocket > |
self, |
|
|
const close_status & |
status = close_status::normal |
|
) |
| |
|
inlinestatic |
Sends close frame to client and closes the underlying TCP connection.
- Parameters
-
| self | websocket instance |
| status | close status to include with close frame |
Reference to TCP connection.
- Returns
- connection
| const std::string& staticlib::pion::websocket::get_id |
( |
| ) |
|
|
inline |
ID for this WebSocket connection.
- Returns
- value of
Sec-WebSocket-Key request handshake header
Reference to handshake request.
- Returns
- handshake request
| static bool staticlib::pion::websocket::is_websocket_upgrade |
( |
const http_request & |
req | ) |
|
|
inlinestatic |
Checks whether specified request is a WebSocket handshake.
Source that can be used to read unmasked payload data from one or more (for continuation) received frames.
Invalidated on send or receive operation over this instance
- Returns
- unmasked payload data
| sl::websocket::frame_type staticlib::pion::websocket::message_type |
( |
| ) |
|
|
inline |
Type (opcode) of the currently cached frame.
- Returns
- opcode of the last cached frame,
invalid type if no frames are cached
Deleted copy-assignment operator.
| static void staticlib::pion::websocket::receive |
( |
std::unique_ptr< websocket > |
self | ) |
|
|
inlinestatic |
Asynchronously receives the incoming messages.
- Parameters
-
| static void staticlib::pion::websocket::send |
( |
std::unique_ptr< websocket > |
self, |
|
|
sl::websocket::frame_type |
msg_type = sl::websocket::frame_type::text |
|
) |
| |
|
inlinestatic |
Asynchronously sends the buffered payload using the specified frame type.
- Parameters
-
| self | websocket instance |
| msg_type | frame opcode to use |
template<typename Handler >
| static void staticlib::pion::websocket::send_message |
( |
std::unique_ptr< websocket > |
self, |
|
|
sl::websocket::frame_type |
msg_type, |
|
|
Handler |
handler |
|
) |
| |
|
inlinestatic |
Asynchronously sends the buffered payload using the specified frame type.
Handler is invoked after send is completed successfully.
- Parameters
-
| self | websocket instance |
| msg_type | frame opcode to use |
| handler | handler function to invoke on successful send |
| static void staticlib::pion::websocket::start |
( |
std::unique_ptr< websocket > |
self | ) |
|
|
inlinestatic |
Starts the processing of WebSocker connection: asynchronously send handshake response and reads incoming messages.
- Parameters
-
| void staticlib::pion::websocket::write |
( |
sl::io::span< const char > |
data | ) |
|
|
inline |
Write payload content.
- Parameters
-
| data | to append to the payload content |
| void staticlib::pion::websocket::write_nocopy |
( |
sl::io::span< const char > |
data | ) |
|
|
inline |
Write payload content; the data written is not copied, and therefore must persist until the message has finished sending.
- Parameters
-
| data | the data to append to the payload content |
The documentation for this class was generated from the following file: