NetBurner 3.5.6
PDF Version |
Web Socket functions and defintions. More...
Topics | |
Web Socket Definitions | |
WebSocket Protocol Implementation for Real-Time Bidirectional Communication #include< websockets.h> | |
Classes | |
class | NB::WebSocket |
WebSocket connection management class. More... | |
Functions | |
int | WSPing (int fd, uint32_t len, uint32_t *sentTick) |
Send a WebSocket ping frame to test connection liveness and measure round-trip time. | |
int | WSGetPingReplyTick (int fd, uint32_t *replyTick) |
Retrieve the timestamp of the most recent pong reply received on a WebSocket connection. | |
int | WSWaitForPingReply (int fd, uint32_t timeout) |
Wait for a pong reply to a previously sent ping with a specified timeout. | |
int | WSUpgrade (HTTP_Request *req, int sock) |
Upgrade an HTTP connection to a WebSocket connection. | |
Web Socket functions and defintions.
int WSGetPingReplyTick | ( | int | fd, |
uint32_t * | replyTick ) |
#include <websockets.h>
Retrieve the timestamp of the most recent pong reply received on a WebSocket connection.
Gets the system tick count when the last pong frame was received in response to a ping. This function is used in conjunction with WSPing() to measure round-trip time and verify connection responsiveness. The reply tick is automatically recorded by the WebSocket implementation when a pong frame arrives.
This is useful for:
fd | The file descriptor of the WebSocket connection |
replyTick | Pointer to store the tick count when the last pong was received. If no pong has been received yet, the value is undefined. |
Expand for Example Usage
int WSPing | ( | int | fd, |
uint32_t | len, | ||
uint32_t * | sentTick ) |
#include <websockets.h>
Send a WebSocket ping frame to test connection liveness and measure round-trip time.
Sends a WebSocket ping control frame with optional payload data. The server or client should respond with a pong frame containing the same payload. This function records the tick count when the ping is sent, allowing calculation of round-trip time when the corresponding pong is received.
Ping frames are used to:
fd | The file descriptor of the WebSocket connection |
len | Length of the ping payload data in bytes (typically 0-125 bytes) |
sentTick | Pointer to store the system tick count when the ping was sent. Can be NULL if timestamp recording is not needed. Use this value with the pong response timestamp to calculate round-trip time. |
Returns the number of bytes sent on success, or a negative value on error:
(receivedTick - sentTick) / (TICKS_PER_SECOND / 1000)
Expand for Example Usage
int WSUpgrade | ( | HTTP_Request * | req, |
int | sock ) |
#include <websockets.h>
Upgrade an HTTP connection to a WebSocket connection.
Performs the WebSocket handshake by upgrading an existing HTTP request to the WebSocket protocol. This function validates the upgrade request, sends the appropriate handshake response, and transitions the socket into WebSocket mode. After a successful upgrade, the socket can be used with WebSocket functions like WSRead() and WSWrite().
This function handles:
req | Pointer to the HTTP_Request structure containing the upgrade request. The request must contain valid WebSocket upgrade headers including:
|
sock | The socket file descriptor of the HTTP connection to upgrade. This socket will be transitioned to WebSocket mode on success. |
Expand for Example Usage
int WSWaitForPingReply | ( | int | fd, |
uint32_t | timeout ) |
#include <websockets.h>
Wait for a pong reply to a previously sent ping with a specified timeout.
Blocks execution until a pong frame is received in response to a ping, or until the specified timeout expires. This is a convenience function that combines waiting and timeout detection, simplifying the common pattern of sending a ping and waiting for the corresponding pong response.
This function is useful for:
fd | The file descriptor of the WebSocket connection |
timeout | Maximum time to wait for a pong reply, in system ticks. Use TICKS_PER_SECOND to specify timeout in seconds (e.g., TICKS_PER_SECOND * 5 for a 5-second timeout). A timeout of 0 returns immediately. |
Expand for Example Usage