view playground/main.c @ 166:78ea8d5ccc87

[ThirdParty] Added sqlite3 to the third_party.
author MrJuneJune <me@mrjunejune.com>
date Mon, 19 Jan 2026 16:28:34 -0800
parents 7387eec8e7f8
children 827c6ac504cd 8d17f6e6e290
line wrap: on
line source

#include "seobeo/seobeo.h"

void Test_Echo()
{
  printf("\n=== Test: Multiple Messages ===\n");
  Seobeo_WebSocket *p_ws = Seobeo_WebSocket_Connect("wss://mrjunejune.com/echo");
  if (!p_ws)
  {
    printf("Failed to connect\n");
    return;
  }

  const char *messages[] = {
    "Message 1",
    "Message 2",
    "Message 3"
  };

  for (int i = 0; i < 3; i++)
  {
    printf("Sending: %s\n", messages[i]);
    Seobeo_WebSocket_Send_Text(p_ws, messages[i]);
    usleep(100000);
  }

  printf("Receiving responses...\n");
  int received = 0;
  int attempts = 0;

  while (received < 3 && attempts < 200)
  {
    Seobeo_WebSocket_Message *p_msg = Seobeo_WebSocket_Receive(p_ws);
    if (p_msg)
    {
      if (p_msg->opcode == SEOBEO_WS_OPCODE_TEXT)
      {
        printf("Response %d: %.*s\n", received + 1, (int)p_msg->length, (char*)p_msg->data);
        received++;
      }
      Seobeo_WebSocket_Message_Destroy(p_msg);
    }

    usleep(10000);
    attempts++;
  }
  printf("Received %d/%d messages\n", received, 3);
  Seobeo_WebSocket_Destroy(p_ws);
}

void Test_Chat()
{
  printf("\n=== Test: Multiple Messages ===\n");

  Seobeo_WebSocket *p_ws = Seobeo_WebSocket_Connect("ws://127.0.0.1:8080/chat");
  if (!p_ws)
  {
    printf("Failed to connect\n");
    return;
  }

  const char *messages[] = {
    "Message 1",
    "Message 2",
    "Message 3"
  };

  for (int i = 0; i < 3; i++)
  {
    printf("Sending: %s\n", messages[i]);
    Seobeo_WebSocket_Send_Text(p_ws, messages[i]);
    usleep(100000);
  }

  printf("Receiving responses...\n");
  int received = 0;
  int attempts = 0;

  while (received < 3 && attempts < 200)
  {
    Seobeo_WebSocket_Message *p_msg = Seobeo_WebSocket_Receive(p_ws);
    if (p_msg)
    {
      if (p_msg->opcode == SEOBEO_WS_OPCODE_TEXT)
      {
        printf("Response %d: %.*s\n", received + 1, (int)p_msg->length, (char*)p_msg->data);
        received++;
      }
      Seobeo_WebSocket_Message_Destroy(p_msg);
    }

    usleep(10000);
    attempts++;
  }
  printf("Received %d/%d messages\n", received, 3);
  Seobeo_WebSocket_Destroy(p_ws);
}

int main(int argc, char *argv[])
{

  Test_Echo();
}