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< CYIWebSocket > | Create (const CYIString &url) |
Public Attributes | |
| CYISignal< CYIWebSocket * > | WebSocketDisconnected |
| CYISignal< CYIWebSocket *, MESSAGE_TYPE, uint32_t > | WebSocketReceivedMessage |
Protected Member Functions | |
| CYIWebSocket (const CYIString &url) | |
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 | |
|
virtual |
|
protected |
| 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 |
Factory method to create a websocket based on the url passed in. Returns the created CYIWebSocket.
| WEBSOCKET_RESULT CYIWebSocket::Disconnect | ( | ) |
Disconnects the websocket.
| uint32_t CYIWebSocket::GetConnectionTimeoutMs | ( | ) | const |
Returns the current timeout for the websocket in milliseconds.
| 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).
| 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().