Mercurial
annotate playground/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 | 827c6ac504cd 8d17f6e6e290 |
| rev | line source |
|---|---|
| 142 | 1 #include "seobeo/seobeo.h" |
| 2 | |
| 3 void Test_Echo() | |
| 4 { | |
| 5 printf("\n=== Test: Multiple Messages ===\n"); | |
|
152
7387eec8e7f8
[Postdog] Updated to use Seobeo_Client instead of CURL. Updated to handle websocket connection.
June Park <parkjune1995@gmail.com>
parents:
142
diff
changeset
|
6 Seobeo_WebSocket *p_ws = Seobeo_WebSocket_Connect("wss://mrjunejune.com/echo"); |
| 142 | 7 if (!p_ws) |
| 8 { | |
| 9 printf("Failed to connect\n"); | |
| 10 return; | |
| 11 } | |
|
71
75de5903355c
Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents:
25
diff
changeset
|
12 |
| 142 | 13 const char *messages[] = { |
| 14 "Message 1", | |
| 15 "Message 2", | |
| 16 "Message 3" | |
| 17 }; | |
| 18 | |
| 19 for (int i = 0; i < 3; i++) | |
| 20 { | |
| 21 printf("Sending: %s\n", messages[i]); | |
| 22 Seobeo_WebSocket_Send_Text(p_ws, messages[i]); | |
| 23 usleep(100000); | |
| 24 } | |
|
71
75de5903355c
Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents:
25
diff
changeset
|
25 |
| 142 | 26 printf("Receiving responses...\n"); |
| 27 int received = 0; | |
| 28 int attempts = 0; | |
| 29 | |
| 30 while (received < 3 && attempts < 200) | |
| 31 { | |
| 32 Seobeo_WebSocket_Message *p_msg = Seobeo_WebSocket_Receive(p_ws); | |
| 33 if (p_msg) | |
| 34 { | |
| 35 if (p_msg->opcode == SEOBEO_WS_OPCODE_TEXT) | |
| 36 { | |
| 37 printf("Response %d: %.*s\n", received + 1, (int)p_msg->length, (char*)p_msg->data); | |
| 38 received++; | |
| 39 } | |
| 40 Seobeo_WebSocket_Message_Destroy(p_msg); | |
| 126 | 41 } |
|
71
75de5903355c
Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents:
25
diff
changeset
|
42 |
| 142 | 43 usleep(10000); |
| 44 attempts++; | |
| 45 } | |
| 46 printf("Received %d/%d messages\n", received, 3); | |
| 47 Seobeo_WebSocket_Destroy(p_ws); | |
|
1
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
48 } |
| 142 | 49 |
| 50 void Test_Chat() | |
| 51 { | |
| 52 printf("\n=== Test: Multiple Messages ===\n"); | |
| 53 | |
| 54 Seobeo_WebSocket *p_ws = Seobeo_WebSocket_Connect("ws://127.0.0.1:8080/chat"); | |
| 55 if (!p_ws) | |
| 56 { | |
| 57 printf("Failed to connect\n"); | |
| 58 return; | |
| 59 } | |
| 60 | |
| 61 const char *messages[] = { | |
| 62 "Message 1", | |
| 63 "Message 2", | |
| 64 "Message 3" | |
| 65 }; | |
| 66 | |
| 67 for (int i = 0; i < 3; i++) | |
| 68 { | |
| 69 printf("Sending: %s\n", messages[i]); | |
| 70 Seobeo_WebSocket_Send_Text(p_ws, messages[i]); | |
| 71 usleep(100000); | |
| 72 } | |
| 73 | |
| 74 printf("Receiving responses...\n"); | |
| 75 int received = 0; | |
| 76 int attempts = 0; | |
| 77 | |
| 78 while (received < 3 && attempts < 200) | |
| 79 { | |
| 80 Seobeo_WebSocket_Message *p_msg = Seobeo_WebSocket_Receive(p_ws); | |
| 81 if (p_msg) | |
| 82 { | |
| 83 if (p_msg->opcode == SEOBEO_WS_OPCODE_TEXT) | |
| 84 { | |
| 85 printf("Response %d: %.*s\n", received + 1, (int)p_msg->length, (char*)p_msg->data); | |
| 86 received++; | |
| 87 } | |
| 88 Seobeo_WebSocket_Message_Destroy(p_msg); | |
| 89 } | |
| 90 | |
| 91 usleep(10000); | |
| 92 attempts++; | |
| 93 } | |
| 94 printf("Received %d/%d messages\n", received, 3); | |
| 95 Seobeo_WebSocket_Destroy(p_ws); | |
| 96 } | |
| 97 | |
| 98 int main(int argc, char *argv[]) | |
| 99 { | |
| 100 | |
| 101 Test_Echo(); | |
| 102 } |