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 10 seconds (10000 ms).

#include <network/YiWebSocket.h>

Public Types

enum  MESSAGE_TYPE {
  NONE,
  BINARY,
  TEXT,
  UNKNOWN
}
 Possible types of data contained in the websocket messages. More...
 
enum  WEBSOCKET_RESULT {
  WS_SUCCESS,
  WS_ERROR,
  WS_NOT_CONNECTED,
  WS_ALREADY_CONNECTED,
  WS_COULD_NOT_CONNECT,
  WS_CONNECT_TIMEOUT,
  WS_BUFFER_TOO_SMALL,
  WS_PROTOCOL_NOT_SUPPORTED
}
 Possible return values from CYIWebSocket calls. More...
 

Public Member Functions

virtual ~CYIWebSocket ()
 
WEBSOCKET_RESULT Connect ()
 
WEBSOCKET_RESULT Disconnect ()
 
bool IsConnected () const
 
WEBSOCKET_RESULT Send (MESSAGE_TYPE eType, const char *pBuffer, size_t nLength)
 
WEBSOCKET_RESULT Receive (MESSAGE_TYPE &eType, char *pBuffer, size_t nLength, size_t &nReceived)
 
WEBSOCKET_RESULT Peek (CYIWebSocket::MESSAGE_TYPE &eType, size_t &nLength)
 
void SetConnectionTimeoutMs (uint32_t uTimeoutMs)
 
uint32_t GetConnectionTimeoutMs () const
 

Static Public Member Functions

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

Public Attributes

CYISignal< CYIWebSocket * > WebSocketDisconnected
 
CYISignal< CYIWebSocket *, MESSAGE_TYPE, 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 

Possible return values from CYIWebSocket calls.

Enumerator
WS_SUCCESS 
WS_ERROR 
WS_NOT_CONNECTED 
WS_ALREADY_CONNECTED 
WS_COULD_NOT_CONNECT 
WS_CONNECT_TIMEOUT 
WS_BUFFER_TOO_SMALL 
WS_PROTOCOL_NOT_SUPPORTED 

Constructor & Destructor Documentation

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

Member Function Documentation

WEBSOCKET_RESULT CYIWebSocket::Connect ( )

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.

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.
WEBSOCKET_RESULT CYIWebSocket::Disconnect ( )

Disconnects the websocket.

uint32_t CYIWebSocket::GetConnectionTimeoutMs ( ) const

Returns the current timeout for the websocket in milliseconds.

See also
SetConnectionTimeoutMs
bool CYIWebSocket::IsConnected ( ) const

Returns true if the websocket is connected.

WEBSOCKET_RESULT CYIWebSocket::Peek ( CYIWebSocket::MESSAGE_TYPE eType,
size_t nLength 
)

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

WEBSOCKET_RESULT CYIWebSocket::Receive ( MESSAGE_TYPE eType,
char *  pBuffer,
size_t  nLength,
size_t nReceived 
)

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

WEBSOCKET_RESULT CYIWebSocket::Send ( MESSAGE_TYPE  eType,
const char *  pBuffer,
size_t  nLength 
)

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

void CYIWebSocket::SetConnectionTimeoutMs ( uint32_t  uTimeoutMs)

Sets the length of time in milliseconds, uTimeoutMs, that the websocket will wait for a connection before aborting the Connect() operation. The default timeout is 10000 milliseconds (10 seconds).

Note
Setting a timeout of 0 will result in no timeout.

Member Data Documentation

CYISignal<CYIWebSocket *> CYIWebSocket::WebSocketDisconnected

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

CYISignal<CYIWebSocket *, MESSAGE_TYPE, 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: