|
Csilk 0.2.1
A lightweight, high-performance C HTTP web framework
|
Server-Sent Events (SSE) implementation. More...
#include <stdint.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <uv.h>#include "csilk/csilk.h"#include "csilk/core/internal.h"
Functions | |
| static void | on_sse_write (uv_write_t *req, int status) |
| SSE write completion callback. | |
| void | csilk_sse_init (csilk_ctx_t *c) |
| Initialize an SSE connection with proper headers and status. | |
| void | csilk_sse_send (csilk_ctx_t *c, const char *event, const char *data) |
| Send an SSE event (optional event type + data). | |
| void | csilk_sse_close (csilk_ctx_t *c) |
| Close the SSE connection. | |
Server-Sent Events (SSE) implementation.
| void csilk_sse_close | ( | csilk_ctx_t * | c | ) |
Close the SSE connection.
Closes the underlying libuv stream handle for the SSE client connection. Performs a graceful close via uv_close(). Any pending write requests will complete before the handle is fully closed.
| c | The request context (must be in SSE mode with an active client). |
| void csilk_sse_init | ( | csilk_ctx_t * | c | ) |
Initialize an SSE connection with proper headers and status.
Initialise a Server-Sent Events connection.
Sets the Content-Type, Cache-Control, Connection, and X-Accel-Buffering headers, marks the context as an SSE connection (is_sse = 1), and writes the raw HTTP 200 OK response headers directly to the client socket.
| c | The request context. |
| void csilk_sse_send | ( | csilk_ctx_t * | c, |
| const char * | event, | ||
| const char * | data | ||
| ) |
Send an SSE event (optional event type + data).
Send an SSE event (or comment) to the client.
Formats and writes a Server-Sent Event message to the client. If an event type is provided, an "event:" line is emitted. The data is written as a "data:" line. Both are terminated by an empty line ("\n") as required by the SSE specification (RFC 8895 ยง2).
| c | The request context (must be in SSE mode). |
| event | Optional event type string (e.g. "message", "update"). May be NULL, in which case no "event:" line is emitted. |
| data | The event payload string. May be NULL (produces a "data:" line with no content). Must not contain embedded "\n\n" sequences unless multi-line data is intended per SSE spec. |
|
static |
SSE write completion callback.
Called by libuv after an asynchronous write to the SSE client socket completes (or fails). On failure, logs the error via CSILK_LOG_E. In either case frees the write request and the associated buffer.
| req | The completed uv_write_t request. req->data points to the heap-allocated buffer that was written. |
| status | 0 on success, negative on error (UV_E* codes). |