WebSocket C Reference Documentation

WebSocket

Current Version: 9.5.0.72

Provides an API for implementing the client side of the WebSocket protocol.

Create/Dispose

HCkWebSocket instance = CkWebSocket_Create();
// ...
CkWebSocket_Dispose(instance);
HCkWebSocket CkWebSocket_Create(void);

Creates an instance of the HCkWebSocket object and returns a handle ("void *" pointer). The handle is passed in the 1st argument for the functions listed on this page.

void CkWebSocket_Dispose(HCkWebSocket handle);

Objects created by calling CkWebSocket_Create must be freed by calling this method. A memory leak occurs if a handle is not disposed by calling this function. Also, any handle returned by a Chilkat "C" function must also be freed by the application by calling the appropriate Dispose method, such as CkWebSocket_Dispose.

Callback Functions

Callback Functions introduced in Chilkat v9.5.0.56
void CkWebSocket_setAbortCheck(HCkWebSocket cHandle, BOOL (*fnAbortCheck)(void));

Provides the opportunity for a method call to be aborted. If TRUE is returned, the operation in progress is aborted. Return FALSE to allow the current method call to continue. This callback function is called periodically based on the value of the HeartbeatMs property. (If HeartbeatMs is 0, then no callbacks are made.) As an example, to make 5 AbortCheck callbacks per second, set the HeartbeatMs property equal to 200.

void CkWebSocket_setPercentDone(HCkWebSocket cHandle, BOOL (*fnPercentDone)(int pctDone));

Provides the percentage completed for any method that involves network communications or time-consuming processing (assuming it is a method where a percentage completion can be measured). This callback is only called when it is possible to know a percentage completion, and when it makes sense to express the operation as a percentage completed. The pctDone argument will have a value from 1 to 100. For methods that complete very quickly, the number of PercentDone callbacks will vary, but the final callback should have a value of 100. For long running operations, no more than one callback per percentage point will occur (for example: 1, 2, 3, ... 98, 99, 100).

This callback counts as an AbortCheck callback, and takes the place of the AbortCheck event when it fires.

The return value indicates whether the method call should be aborted, or whether it should proceed. Return TRUE to abort, and FALSE to proceed.

void CkWebSocket_setProgressInfo(HCkWebSocket cHandle, void (*fnProgressInfo)(const char *name, const char *value));

This is a general callback that provides name/value information about what is happening at certain points during a method call. To see the information provided in ProgressInfo callbacks, if any, write code to handle this event and log the name/value pairs. Most are self-explanatory.

void CkWebSocket_setTaskCompleted(HCkWebSocket cHandle, void (*fnTaskCompleted)(HCkTask hTask));

Called in the background thread when an asynchronous task completes. (Note: When an async method is running, all callbacks are in the background thread.)

Properties

CloseAutoRespond
BOOL CkWebSocket_getCloseAutoRespond(HCkWebSocket cHandle);
void CkWebSocket_putCloseAutoRespond(HCkWebSocket cHandle, BOOL newVal);
Introduced in version 9.5.0.70

If TRUE, then a Close control frame is automatically sent in response to receiving a Close control frame (assuming that we did not initiate the Close in the first place). When the Close frame has both been received and sent, the underlying connection is automatically closed (as per the WebSocket protocol RFC specifications). Thus, if this property is TRUE, then two things automatically happen when a Close frame is received: (1) a Close frame is sent in response, and (2) the connection is closed.

The default value of this property is TRUE.

top
CloseReason
void CkWebSocket_getCloseReason(HCkWebSocket cHandle, HCkString retval);
const char *CkWebSocket_closeReason(HCkWebSocket cHandle);
Introduced in version 9.5.0.70

The reason string received with the Close frame, if any.

top
CloseReceived
BOOL CkWebSocket_getCloseReceived(HCkWebSocket cHandle);
Introduced in version 9.5.0.70

If TRUE, then a Close frame was already received on this websocket connection. If CloseAutoRespond is FALSE, then an application can check this property value to determine if a Close frame should be sent in response.

top
CloseStatusCode
int CkWebSocket_getCloseStatusCode(HCkWebSocket cHandle);
Introduced in version 9.5.0.70

The status code received with the Close frame. If no status code was provided, or no Close frame has yet been received, then this property will be 0.

top
DebugLogFilePath
void CkWebSocket_getDebugLogFilePath(HCkWebSocket cHandle, HCkString retval);
void CkWebSocket_putDebugLogFilePath(HCkWebSocket cHandle, const char *newVal);
const char *CkWebSocket_debugLogFilePath(HCkWebSocket cHandle);

If set to a file path, causes each Chilkat method or property call to automatically append it's LastErrorText to the specified log file. The information is appended such that if a hang or crash occurs, it is possible to see the context in which the problem occurred, as well as a history of all Chilkat calls up to the point of the problem. The VerboseLogging property can be set to provide more detailed information.

This property is typically used for debugging the rare cases where a Chilkat method call hangs or generates an exception that halts program execution (i.e. crashes). A hang or crash should generally never happen. The typical causes of a hang are:

  1. a timeout related property was set to 0 to explicitly indicate that an infinite timeout is desired,
  2. the hang is actually a hang within an event callback (i.e. it is a hang within the application code), or
  3. there is an internal problem (bug) in the Chilkat code that causes the hang.

top
FinalFrame
BOOL CkWebSocket_getFinalFrame(HCkWebSocket cHandle);
Introduced in version 9.5.0.70

TRUE if the last data frame received by calling ReadFrame was a final frame. Otherwise FALSE.

top
FrameDataLen
int CkWebSocket_getFrameDataLen(HCkWebSocket cHandle);
Introduced in version 9.5.0.70

The number of bytes accumulated from one or more calls to ReadFrame. Accumulated incoming frame data can be retrieved by calling GetFrameData, GetFrameDataSb, or GetFrameDataBd.

top
FrameOpcode
void CkWebSocket_getFrameOpcode(HCkWebSocket cHandle, HCkString retval);
const char *CkWebSocket_frameOpcode(HCkWebSocket cHandle);
Introduced in version 9.5.0.70

Indicates the type of frame received in the last call to ReadFrame. Possible values are "Continuation", "Text", "Binary", "Close", "Ping", or "Pong". Initially this property is set to the empty string because nothing has yet been received.

top
FrameOpcodeInt
int CkWebSocket_getFrameOpcodeInt(HCkWebSocket cHandle);
Introduced in version 9.5.0.70

The integer value of the opcode (type of frame) received in the last call to ReadFrame. Possible values are:

0 - Continuation
1 - Text
2 - Binary
8 - Close
9 - Ping
10 - Pong

top
IdleTimeoutMs
int CkWebSocket_getIdleTimeoutMs(HCkWebSocket cHandle);
void CkWebSocket_putIdleTimeoutMs(HCkWebSocket cHandle, int newVal);

The maximum amount of time to wait for additional incoming data when receiving, or the max time to wait to send additional data. The default value is 30000 (30 seconds). This is not an overall max timeout. Rather, it is the maximum time to wait when receiving or sending has halted.

top
IsConnected
BOOL CkWebSocket_getIsConnected(HCkWebSocket cHandle);
Introduced in version 9.5.0.70

Returns TRUE if the websocket is connected. Otherwise returns FALSE.

top
LastErrorHtml
void CkWebSocket_getLastErrorHtml(HCkWebSocket cHandle, HCkString retval);
const char *CkWebSocket_lastErrorHtml(HCkWebSocket cHandle);

Provides information in HTML format about the last method/property called. If a method call returns a value indicating failure, or behaves unexpectedly, examine this property to get more information.

top
LastErrorText
void CkWebSocket_getLastErrorText(HCkWebSocket cHandle, HCkString retval);
const char *CkWebSocket_lastErrorText(HCkWebSocket cHandle);

Provides information in plain-text format about the last method/property called. If a method call returns a value indicating failure, or behaves unexpectedly, examine this property to get more information.

top
LastErrorXml
void CkWebSocket_getLastErrorXml(HCkWebSocket cHandle, HCkString retval);
const char *CkWebSocket_lastErrorXml(HCkWebSocket cHandle);

Provides information in XML format about the last method/property called. If a method call returns a value indicating failure, or behaves unexpectedly, examine this property to get more information.

top
LastMethodSuccess
BOOL CkWebSocket_getLastMethodSuccess(HCkWebSocket cHandle);
void CkWebSocket_putLastMethodSuccess(HCkWebSocket cHandle, BOOL newVal);
Introduced in version 9.5.0.52

Indicate whether the last method call succeeded or failed. A value of TRUE indicates success, a value of FALSE indicates failure. This property is automatically set for method calls. It is not modified by property accesses. The property is automatically set to indicate success for the following types of method calls:

  • Any method that returns a string.
  • Any method returning a Chilkat object, binary bytes, or a date/time.
  • Any method returning a standard boolean status value where success = TRUE and failure = FALSE.
  • Any method returning an integer where failure is defined by a return value less than zero.

Note: Methods that do not fit the above requirements will always set this property equal to TRUE. For example, a method that returns no value (such as a "void" in C++) will technically always succeed.

top
NeedSendPong
BOOL CkWebSocket_getNeedSendPong(HCkWebSocket cHandle);
Introduced in version 9.5.0.70

If TRUE, then a Ping frame was received, but no Pong frame has yet been sent in response. The application should send a Pong frame by calling SendPong as soon as possible.

top
PingAutoRespond
BOOL CkWebSocket_getPingAutoRespond(HCkWebSocket cHandle);
void CkWebSocket_putPingAutoRespond(HCkWebSocket cHandle, BOOL newVal);
Introduced in version 9.5.0.70

If TRUE, then a Pong frame is automatically sent when a Ping frame is received. If set to FALSE, then the application may check the NeedSendPong property to determine if a Pong response is needed, and if so, may call the SendPong method to send a Pong.

Note: If this property is TRUE, then the ReadFrame method will auto-consume incoming Ping frames. In other words, ReadFrame will continue with reading the next incoming frame (thus Ping frames will never be returned to the application). This relieves the application from having to worry about receiving and handling spurious Ping frames.

The default value is TRUE.

top
PongAutoConsume
BOOL CkWebSocket_getPongAutoConsume(HCkWebSocket cHandle);
void CkWebSocket_putPongAutoConsume(HCkWebSocket cHandle, BOOL newVal);
Introduced in version 9.5.0.70

If TRUE, then incoming Pong frames are automatically consumed, and a call to ReadFrame will continue reading until it receives a non-Pong frame. The PongConsumed property can be checked to see if the last ReadFrame method call auto-consumed a Pong frame.

The default value is TRUE.

top
PongConsumed
BOOL CkWebSocket_getPongConsumed(HCkWebSocket cHandle);
Introduced in version 9.5.0.70

Is TRUE if the last call to ReadFrame auto-consumed a Pong frame. This property is reset to FALSE each time a ReadFrame method is called, and will get set to TRUE if (1) the PongAutoConsume property is TRUE and (2) a Pong frame was consumed within the ReadFrame method.

The purpose of PongAutoConsume and PongConsumed is to eliminate the concern for unanticipated Pong frames in the stream. In the websocket protocol, both sides (client and server) may send Pong frames at any time. In addition, if a Ping frame is sent, the corresponding Pong response frame can arrive at some unanticipated point later in the conversation. It's also possible, if several Ping frames are sent, that a Pong response frame is only sent for the most recent Ping frame. The default behavior of Chilkat's WebSocket API is to auto-consume incoming Pong frames and set this property to TRUE. This allows the application to call a ReadFrame method for whatever application data frame it may be expecting, without needing to be concerned if the next incoming frame is a Pong frame.

top
ReadFrameFailReason
int CkWebSocket_getReadFrameFailReason(HCkWebSocket cHandle);
Introduced in version 9.5.0.70

If the ReadFrame method returns FALSE, this property holds the fail reason. It can have one of the following values:

0 - No failure.
1 - Read Timeout.
2 - Aborted by Application Callback.
3 - Fatal Socket Error (Lost Connection).
4 - Received invalid WebSocket frame bytes.
99 - A catch-all for any unknown failure.  (Should not ever occur.  If it does, contact Chilkat.)

top
Utf8
BOOL CkWebSocket_getUtf8(HCkWebSocket cHandle);
void CkWebSocket_putUtf8(HCkWebSocket cHandle, BOOL newVal);

When set to TRUE, all "const char *" arguments are interpreted as utf-8 strings. If set to FALSE (the default), then "const char *" arguments are interpreted as ANSI strings. Also, when set to TRUE, and Chilkat method returning a "const char *" is returning the utf-8 representation. If set to FALSE, all "const char *" return values are ANSI strings.

top
VerboseLogging
BOOL CkWebSocket_getVerboseLogging(HCkWebSocket cHandle);
void CkWebSocket_putVerboseLogging(HCkWebSocket cHandle, BOOL newVal);

If set to TRUE, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is FALSE. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.

top
Version
void CkWebSocket_getVersion(HCkWebSocket cHandle, HCkString retval);
const char *CkWebSocket_version(HCkWebSocket cHandle);

Version of the component/library, such as "9.5.0.63"

top

Methods

AddClientHeaders
BOOL CkWebSocket_AddClientHeaders(HCkWebSocket cHandle);
Introduced in version 9.5.0.70

Adds the required WebSocket client-side open handshake headers. The headers specifically added to the previously specified REST object (in the call to UseConnection) are:

Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: ...
Sec-WebSocket-Version: 13

Returns TRUE for success, FALSE for failure.

More Information and Examples
top
CloseConnection
BOOL CkWebSocket_CloseConnection(HCkWebSocket cHandle);
Introduced in version 9.5.0.70

Forcibly closes the underlying connection. This is a non-clean way to close the connection, but may be used if needed. The clean way to close a websocket is to send a Close frame, and then receive the Close response.

Returns TRUE for success, FALSE for failure.

top
GetFrameData
BOOL CkWebSocket_GetFrameData(HCkWebSocket cHandle, HCkString outStr);
const char *CkWebSocket_getFrameData(HCkWebSocket cHandle);
Introduced in version 9.5.0.70

Returns the accumulated received frame data as a string. Calling GetFrameData clears the internal receive buffer.

Returns TRUE for success, FALSE for failure.

top
GetFrameDataBd
BOOL CkWebSocket_GetFrameDataBd(HCkWebSocket cHandle, HCkBinData binData);
Introduced in version 9.5.0.70

Returns the accumulated received frame data in a BinData object. The received data is appended to the binData.

Calling this method clears the internal receive buffer.

More Information and Examples
top
GetFrameDataSb
BOOL CkWebSocket_GetFrameDataSb(HCkWebSocket cHandle, HCkStringBuilder sb);
Introduced in version 9.5.0.70

Returns the accumulated received frame data in a StringBuilder object. The received data is appended to the sb.

Calling this method clears the internal receive buffer.

top
PollDataAvailable
BOOL CkWebSocket_PollDataAvailable(HCkWebSocket cHandle);
Introduced in version 9.5.0.70

Check to see if data is available for reading on the websocket. Returns TRUE if data is waiting and FALSE if no data is waiting to be read.

top
ReadFrame
BOOL CkWebSocket_ReadFrame(HCkWebSocket cHandle);
Introduced in version 9.5.0.70

Reads a single frame from the connected websocket. If a frame was successfuly received, then the following properties are set: FrameOpcode, FrameDataLen, FinalFrame, and the received frame data can be retrieved by calling GetFrameData, GetFrameDataSb, or GetFrameDataBd.

Returns TRUE for success, FALSE for failure.

top
ReadFrameAsync (1)
HCkTask CkWebSocket_ReadFrameAsync(HCkWebSocket cHandle);
Introduced in version 9.5.0.70

Creates an asynchronous task to call the ReadFrame method with the arguments provided. (Async methods are available starting in Chilkat v9.5.0.52.)

Returns NULL on failure

top
SaveLastError
BOOL CkWebSocket_SaveLastError(HCkWebSocket cHandle, const char *path);

Saves the last-error information (the contents of LastErrorXml) to an XML formatted file.

Returns TRUE for success, FALSE for failure.

top
SendClose
BOOL CkWebSocket_SendClose(HCkWebSocket cHandle, BOOL includeStatus, int statusCode, const char *reason);
Introduced in version 9.5.0.70

Sends a Close control frame. If includeStatus is TRUE, then the statusCode is sent in the application data part of the Close frame. A Close reason may be provided only if includeStatus is TRUE. If this Close was sent to satisfy an already-received Close frame, then the underlying connection will also automatically be closed.

Note: If a status code and reason are provided, the utf-8 representation of the reason string must be 123 bytes or less. Chilkat will automatically truncate the reason to 123 bytes if necessary. Also, the status code must be an integer in the range 0 to 16383.

The WebSocket protocol specifies some pre-defined status codes at WebSocket Status Codes. For a normal closure, a status code value of 1000 should be used. The reason can be any string, as long as it is 123 bytes or less.

Returns TRUE for success, FALSE for failure.

top
SendCloseAsync (1)
HCkTask CkWebSocket_SendCloseAsync(HCkWebSocket cHandle, BOOL includeStatus, int statusCode, const char *reason);
Introduced in version 9.5.0.70

Creates an asynchronous task to call the SendClose method with the arguments provided. (Async methods are available starting in Chilkat v9.5.0.52.)

Returns NULL on failure

top
SendFrame
BOOL CkWebSocket_SendFrame(HCkWebSocket cHandle, const char *stringToSend, BOOL finalFrame);
Introduced in version 9.5.0.70

Sends a single data frame containing a string. If this is the final frame in a message, then finalFrame should be set to TRUE. Otherwise set finalFrame equal to FALSE.

Returns TRUE for success, FALSE for failure.

top
SendFrameAsync (1)
HCkTask CkWebSocket_SendFrameAsync(HCkWebSocket cHandle, const char *stringToSend, BOOL finalFrame);
Introduced in version 9.5.0.70

Creates an asynchronous task to call the SendFrame method with the arguments provided. (Async methods are available starting in Chilkat v9.5.0.52.)

Returns NULL on failure

top
SendFrameBd
BOOL CkWebSocket_SendFrameBd(HCkWebSocket cHandle, HCkBinData bdToSend, BOOL finalFrame);
Introduced in version 9.5.0.70

Sends a single data frame containing binary data (the contents of bdToSend). If this is the final frame in a message, then finalFrame should be set to TRUE. Otherwise set finalFrame equal to FALSE.

Returns TRUE for success, FALSE for failure.

More Information and Examples
top
SendFrameBdAsync (1)
HCkTask CkWebSocket_SendFrameBdAsync(HCkWebSocket cHandle, HCkBinData bdToSend, BOOL finalFrame);
Introduced in version 9.5.0.70

Creates an asynchronous task to call the SendFrameBd method with the arguments provided. (Async methods are available starting in Chilkat v9.5.0.52.)

Returns NULL on failure

top
SendFrameSb
BOOL CkWebSocket_SendFrameSb(HCkWebSocket cHandle, HCkStringBuilder sbToSend, BOOL finalFrame);
Introduced in version 9.5.0.70

Sends a single data frame containing a string (the contents of sbToSend). If this is the final frame in a message, then finalFrame should be set to TRUE. Otherwise set finalFrame equal to FALSE.

Returns TRUE for success, FALSE for failure.

top
SendFrameSbAsync (1)
HCkTask CkWebSocket_SendFrameSbAsync(HCkWebSocket cHandle, HCkStringBuilder sbToSend, BOOL finalFrame);
Introduced in version 9.5.0.70

Creates an asynchronous task to call the SendFrameSb method with the arguments provided. (Async methods are available starting in Chilkat v9.5.0.52.)

Returns NULL on failure

top
SendPing
BOOL CkWebSocket_SendPing(HCkWebSocket cHandle, const char *pingData);
Introduced in version 9.5.0.70

Sends a Ping control frame, optionally including text data. If pingData is non-empty, the utf-8 representation of the string must be 125 bytes or less. Chilkat will automatically truncate the pingData to 125 bytes if necessary.

Returns TRUE for success, FALSE for failure.

More Information and Examples
top
SendPingAsync (1)
HCkTask CkWebSocket_SendPingAsync(HCkWebSocket cHandle, const char *pingData);
Introduced in version 9.5.0.70

Creates an asynchronous task to call the SendPing method with the arguments provided. (Async methods are available starting in Chilkat v9.5.0.52.)

Returns NULL on failure

top
SendPong
BOOL CkWebSocket_SendPong(HCkWebSocket cHandle);
Introduced in version 9.5.0.70

Sends a Pong control frame. If this Pong frame is sent to satisfy an unresponded-to Ping frame, then the previously received Ping data is automatically sent in this Pong frame.

Returns TRUE for success, FALSE for failure.

top
SendPongAsync (1)
HCkTask CkWebSocket_SendPongAsync(HCkWebSocket cHandle);
Introduced in version 9.5.0.70

Creates an asynchronous task to call the SendPong method with the arguments provided. (Async methods are available starting in Chilkat v9.5.0.52.)

Returns NULL on failure

top
UseConnection
BOOL CkWebSocket_UseConnection(HCkWebSocket cHandle, HCkRest connection);
Introduced in version 9.5.0.70

Initializes the connection for a WebSocket session. All WebSocket sessions begin with a call to UseConnection. A Chilkat REST object is used for the connection because the WebSocket handshake begins with an HTTP GET request. The Chilkat REST API provides the ability to add custom headers, authentication, etc. to the opening GET handshake. It also provides the ability to establish connections over TLS or SSH and to benefit from the rich set of features already present relating to HTTP proxies, SOCKS proxies, bandwidth throttling, IPv6, socket options, etc.

Returns TRUE for success, FALSE for failure.

top
ValidateServerHandshake
BOOL CkWebSocket_ValidateServerHandshake(HCkWebSocket cHandle);
Introduced in version 9.5.0.70

Called after sending the opening handshake from the Rest object. Validates the server's response to the opening handshake message. If validation is successful, the application may begin sending and receiving data and control frames.

Returns TRUE for success, FALSE for failure.

More Information and Examples
top