comparison postdog/main.c @ 153:790930d9bb90

[PostDog] Adding websocket to be more usable.
author June Park <parkjune1995@gmail.com>
date Sun, 11 Jan 2026 08:11:24 -0800
parents 7387eec8e7f8
children 3bb45eb67906
comparison
equal deleted inserted replaced
152:7387eec8e7f8 153:790930d9bb90
77 char *url_result_text = NULL; 77 char *url_result_text = NULL;
78 char **url_body_map = NULL; 78 char **url_body_map = NULL;
79 int active_method_dropdown = 0; 79 int active_method_dropdown = 0;
80 int active_input_tab = 0; 80 int active_input_tab = 0;
81 Seobeo_WebSocket *ws = NULL; 81 Seobeo_WebSocket *ws = NULL;
82 boolean WS_BREAK = FALSE;
82 83
83 int CompareHistoryItemsByDate(const void *a, const void *b) { 84 int CompareHistoryItemsByDate(const void *a, const void *b) {
84 HistoryItem *itemA = (HistoryItem *)a; 85 HistoryItem *itemA = (HistoryItem *)a;
85 HistoryItem *itemB = (HistoryItem *)b; 86 HistoryItem *itemB = (HistoryItem *)b;
86 return (itemB->time_modified - itemA->time_modified); 87 return (itemB->time_modified - itemA->time_modified);
288 printf("URL %s\n", url_input_text); 289 printf("URL %s\n", url_input_text);
289 if (Seobeo_WebSocket_Send_Text(ws, url_body_map[active_input_tab]) < 0) 290 if (Seobeo_WebSocket_Send_Text(ws, url_body_map[active_input_tab]) < 0)
290 printf("Failed to send message\n"); 291 printf("Failed to send message\n");
291 292
292 printf("Receiving responses...\n"); 293 printf("Receiving responses...\n");
293
294 int received = 0; 294 int received = 0;
295 while (TRUE) 295 while (TRUE)
296 { 296 {
297 if (WS_BREAK) break;
298
297 Seobeo_WebSocket_Message *p_msg = Seobeo_WebSocket_Receive(ws); 299 Seobeo_WebSocket_Message *p_msg = Seobeo_WebSocket_Receive(ws);
298 if (p_msg) 300 if (p_msg)
299 { 301 {
300 if (p_msg->opcode == SEOBEO_WS_OPCODE_TEXT) 302 if (p_msg->opcode == SEOBEO_WS_OPCODE_TEXT)
301 { 303 {
302 printf("Response %d: %.*s\n", received + 1, (int)p_msg->length, (char*)p_msg->data); 304 printf("Response %d: %.*s\n", received + 1, (int)p_msg->length, (char*)p_msg->data);
303 snprintf(url_result_text, RESULT_BUFFER_LENGTH, "%s", p_msg->data); 305 snprintf(url_result_text + strlen(url_result_text), RESULT_BUFFER_LENGTH - strlen(url_result_text),
306 "\n%s", (char*)p_msg->data);
304 received++; 307 received++;
305 } 308 }
306 Seobeo_WebSocket_Message_Destroy(p_msg); 309 Seobeo_WebSocket_Message_Destroy(p_msg);
307 } 310 }
308 usleep(10000); 311 usleep(10000);
309 } 312 }
310 printf("Received %d/%d messages\n", received, 3); 313 printf("Received %d/%d messages\n", received, 3);
311 } 314 Seobeo_WebSocket_Destroy(ws);
312 315 }
313 void* PostDog_Websocket_Thread(void* arg) 316
314 { 317 void *PostDog_Websocket_Thread(void *arg)
315 PostDog_Websocket_Send(); 318 {
316 printf("Websocket request finished.\n"); 319 PostDog_Websocket_Send();
317 return NULL; 320 printf("Websocket request finished.\n");
318 } 321 return NULL;
319 322 }
320 void Trigger_Async_Send() 323
324 void PostDog_Websocket_Thread_Send()
321 { 325 {
322 pthread_t thread_id; 326 pthread_t thread_id;
323 327
324 if (pthread_create(&thread_id, NULL, PostDog_Websocket_Thread, NULL) != 0) 328 if (pthread_create(&thread_id, NULL, PostDog_Websocket_Thread, NULL) != 0)
325 { 329 {
600 SetWindowState(FLAG_WINDOW_RESIZABLE); 604 SetWindowState(FLAG_WINDOW_RESIZABLE);
601 SetTargetFPS(60); 605 SetTargetFPS(60);
602 606
603 Font customFont = LoadFontEx("postdog/Roboto-Regular.ttf", 20, 0, 0); 607 Font customFont = LoadFontEx("postdog/Roboto-Regular.ttf", 20, 0, 0);
604 GuiSetFont(customFont); 608 GuiSetFont(customFont);
605 GuiSetStyle(DEFAULT, TEXT_SIZE, 10); 609 GuiSetStyle(DEFAULT, TEXT_SIZE, 15);
606 Image logo_original = LoadImage("postdog/epi_all_colors.png"); 610 Image logo_original = LoadImage("postdog/epi_all_colors.png");
607 ImageResize(&logo_original, 60, 60); 611 ImageResize(&logo_original, 60, 60);
608 SetWindowIcon(logo_original); 612 SetWindowIcon(logo_original);
609 Texture2D logo_texture = LoadTextureFromImage(logo_original); 613 Texture2D logo_texture = LoadTextureFromImage(logo_original);
610 UnloadImage(logo_original); 614 UnloadImage(logo_original);
899 Rectangle scrolled_input = AddPadding(input_body, padding * 2); 903 Rectangle scrolled_input = AddPadding(input_body, padding * 2);
900 scrolled_input.y += input_body_scroll_offset; 904 scrolled_input.y += input_body_scroll_offset;
901 scrolled_input.height = MAX_SCROLL_HEIGHT; 905 scrolled_input.height = MAX_SCROLL_HEIGHT;
902 if (active_input_tab != TAB_WEBSOCKET) 906 if (active_input_tab != TAB_WEBSOCKET)
903 { 907 {
908 WS_BREAK = TRUE;
904 if (JUNE_GuiTextBox(scrolled_input, url_body_map[active_input_tab], DEFAULT_TEXT_BUFFER_LENGTH, input_body_bool)) 909 if (JUNE_GuiTextBox(scrolled_input, url_body_map[active_input_tab], DEFAULT_TEXT_BUFFER_LENGTH, input_body_bool))
905 input_body_bool = !input_body_bool; 910 input_body_bool = !input_body_bool;
906 } 911 }
907 else 912 else
908 { 913 {
909 boolean temp = true; 914 boolean temp = true;
910 if (GuiTextInputBox(input_body, "send message", "connected", 915 WS_BREAK = FALSE;
911 "send", url_body_map[active_input_tab], BODY_BUFFER_LENGTH, &temp) == 1) 916 if (GuiTextInputBox(
912 Trigger_Async_Send(); 917 input_body,
918 "send message", ws != NULL ? "connected" : "start messaging",
919 "send", url_body_map[active_input_tab], BODY_BUFFER_LENGTH, &temp) == 1)
920 PostDog_Websocket_Thread_Send();
913 } 921 }
914 EndScissorMode(); 922 EndScissorMode();
915 923
916 if (input_body_scroll_offset < 0) { 924 if (input_body_scroll_offset < 0) {
917 float scrollbar_height = 10; 925 float scrollbar_height = 10;