comparison playground/main.c @ 147:6de849867459 hg-web

[HgWeb] Updated logic to use Seobeo Client.
author June Park <parkjune1995@gmail.com>
date Fri, 09 Jan 2026 18:39:34 -0800
parents 893d87124d16
children 827c6ac504cd
comparison
equal deleted inserted replaced
146:8e56f800b7e4 147:6de849867459
1 #include "seobeo/seobeo.h" 1 #include "seobeo/seobeo.h"
2
3 void Test_Echo()
4 {
5 printf("\n=== Test: Multiple Messages ===\n");
6
7 // Seobeo_Client_Request *foo = Seobeo_Client_Request_Create("http://mrjunejune.com/echo");
8 // Seobeo_Client_Request_Add_Header_Array(foo, "Upgrade: websocket");
9 // Seobeo_Client_Request_Add_Header_Array(foo, "Connection: Upgrade");
10 // Seobeo_Client_Request_Add_Header_Array(foo, "Sec-WebSocket-Key: asbc3e12bA");
11 // Seobeo_Client_Request_Add_Header_Array(foo, "Sec-WebSocket-Version: 13");
12 // Seobeo_Client_Response *foo2 = Seobeo_Client_Request_Execute(foo);
13 // printf("June: %s\n", foo2->body);
14
15
16 Seobeo_WebSocket *p_ws = Seobeo_WebSocket_Connect("ws://mrjunejune.com/echo");
17 // Seobeo_WebSocket *p_ws = Seobeo_WebSocket_Connect("ws://localhost:6969/echo");
18 if (!p_ws)
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);
52 }
53
54 usleep(10000);
55 attempts++;
56 }
57 printf("Received %d/%d messages\n", received, 3);
58 Seobeo_WebSocket_Destroy(p_ws);
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 2
109 int main(int argc, char *argv[]) 3 int main(int argc, char *argv[])
110 { 4 {
111 5 Seobeo_Client_Request *p_req = Seobeo_Client_Request_Create("http://127.0.0.1:4444/file/tip?style=json");
112 Test_Echo(); 6 Seobeo_Client_Request_Add_Header_Array(p_req, "User-Agent: Seobeo/1.0 (Array Mode)");
7 Seobeo_Client_Request_Add_Header_Array(p_req, "Accept: application/json");
8 Seobeo_Client_Request_Add_Header_Array(p_req, "X-Test-Header: TestValue");
9 Seobeo_Client_Response *p_resp = Seobeo_Client_Request_Execute(p_req);
10 if (p_resp)
11 {
12 printf("Status: %d\n", p_resp->status_code);
13 if (p_resp->body)
14 printf("Response:\n%s\n", p_resp->body);
15 Seobeo_Client_Response_Destroy(p_resp);
16 }
17 else
18 printf("Request failed\n");
19 Seobeo_Client_Request_Destroy(p_req);
113 } 20 }