You.i Engine
CYIWebSocket Class Reference

Detailed Description

Implements a basic secure WebSocket.

The CYIWebSocket class provides methods to send and receive data over a secure (wss://) or insecure (ws://) websocket. After it is connected, the websocket can be used in a polling mode, using peek to see if there is a message waiting, or by using a signal handler for the WebSocketReceivedMessage signal which is emitted when a message arrives. The default connection timeout (the length of time the websocket Connect() operation will wait for a connection) is 1 minute.

#include <network/YiWebSocket.h>

Public Types

enum  MessageType {
  MessageType::None = 0,
 Possible types of data contained in the websocket messages. More...
enum  Result {
  Result::Success = 0,
 Possible return values from CYIWebSocket calls. More...

Public Member Functions

virtual ~CYIWebSocket ()
Result Connect (std::chrono::milliseconds timeout=std::chrono::minutes(1))
Result Disconnect ()
bool IsConnected () const
Result Send (MessageType type, const char *pBuffer, size_t length)
Result Receive (MessageType &type, char *pBuffer, size_t length, size_t &received)
Result Peek (CYIWebSocket::MessageType &type, size_t &length)
void AddSubprotocol (const CYIString &subprotocol)
const CYIStringGetSubprotocol () const
void AddHeader (const CYIString &key, const CYIString &value)
const std::map< CYIString, CYIString > & GetHeaders () const

Static Public Member Functions

static std::unique_ptr< CYIWebSocketCreate (const CYIString &url)

Public Attributes

CYISignal< CYIWebSocket * > WebSocketDisconnected
CYISignal< CYIWebSocket *, MessageType, uint32_t > WebSocketReceivedMessage

Protected Member Functions

 CYIWebSocket (const CYIString &url)

Member Enumeration Documentation

◆ MessageType

Possible types of data contained in the websocket messages.


◆ Result

enum CYIWebSocket::Result

Possible return values from CYIWebSocket calls.


Constructor & Destructor Documentation

◆ ~CYIWebSocket()

virtual CYIWebSocket::~CYIWebSocket ( )

◆ CYIWebSocket()

CYIWebSocket::CYIWebSocket ( const CYIString url)

Member Function Documentation

◆ AddHeader()

void CYIWebSocket::AddHeader ( const CYIString key,
const CYIString value 

Adds an HTTP header to the connect request.

◆ AddSubprotocol()

void CYIWebSocket::AddSubprotocol ( const CYIString subprotocol)

Adds a subprotocol to the list to send with the opening handshake. This may be called multiple times to request more than one. If the server supports one of these, it may choose one. If so, it will return it in it's handshake reponse and the value will be available via GetSubprotocol(). Subprotocol requests should be added in order of preference.

◆ Connect()

Result CYIWebSocket::Connect ( std::chrono::milliseconds  timeout = std::chrono::minutes(1))

Initiates a connection based on the websocket's uri and timeout value. Connect() will fail if the websockets timeout value has been exceeded. A successful connection will result in the WebSocketConnected signal being emitted.

Setting timeout to NO_TIMEOUT will cause the operation to never timeout.

◆ Create()

static std::unique_ptr<CYIWebSocket> CYIWebSocket::Create ( const CYIString url)

Factory method to create a websocket based on the url passed in. Returns the created CYIWebSocket.

Caller obtains ownership of the returned pointer.

◆ Disconnect()

Result CYIWebSocket::Disconnect ( )

Disconnects the websocket.

◆ GetHeaders()

const std::map<CYIString, CYIString>& CYIWebSocket::GetHeaders ( ) const

Retrieves the HTTP headers.

◆ GetSubprotocol()

const CYIString& CYIWebSocket::GetSubprotocol ( ) const

Retrieves the subprotocol that was negotiated during the handshake.

◆ IsConnected()

bool CYIWebSocket::IsConnected ( ) const

Returns true if the websocket is connected.

◆ Peek()

Result CYIWebSocket::Peek ( CYIWebSocket::MessageType type,
size_t length 

Peeks at the websocket to determine if there are any messages waiting to be retrieved via the Receive() method. type is the type of message to be received and length is the length of the message buffer.

◆ Receive()

Result CYIWebSocket::Receive ( MessageType type,
char *  pBuffer,
size_t  length,
size_t received 

Retrieves a character buffer, pBuffer along with the buffer length, length, and a descriptor of the message type type, from the websocket resource described by the URI. received is an output parameter representing the number of bytes successfully received.

◆ Send()

Result CYIWebSocket::Send ( MessageType  type,
const char *  pBuffer,
size_t  length 

Sends a character buffer, pBuffer along with the buffer length, length, along with a descriptor of the message type type to the websocket resource described by the URI.

Member Data Documentation

◆ WebSocketDisconnected

CYISignal<CYIWebSocket *> CYIWebSocket::WebSocketDisconnected

Signals that websocket has been disconnected and provides a pointer to the disconnected websocket.

◆ WebSocketReceivedMessage

CYISignal<CYIWebSocket *, MessageType, uint32_t> CYIWebSocket::WebSocketReceivedMessage

Signals that websocket has received a message and provides a pointer to the websocket the message was received on, the type of message that was received, and the size of the message buffer. The message can then be retrieved by subsequently calling Receive().

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