Mercurial
comparison seobeo/examples/websocket_server_example.c @ 121:7b1719fa918c
[Seobeo] Added web socket server.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Thu, 08 Jan 2026 06:45:10 -0800 |
| parents | |
| children | 7eb79fd91c7e |
comparison
equal
deleted
inserted
replaced
| 120:cbbf78b17cfa | 121:7b1719fa918c |
|---|---|
| 1 #include "seobeo/seobeo.h" | |
| 2 #include <stdio.h> | |
| 3 #include <stdlib.h> | |
| 4 #include <string.h> | |
| 5 | |
| 6 void Echo_Handler(Seobeo_WebSocket_Server_Connection *p_conn, Seobeo_WebSocket_Message *p_msg, void *p_user_data) | |
| 7 { | |
| 8 (void)p_user_data; | |
| 9 | |
| 10 if (p_msg->opcode == SEOBEO_WS_OPCODE_TEXT) | |
| 11 { | |
| 12 printf("[Echo] Received text: %.*s\n", (int)p_msg->length, (char*)p_msg->data); | |
| 13 Seobeo_WebSocket_Server_Send_Text(p_conn, (char*)p_msg->data); | |
| 14 } | |
| 15 else if (p_msg->opcode == SEOBEO_WS_OPCODE_BINARY) | |
| 16 { | |
| 17 printf("[Echo] Received %zu bytes of binary data\n", p_msg->length); | |
| 18 Seobeo_WebSocket_Server_Send_Binary(p_conn, p_msg->data, p_msg->length); | |
| 19 } | |
| 20 } | |
| 21 | |
| 22 void Chat_Handler(Seobeo_WebSocket_Server_Connection *p_conn, Seobeo_WebSocket_Message *p_msg, void *p_user_data) | |
| 23 { | |
| 24 (void)p_user_data; | |
| 25 | |
| 26 if (p_msg->opcode == SEOBEO_WS_OPCODE_TEXT) | |
| 27 { | |
| 28 char message[2048]; | |
| 29 snprintf(message, sizeof(message), "[%s]: %.*s", p_conn->client_id, (int)p_msg->length, (char*)p_msg->data); | |
| 30 | |
| 31 printf("[Chat] Broadcasting: %s\n", message); | |
| 32 Seobeo_WebSocket_Server_Broadcast_Text(message); | |
| 33 } | |
| 34 } | |
| 35 | |
| 36 void Binary_Handler(Seobeo_WebSocket_Server_Connection *p_conn, Seobeo_WebSocket_Message *p_msg, void *p_user_data) | |
| 37 { | |
| 38 (void)p_user_data; | |
| 39 (void)p_conn; | |
| 40 | |
| 41 if (p_msg->opcode == SEOBEO_WS_OPCODE_BINARY) | |
| 42 { | |
| 43 printf("[Binary] Received %zu bytes, broadcasting to all clients\n", p_msg->length); | |
| 44 Seobeo_WebSocket_Server_Broadcast_Binary(p_msg->data, p_msg->length); | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 int main() | |
| 49 { | |
| 50 printf("=== Seobeo WebSocket Server Example ===\n\n"); | |
| 51 | |
| 52 Seobeo_WebSocket_Server_Init(); | |
| 53 | |
| 54 Seobeo_WebSocket_Server_Register("/echo", Echo_Handler, NULL); | |
| 55 printf("Registered /echo endpoint\n"); | |
| 56 | |
| 57 Seobeo_WebSocket_Server_Register("/chat", Chat_Handler, NULL); | |
| 58 printf("Registered /chat endpoint\n"); | |
| 59 | |
| 60 Seobeo_WebSocket_Server_Register("/binary", Binary_Handler, NULL); | |
| 61 printf("Registered /binary endpoint\n"); | |
| 62 | |
| 63 printf("\nStarting server on port 8080...\n"); | |
| 64 printf("\nTest with:\n"); | |
| 65 printf(" ws://localhost:8080/echo - Echo server\n"); | |
| 66 printf(" ws://localhost:8080/chat - Chat room\n"); | |
| 67 printf(" ws://localhost:8080/binary - Binary data broadcast\n"); | |
| 68 printf("\n"); | |
| 69 | |
| 70 Seobeo_Web_Server_Start(NULL, "8080", SEOBEO_MODE_FORK, 0); | |
| 71 | |
| 72 return 0; | |
| 73 } |