comparison seobeo/tests/seobeo_websocket_test.c @ 120:cbbf78b17cfa

[Seobeo][Websocket] Created Web socket client logic.
author June Park <parkjune1995@gmail.com>
date Thu, 08 Jan 2026 03:19:59 -0800
parents
children
comparison
equal deleted inserted replaced
119:c39582f937e5 120:cbbf78b17cfa
1 #include "seobeo/seobeo.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5
6 void Test_WebSocket_Echo()
7 {
8 printf("\n=== Test: WebSocket Echo Server ===\n");
9
10 Seobeo_WebSocket *p_ws = Seobeo_WebSocket_Connect("wss://echo.websocket.org");
11 if (!p_ws)
12 {
13 printf("Failed to connect to WebSocket server\n");
14 return;
15 }
16
17 printf("Connected to WebSocket server\n");
18
19 const char *test_message = "Hello, WebSocket!";
20 printf("Sending: %s\n", test_message);
21
22 if (Seobeo_WebSocket_Send_Text(p_ws, test_message) < 0)
23 {
24 printf("Failed to send message\n");
25 Seobeo_WebSocket_Destroy(p_ws);
26 return;
27 }
28
29 printf("Waiting for echo response...\n");
30
31 int attempts = 0;
32 while (attempts < 100)
33 {
34 Seobeo_WebSocket_Message *p_msg = Seobeo_WebSocket_Receive(p_ws);
35 if (p_msg)
36 {
37 if (p_msg->opcode == SEOBEO_WS_OPCODE_TEXT)
38 {
39 printf("Received text: %.*s\n", (int)p_msg->length, (char*)p_msg->data);
40 }
41 else if (p_msg->opcode == SEOBEO_WS_OPCODE_BINARY)
42 {
43 printf("Received binary data (%zu bytes)\n", p_msg->length);
44 }
45
46 Seobeo_WebSocket_Message_Destroy(p_msg);
47 break;
48 }
49
50 usleep(10000);
51 attempts++;
52 }
53
54 if (attempts >= 100)
55 printf("Timeout waiting for response\n");
56
57 Seobeo_WebSocket_Close(p_ws, 1000, "Test complete");
58 Seobeo_WebSocket_Destroy(p_ws);
59
60 printf("WebSocket test completed\n");
61 }
62
63 void Test_WebSocket_Multiple_Messages()
64 {
65 printf("\n=== Test: Multiple Messages ===\n");
66
67 Seobeo_WebSocket *p_ws = Seobeo_WebSocket_Connect("wss://echo.websocket.org");
68 if (!p_ws)
69 {
70 printf("Failed to connect\n");
71 return;
72 }
73
74 const char *messages[] = {
75 "Message 1",
76 "Message 2",
77 "Message 3"
78 };
79
80 for (int i = 0; i < 3; i++)
81 {
82 printf("Sending: %s\n", messages[i]);
83 Seobeo_WebSocket_Send_Text(p_ws, messages[i]);
84 usleep(100000);
85 }
86
87 printf("Receiving responses...\n");
88 int received = 0;
89 int attempts = 0;
90
91 while (received < 3 && attempts < 200)
92 {
93 Seobeo_WebSocket_Message *p_msg = Seobeo_WebSocket_Receive(p_ws);
94 if (p_msg)
95 {
96 if (p_msg->opcode == SEOBEO_WS_OPCODE_TEXT)
97 {
98 printf("Response %d: %.*s\n", received + 1, (int)p_msg->length, (char*)p_msg->data);
99 received++;
100 }
101 Seobeo_WebSocket_Message_Destroy(p_msg);
102 }
103
104 usleep(10000);
105 attempts++;
106 }
107
108 printf("Received %d/%d messages\n", received, 3);
109
110 Seobeo_WebSocket_Destroy(p_ws);
111 }
112
113 void Test_WebSocket_Binary_Data()
114 {
115 printf("\n=== Test: Binary Data ===\n");
116
117 Seobeo_WebSocket *p_ws = Seobeo_WebSocket_Connect("wss://echo.websocket.org");
118 if (!p_ws)
119 {
120 printf("Failed to connect\n");
121 return;
122 }
123
124 uint8 binary_data[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};
125 printf("Sending %zu bytes of binary data\n", sizeof(binary_data));
126
127 if (Seobeo_WebSocket_Send_Binary(p_ws, binary_data, sizeof(binary_data)) < 0)
128 {
129 printf("Failed to send binary data\n");
130 Seobeo_WebSocket_Destroy(p_ws);
131 return;
132 }
133
134 int attempts = 0;
135 while (attempts < 100)
136 {
137 Seobeo_WebSocket_Message *p_msg = Seobeo_WebSocket_Receive(p_ws);
138 if (p_msg)
139 {
140 if (p_msg->opcode == SEOBEO_WS_OPCODE_BINARY)
141 {
142 printf("Received %zu bytes of binary data: ", p_msg->length);
143 for (size_t i = 0; i < p_msg->length && i < 16; i++)
144 printf("%02X ", p_msg->data[i]);
145 printf("\n");
146 }
147
148 Seobeo_WebSocket_Message_Destroy(p_msg);
149 break;
150 }
151
152 usleep(10000);
153 attempts++;
154 }
155
156 Seobeo_WebSocket_Destroy(p_ws);
157 }
158
159 void Test_WebSocket_Ping_Pong()
160 {
161 printf("\n=== Test: Ping/Pong ===\n");
162
163 Seobeo_WebSocket *p_ws = Seobeo_WebSocket_Connect("wss://echo.websocket.org");
164 if (!p_ws)
165 {
166 printf("Failed to connect\n");
167 return;
168 }
169
170 printf("Sending ping...\n");
171 if (Seobeo_WebSocket_Send_Ping(p_ws, "ping payload") < 0)
172 {
173 printf("Failed to send ping\n");
174 Seobeo_WebSocket_Destroy(p_ws);
175 return;
176 }
177
178 printf("Ping sent (pong should be handled automatically)\n");
179
180 usleep(500000);
181
182 Seobeo_WebSocket_Destroy(p_ws);
183 }
184
185 int main()
186 {
187 printf("=== Seobeo WebSocket Tests ===\n");
188
189 Test_WebSocket_Echo();
190 Test_WebSocket_Multiple_Messages();
191 Test_WebSocket_Binary_Data();
192 Test_WebSocket_Ping_Pong();
193
194 printf("\n=== All Tests Complete ===\n");
195 return 0;
196 }