view seobeo/tests/seobeo_websocket_test.c @ 133:902e29c38d66

[Blog] Final copy for websocket one.
author June Park <parkjune1995@gmail.com>
date Fri, 09 Jan 2026 08:30:35 -0800
parents cbbf78b17cfa
children
line wrap: on
line source

#include "seobeo/seobeo.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void Test_WebSocket_Echo()
{
  printf("\n=== Test: WebSocket Echo Server ===\n");

  Seobeo_WebSocket *p_ws = Seobeo_WebSocket_Connect("wss://echo.websocket.org");
  if (!p_ws)
  {
    printf("Failed to connect to WebSocket server\n");
    return;
  }

  printf("Connected to WebSocket server\n");

  const char *test_message = "Hello, WebSocket!";
  printf("Sending: %s\n", test_message);

  if (Seobeo_WebSocket_Send_Text(p_ws, test_message) < 0)
  {
    printf("Failed to send message\n");
    Seobeo_WebSocket_Destroy(p_ws);
    return;
  }

  printf("Waiting for echo response...\n");

  int attempts = 0;
  while (attempts < 100)
  {
    Seobeo_WebSocket_Message *p_msg = Seobeo_WebSocket_Receive(p_ws);
    if (p_msg)
    {
      if (p_msg->opcode == SEOBEO_WS_OPCODE_TEXT)
      {
        printf("Received text: %.*s\n", (int)p_msg->length, (char*)p_msg->data);
      }
      else if (p_msg->opcode == SEOBEO_WS_OPCODE_BINARY)
      {
        printf("Received binary data (%zu bytes)\n", p_msg->length);
      }

      Seobeo_WebSocket_Message_Destroy(p_msg);
      break;
    }

    usleep(10000);
    attempts++;
  }

  if (attempts >= 100)
    printf("Timeout waiting for response\n");

  Seobeo_WebSocket_Close(p_ws, 1000, "Test complete");
  Seobeo_WebSocket_Destroy(p_ws);

  printf("WebSocket test completed\n");
}

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

  Seobeo_WebSocket *p_ws = Seobeo_WebSocket_Connect("wss://echo.websocket.org");
  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_WebSocket_Binary_Data()
{
  printf("\n=== Test: Binary Data ===\n");

  Seobeo_WebSocket *p_ws = Seobeo_WebSocket_Connect("wss://echo.websocket.org");
  if (!p_ws)
  {
    printf("Failed to connect\n");
    return;
  }

  uint8 binary_data[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};
  printf("Sending %zu bytes of binary data\n", sizeof(binary_data));

  if (Seobeo_WebSocket_Send_Binary(p_ws, binary_data, sizeof(binary_data)) < 0)
  {
    printf("Failed to send binary data\n");
    Seobeo_WebSocket_Destroy(p_ws);
    return;
  }

  int attempts = 0;
  while (attempts < 100)
  {
    Seobeo_WebSocket_Message *p_msg = Seobeo_WebSocket_Receive(p_ws);
    if (p_msg)
    {
      if (p_msg->opcode == SEOBEO_WS_OPCODE_BINARY)
      {
        printf("Received %zu bytes of binary data: ", p_msg->length);
        for (size_t i = 0; i < p_msg->length && i < 16; i++)
          printf("%02X ", p_msg->data[i]);
        printf("\n");
      }

      Seobeo_WebSocket_Message_Destroy(p_msg);
      break;
    }

    usleep(10000);
    attempts++;
  }

  Seobeo_WebSocket_Destroy(p_ws);
}

void Test_WebSocket_Ping_Pong()
{
  printf("\n=== Test: Ping/Pong ===\n");

  Seobeo_WebSocket *p_ws = Seobeo_WebSocket_Connect("wss://echo.websocket.org");
  if (!p_ws)
  {
    printf("Failed to connect\n");
    return;
  }

  printf("Sending ping...\n");
  if (Seobeo_WebSocket_Send_Ping(p_ws, "ping payload") < 0)
  {
    printf("Failed to send ping\n");
    Seobeo_WebSocket_Destroy(p_ws);
    return;
  }

  printf("Ping sent (pong should be handled automatically)\n");

  usleep(500000);

  Seobeo_WebSocket_Destroy(p_ws);
}

int main()
{
  printf("=== Seobeo WebSocket Tests ===\n");

  Test_WebSocket_Echo();
  Test_WebSocket_Multiple_Messages();
  Test_WebSocket_Binary_Data();
  Test_WebSocket_Ping_Pong();

  printf("\n=== All Tests Complete ===\n");
  return 0;
}