Mercurial
comparison seobeo/examples/sse_server_example.c @ 257:609d3c6aff4e
[seobeo] Add persistent SSE streams
Co-authored-by: Copilot <[email protected]>
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Tue, 04 Aug 2026 16:49:11 -0700 |
| parents | |
| children | 1f9877b637e9 |
comparison
equal
deleted
inserted
replaced
| 256:30c2196d03d4 | 257:609d3c6aff4e |
|---|---|
| 1 #include "seobeo/seobeo.h" | |
| 2 | |
| 3 #include <unistd.h> | |
| 4 | |
| 5 static void Send_Later(void *p_context) | |
| 6 { | |
| 7 Seobeo_SSE_Stream *p_stream = p_context; | |
| 8 usleep(100000); | |
| 9 if (Seobeo_SSE_Is_Open(p_stream)) | |
| 10 Seobeo_SSE_Send_Data(p_stream, "later"); | |
| 11 } | |
| 12 | |
| 13 static void Release_Stream(void *p_context) | |
| 14 { | |
| 15 Seobeo_SSE_Release(p_context); | |
| 16 } | |
| 17 | |
| 18 static void Events( | |
| 19 Seobeo_SSE_Stream *p_stream, | |
| 20 Seobeo_Request_Entry *p_request, | |
| 21 Dowa_Arena *p_arena) | |
| 22 { | |
| 23 (void)p_request; | |
| 24 (void)p_arena; | |
| 25 Seobeo_SSE_Send_Data(p_stream, "connected"); | |
| 26 if (Seobeo_SSE_Retain(p_stream)) | |
| 27 { | |
| 28 Seobeo_Worker_Result result = Seobeo_Thread_Start_Detached( | |
| 29 Send_Later, | |
| 30 p_stream, | |
| 31 Release_Stream); | |
| 32 if (result != SEOBEO_WORKER_OK) | |
| 33 Seobeo_SSE_Release(p_stream); | |
| 34 } | |
| 35 } | |
| 36 | |
| 37 static Seobeo_Request_Entry *Health( | |
| 38 Seobeo_Request_Entry *p_request, | |
| 39 Dowa_Arena *p_arena) | |
| 40 { | |
| 41 (void)p_request; | |
| 42 Seobeo_Request_Entry *p_response = NULL; | |
| 43 Dowa_HashMap_Push_Arena(p_response, "status", "200", p_arena); | |
| 44 Dowa_HashMap_Push_Arena( | |
| 45 p_response, | |
| 46 "content-type", | |
| 47 "text/plain", | |
| 48 p_arena); | |
| 49 Dowa_HashMap_Push_Arena(p_response, "body", "ok", p_arena); | |
| 50 return p_response; | |
| 51 } | |
| 52 | |
| 53 int main(int argc, char **argv) | |
| 54 { | |
| 55 const char *port = argc > 1 ? argv[1] : "18081"; | |
| 56 Seobeo_Router_Init(); | |
| 57 Seobeo_Router_Register_SSE("/events", Events); | |
| 58 Seobeo_Router_Register("GET", "/health", Health); | |
| 59 int result = Seobeo_Web_Server_Start_On( | |
| 60 "127.0.0.1", | |
| 61 "seobeo/examples", | |
| 62 port, | |
| 63 SEOBEO_MODE_EDGE, | |
| 64 1); | |
| 65 Seobeo_Router_Destroy(); | |
| 66 return result; | |
| 67 } |