Mercurial
comparison playground/main.c @ 142:893d87124d16
please work
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Fri, 09 Jan 2026 13:09:52 -0800 |
| parents | e7899c93da77 |
| children | 6de849867459 7387eec8e7f8 |
comparison
equal
deleted
inserted
replaced
| 141:1c4d8873e846 | 142:893d87124d16 |
|---|---|
| 1 #include <stdlib.h> | 1 #include "seobeo/seobeo.h" |
| 2 #include <stdio.h> | |
| 3 #include <string.h> | |
| 4 | 2 |
| 5 int main() { | 3 void Test_Echo() |
| 6 char *input[5] = {"Hello ", "Foo <<E", "N", "DD>", "Park <END> "}; | 4 { |
| 7 char *key = "<END>"; | 5 printf("\n=== Test: Multiple Messages ===\n"); |
| 8 int key_len = strlen(key); | |
| 9 | 6 |
| 10 char **buffers = malloc(sizeof(char *) * 100); | 7 // Seobeo_Client_Request *foo = Seobeo_Client_Request_Create("http://mrjunejune.com/echo"); |
| 11 int buffer_index = 0; | 8 // Seobeo_Client_Request_Add_Header_Array(foo, "Upgrade: websocket"); |
| 12 int key_ptr = 0; | 9 // Seobeo_Client_Request_Add_Header_Array(foo, "Connection: Upgrade"); |
| 13 for (int i = 0; i < 5; i++) { | 10 // Seobeo_Client_Request_Add_Header_Array(foo, "Sec-WebSocket-Key: asbc3e12bA"); |
| 14 char *packet = input[i]; | 11 // Seobeo_Client_Request_Add_Header_Array(foo, "Sec-WebSocket-Version: 13"); |
| 15 int p_len = strlen(packet); | 12 // Seobeo_Client_Response *foo2 = Seobeo_Client_Request_Execute(foo); |
| 13 // printf("June: %s\n", foo2->body); | |
| 16 | 14 |
| 17 for (int j = 0; j < p_len; j++) { | |
| 18 if (packet[j] == key[key_ptr]) { | |
| 19 key_ptr++; | |
| 20 | |
| 21 // If the WHOLE keyword is found | |
| 22 if (key_ptr == key_len) { | |
| 23 // 1. Print all previous "safe" buffers | |
| 24 // (The ones before the one where the keyword started) | |
| 25 for (int b = 0; b < buffer_index; b++) | |
| 26 printf("%s", buffers[b]); | |
| 27 | |
| 28 // 2. Handle the "Current" packet truncation | |
| 29 // Calculate where the match started in THIS packet | |
| 30 // If key_ptr was satisfied across multiple packets, | |
| 31 // 'j' is the end of the match in the current packet. | |
| 32 int match_end_in_packet = j + 1; | |
| 33 int match_len_in_this_packet = (key_ptr <= match_end_in_packet) ? key_ptr : match_end_in_packet; | |
| 34 | |
| 35 printf("%.*s\n", (match_end_in_packet - match_len_in_this_packet), packet); | |
| 36 | |
| 37 free(buffers); | |
| 38 return 0; | |
| 39 } | |
| 40 } else { | |
| 41 // If a match fails, we must "flush" the buffers we were holding | |
| 42 if (key_ptr > 0) { | |
| 43 for (int b = 0; b < buffer_index; b++) printf("%s", buffers[b]); | |
| 44 buffer_index = 0; | |
| 45 | |
| 46 if (packet[j] == key[0]) key_ptr = 1; | |
| 47 else { | |
| 48 printf("%c", packet[j]); | |
| 49 key_ptr = 0; | |
| 50 } | |
| 51 } else { | |
| 52 printf("%c", packet[j]); | |
| 53 } | |
| 54 } | |
| 55 } | |
| 56 | 15 |
| 57 // If we finish a packet and we are in the middle of a match, buffer it | 16 Seobeo_WebSocket *p_ws = Seobeo_WebSocket_Connect("ws://mrjunejune.com/echo"); |
| 58 if (key_ptr > 0) { | 17 // Seobeo_WebSocket *p_ws = Seobeo_WebSocket_Connect("ws://localhost:6969/echo"); |
| 59 buffers[buffer_index++] = packet; | 18 if (!p_ws) |
| 60 } | 19 { |
| 20 printf("Failed to connect\n"); | |
| 21 return; | |
| 22 } | |
| 23 | |
| 24 const char *messages[] = { | |
| 25 "Message 1", | |
| 26 "Message 2", | |
| 27 "Message 3" | |
| 28 }; | |
| 29 | |
| 30 for (int i = 0; i < 3; i++) | |
| 31 { | |
| 32 printf("Sending: %s\n", messages[i]); | |
| 33 Seobeo_WebSocket_Send_Text(p_ws, messages[i]); | |
| 34 usleep(100000); | |
| 35 } | |
| 36 | |
| 37 printf("Receiving responses...\n"); | |
| 38 int received = 0; | |
| 39 int attempts = 0; | |
| 40 | |
| 41 while (received < 3 && attempts < 200) | |
| 42 { | |
| 43 Seobeo_WebSocket_Message *p_msg = Seobeo_WebSocket_Receive(p_ws); | |
| 44 if (p_msg) | |
| 45 { | |
| 46 if (p_msg->opcode == SEOBEO_WS_OPCODE_TEXT) | |
| 47 { | |
| 48 printf("Response %d: %.*s\n", received + 1, (int)p_msg->length, (char*)p_msg->data); | |
| 49 received++; | |
| 50 } | |
| 51 Seobeo_WebSocket_Message_Destroy(p_msg); | |
| 61 } | 52 } |
| 62 | 53 |
| 63 free(buffers); | 54 usleep(10000); |
| 64 return 0; | 55 attempts++; |
| 56 } | |
| 57 printf("Received %d/%d messages\n", received, 3); | |
| 58 Seobeo_WebSocket_Destroy(p_ws); | |
| 65 } | 59 } |
| 60 | |
| 61 void Test_Chat() | |
| 62 { | |
| 63 printf("\n=== Test: Multiple Messages ===\n"); | |
| 64 | |
| 65 Seobeo_WebSocket *p_ws = Seobeo_WebSocket_Connect("ws://127.0.0.1:8080/chat"); | |
| 66 if (!p_ws) | |
| 67 { | |
| 68 printf("Failed to connect\n"); | |
| 69 return; | |
| 70 } | |
| 71 | |
| 72 const char *messages[] = { | |
| 73 "Message 1", | |
| 74 "Message 2", | |
| 75 "Message 3" | |
| 76 }; | |
| 77 | |
| 78 for (int i = 0; i < 3; i++) | |
| 79 { | |
| 80 printf("Sending: %s\n", messages[i]); | |
| 81 Seobeo_WebSocket_Send_Text(p_ws, messages[i]); | |
| 82 usleep(100000); | |
| 83 } | |
| 84 | |
| 85 printf("Receiving responses...\n"); | |
| 86 int received = 0; | |
| 87 int attempts = 0; | |
| 88 | |
| 89 while (received < 3 && attempts < 200) | |
| 90 { | |
| 91 Seobeo_WebSocket_Message *p_msg = Seobeo_WebSocket_Receive(p_ws); | |
| 92 if (p_msg) | |
| 93 { | |
| 94 if (p_msg->opcode == SEOBEO_WS_OPCODE_TEXT) | |
| 95 { | |
| 96 printf("Response %d: %.*s\n", received + 1, (int)p_msg->length, (char*)p_msg->data); | |
| 97 received++; | |
| 98 } | |
| 99 Seobeo_WebSocket_Message_Destroy(p_msg); | |
| 100 } | |
| 101 | |
| 102 usleep(10000); | |
| 103 attempts++; | |
| 104 } | |
| 105 printf("Received %d/%d messages\n", received, 3); | |
| 106 Seobeo_WebSocket_Destroy(p_ws); | |
| 107 } | |
| 108 | |
| 109 int main(int argc, char *argv[]) | |
| 110 { | |
| 111 | |
| 112 Test_Echo(); | |
| 113 } |