Mercurial
comparison seobeo/tests/seobeo_web_server_test.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 | |
| 3 pid_t start_test_server(const char *server_binary) | |
| 4 { | |
| 5 pid_t server_pid = fork(); | |
| 6 | |
| 7 if (server_pid < 0) | |
| 8 { | |
| 9 perror("fork"); | |
| 10 return -1; | |
| 11 } | |
| 12 | |
| 13 if (server_pid == 0) | |
| 14 { | |
| 15 printf("Starting server on port 8000...\n"); | |
| 16 execlp(server_binary, NULL); | |
| 17 perror("execl failed"); | |
| 18 exit(1); | |
| 19 } | |
| 20 | |
| 21 printf("Server started (PID: %d)\n", server_pid); | |
| 22 | |
| 23 usleep(100000); | |
| 24 int status; | |
| 25 pid_t result = waitpid(server_pid, &status, WNOHANG); | |
| 26 if (result != 0) | |
| 27 { | |
| 28 if (WIFEXITED(status)) | |
| 29 { | |
| 30 fprintf(stderr, "Server exited immediately with code: %d\n", WEXITSTATUS(status)); | |
| 31 } | |
| 32 else if (WIFSIGNALED(status)) | |
| 33 { | |
| 34 fprintf(stderr, "Server was killed by signal: %d\n", WTERMSIG(status)); | |
| 35 } | |
| 36 return -1; | |
| 37 } | |
| 38 | |
| 39 sleep(2); | |
| 40 printf("Server ready\n\n"); | |
| 41 | |
| 42 return server_pid; | |
| 43 } | |
| 44 | |
| 45 void Test_Echo() | |
| 46 { | |
| 47 printf("\n=== Test: Multiple Messages ===\n"); | |
| 48 | |
| 49 Seobeo_WebSocket *p_ws = Seobeo_WebSocket_Connect("ws://127.0.0.1:8080/echo"); | |
| 50 if (!p_ws) | |
| 51 { | |
| 52 printf("Failed to connect\n"); | |
| 53 return; | |
| 54 } | |
| 55 | |
| 56 const char *messages[] = { | |
| 57 "Message 1", | |
| 58 "Message 2", | |
| 59 "Message 3" | |
| 60 }; | |
| 61 | |
| 62 for (int i = 0; i < 3; i++) | |
| 63 { | |
| 64 printf("Sending: %s\n", messages[i]); | |
| 65 Seobeo_WebSocket_Send_Text(p_ws, messages[i]); | |
| 66 usleep(100000); | |
| 67 } | |
| 68 | |
| 69 printf("Receiving responses...\n"); | |
| 70 int received = 0; | |
| 71 int attempts = 0; | |
| 72 | |
| 73 while (received < 3 && attempts < 200) | |
| 74 { | |
| 75 Seobeo_WebSocket_Message *p_msg = Seobeo_WebSocket_Receive(p_ws); | |
| 76 if (p_msg) | |
| 77 { | |
| 78 if (p_msg->opcode == SEOBEO_WS_OPCODE_TEXT) | |
| 79 { | |
| 80 printf("Response %d: %.*s\n", received + 1, (int)p_msg->length, (char*)p_msg->data); | |
| 81 received++; | |
| 82 } | |
| 83 Seobeo_WebSocket_Message_Destroy(p_msg); | |
| 84 } | |
| 85 | |
| 86 usleep(10000); | |
| 87 attempts++; | |
| 88 } | |
| 89 printf("Received %d/%d messages\n", received, 3); | |
| 90 Seobeo_WebSocket_Destroy(p_ws); | |
| 91 } | |
| 92 | |
| 93 void Test_Chat() | |
| 94 { | |
| 95 printf("\n=== Test: Multiple Messages ===\n"); | |
| 96 | |
| 97 Seobeo_WebSocket *p_ws = Seobeo_WebSocket_Connect("ws://127.0.0.1:8080/chat"); | |
| 98 if (!p_ws) | |
| 99 { | |
| 100 printf("Failed to connect\n"); | |
| 101 return; | |
| 102 } | |
| 103 | |
| 104 const char *messages[] = { | |
| 105 "Message 1", | |
| 106 "Message 2", | |
| 107 "Message 3" | |
| 108 }; | |
| 109 | |
| 110 for (int i = 0; i < 3; i++) | |
| 111 { | |
| 112 printf("Sending: %s\n", messages[i]); | |
| 113 Seobeo_WebSocket_Send_Text(p_ws, messages[i]); | |
| 114 usleep(100000); | |
| 115 } | |
| 116 | |
| 117 printf("Receiving responses...\n"); | |
| 118 int received = 0; | |
| 119 int attempts = 0; | |
| 120 | |
| 121 while (received < 3 && attempts < 200) | |
| 122 { | |
| 123 Seobeo_WebSocket_Message *p_msg = Seobeo_WebSocket_Receive(p_ws); | |
| 124 if (p_msg) | |
| 125 { | |
| 126 if (p_msg->opcode == SEOBEO_WS_OPCODE_TEXT) | |
| 127 { | |
| 128 printf("Response %d: %.*s\n", received + 1, (int)p_msg->length, (char*)p_msg->data); | |
| 129 received++; | |
| 130 } | |
| 131 Seobeo_WebSocket_Message_Destroy(p_msg); | |
| 132 } | |
| 133 | |
| 134 usleep(10000); | |
| 135 attempts++; | |
| 136 } | |
| 137 printf("Received %d/%d messages\n", received, 3); | |
| 138 Seobeo_WebSocket_Destroy(p_ws); | |
| 139 } | |
| 140 | |
| 141 void stop_test_server(pid_t server_pid) | |
| 142 { | |
| 143 if (server_pid > 0) | |
| 144 { | |
| 145 printf("\nStopping server (PID: %d)...\n", server_pid); | |
| 146 kill(server_pid, SIGTERM); | |
| 147 waitpid(server_pid, NULL, 0); | |
| 148 printf("Server stopped\n"); | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 int main(int argc, char *argv[]) | |
| 153 { | |
| 154 const char *server_binary = "./websocket_server_example"; | |
| 155 if (argc > 1) | |
| 156 { | |
| 157 server_binary = argv[1]; | |
| 158 } | |
| 159 | |
| 160 pid_t server_pid = start_test_server(server_binary); | |
| 161 | |
| 162 Test_Echo(); | |
| 163 Test_Chat(); | |
| 164 | |
| 165 stop_test_server(server_pid); | |
| 166 } |