Mercurial
changeset 142:893d87124d16
please work
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Fri, 09 Jan 2026 13:09:52 -0800 |
| parents | 1c4d8873e846 |
| children | 276bb3555034 |
| files | playground/main.c seobeo/s_websocket_server.c |
| diffstat | 2 files changed, 109 insertions(+), 58 deletions(-) [+] |
line wrap: on
line diff
--- a/playground/main.c Fri Jan 09 12:53:18 2026 -0800 +++ b/playground/main.c Fri Jan 09 13:09:52 2026 -0800 @@ -1,65 +1,113 @@ -#include <stdlib.h> -#include <stdio.h> -#include <string.h> +#include "seobeo/seobeo.h" + +void Test_Echo() +{ + printf("\n=== Test: Multiple Messages ===\n"); -int main() { - char *input[5] = {"Hello ", "Foo <<E", "N", "DD>", "Park <END> "}; - char *key = "<END>"; - int key_len = strlen(key); + // Seobeo_Client_Request *foo = Seobeo_Client_Request_Create("http://mrjunejune.com/echo"); + // Seobeo_Client_Request_Add_Header_Array(foo, "Upgrade: websocket"); + // Seobeo_Client_Request_Add_Header_Array(foo, "Connection: Upgrade"); + // Seobeo_Client_Request_Add_Header_Array(foo, "Sec-WebSocket-Key: asbc3e12bA"); + // Seobeo_Client_Request_Add_Header_Array(foo, "Sec-WebSocket-Version: 13"); + // Seobeo_Client_Response *foo2 = Seobeo_Client_Request_Execute(foo); + // printf("June: %s\n", foo2->body); - char **buffers = malloc(sizeof(char *) * 100); - int buffer_index = 0; - int key_ptr = 0; - for (int i = 0; i < 5; i++) { - char *packet = input[i]; - int p_len = strlen(packet); + + Seobeo_WebSocket *p_ws = Seobeo_WebSocket_Connect("ws://mrjunejune.com/echo"); + // Seobeo_WebSocket *p_ws = Seobeo_WebSocket_Connect("ws://localhost:6969/echo"); + if (!p_ws) + { + printf("Failed to connect\n"); + return; + } - for (int j = 0; j < p_len; j++) { - if (packet[j] == key[key_ptr]) { - key_ptr++; - - // If the WHOLE keyword is found - if (key_ptr == key_len) { - // 1. Print all previous "safe" buffers - // (The ones before the one where the keyword started) - for (int b = 0; b < buffer_index; b++) - printf("%s", buffers[b]); - - // 2. Handle the "Current" packet truncation - // Calculate where the match started in THIS packet - // If key_ptr was satisfied across multiple packets, - // 'j' is the end of the match in the current packet. - int match_end_in_packet = j + 1; - int match_len_in_this_packet = (key_ptr <= match_end_in_packet) ? key_ptr : match_end_in_packet; - - printf("%.*s\n", (match_end_in_packet - match_len_in_this_packet), packet); - - free(buffers); - return 0; - } - } else { - // If a match fails, we must "flush" the buffers we were holding - if (key_ptr > 0) { - for (int b = 0; b < buffer_index; b++) printf("%s", buffers[b]); - buffer_index = 0; - - if (packet[j] == key[0]) key_ptr = 1; - else { - printf("%c", packet[j]); - key_ptr = 0; - } - } else { - printf("%c", packet[j]); - } - } - } + const char *messages[] = { + "Message 1", + "Message 2", + "Message 3" + }; + + for (int i = 0; i < 3; i++) + { + printf("Sending: %s\n", messages[i]); + Seobeo_WebSocket_Send_Text(p_ws, messages[i]); + usleep(100000); + } - // If we finish a packet and we are in the middle of a match, buffer it - if (key_ptr > 0) { - buffers[buffer_index++] = packet; - } + printf("Receiving responses...\n"); + int received = 0; + int attempts = 0; + + while (received < 3 && attempts < 200) + { + Seobeo_WebSocket_Message *p_msg = Seobeo_WebSocket_Receive(p_ws); + if (p_msg) + { + if (p_msg->opcode == SEOBEO_WS_OPCODE_TEXT) + { + printf("Response %d: %.*s\n", received + 1, (int)p_msg->length, (char*)p_msg->data); + received++; + } + Seobeo_WebSocket_Message_Destroy(p_msg); } - free(buffers); - return 0; + usleep(10000); + attempts++; + } + printf("Received %d/%d messages\n", received, 3); + Seobeo_WebSocket_Destroy(p_ws); } + +void Test_Chat() +{ + printf("\n=== Test: Multiple Messages ===\n"); + + Seobeo_WebSocket *p_ws = Seobeo_WebSocket_Connect("ws://127.0.0.1:8080/chat"); + if (!p_ws) + { + printf("Failed to connect\n"); + return; + } + + const char *messages[] = { + "Message 1", + "Message 2", + "Message 3" + }; + + for (int i = 0; i < 3; i++) + { + printf("Sending: %s\n", messages[i]); + Seobeo_WebSocket_Send_Text(p_ws, messages[i]); + usleep(100000); + } + + printf("Receiving responses...\n"); + int received = 0; + int attempts = 0; + + while (received < 3 && attempts < 200) + { + Seobeo_WebSocket_Message *p_msg = Seobeo_WebSocket_Receive(p_ws); + if (p_msg) + { + if (p_msg->opcode == SEOBEO_WS_OPCODE_TEXT) + { + printf("Response %d: %.*s\n", received + 1, (int)p_msg->length, (char*)p_msg->data); + received++; + } + Seobeo_WebSocket_Message_Destroy(p_msg); + } + + usleep(10000); + attempts++; + } + printf("Received %d/%d messages\n", received, 3); + Seobeo_WebSocket_Destroy(p_ws); +} + +int main(int argc, char *argv[]) +{ + + Test_Echo(); +}
--- a/seobeo/s_websocket_server.c Fri Jan 09 12:53:18 2026 -0800 +++ b/seobeo/s_websocket_server.c Fri Jan 09 13:09:52 2026 -0800 @@ -60,6 +60,9 @@ boolean Seobeo_WebSocket_Server_Handle_Upgrade(Seobeo_Handle *p_handle, Seobeo_Request_Entry *p_req_map, const char *path) { + for (int i = 0; i < Dowa_Array_Length(p_req_map); i++) + printf("DELETE ME PLEASE Key: %s Value: %s\n", p_req_map[i].key, p_req_map[i].value); + void *p_upgrade_kv = Dowa_HashMap_Get_Ptr(p_req_map, "Upgrade"); if (!p_upgrade_kv) return FALSE;