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,
  MessageType::Binary,
  MessageType::Text,
  MessageType::Unknown
}
 Possible types of data contained in the websocket messages. More...
 
enum  Result {
  Result::Success = 0,
  Result::Error,
  Result::NotConnected,
  Result::AlreadyConnected,
  Result::CouldNotConnect,
  Result::ConnectTimeout,
  Result::BufferTooSmall,
  Result::ProtocolNotSupported
}
 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
 

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

Possible types of data contained in the websocket messages.

Enumerator
None 
Binary 
Text 
Unknown 
enum CYIWebSocket::Result
strong

Possible return values from CYIWebSocket calls.

Enumerator
Success 
Error 
NotConnected 
AlreadyConnected 
CouldNotConnect 
ConnectTimeout 
BufferTooSmall 
ProtocolNotSupported 

Constructor & Destructor Documentation

virtual CYIWebSocket::~CYIWebSocket ( )
virtual
CYIWebSocket::CYIWebSocket ( const CYIString url)
protected

Member Function Documentation

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.

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.

Note
Setting timeout to NO_TIMEOUT will cause the operation to never timeout.
static std::unique_ptr<CYIWebSocket> CYIWebSocket::Create ( const CYIString url)
static

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

Note
Caller obtains ownership of the returned pointer.
Result CYIWebSocket::Disconnect ( )

Disconnects the websocket.

const CYIString& CYIWebSocket::GetSubprotocol ( ) const

Retrieves the subprotocol that was negotiated during the handshake.

bool CYIWebSocket::IsConnected ( ) const

Returns true if the websocket is connected.

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.

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.

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

CYISignal<CYIWebSocket *> CYIWebSocket::WebSocketDisconnected

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

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: