Untitled

mail@pastecode.io avatar
unknown
plain_text
10 months ago
1.0 kB
3
Indexable
Never
#include <iostream>

#include "../include/cinatra.hpp"

using namespace cinatra;


async_simple::coro::Lazy<void> test_websocket() {
  coro_http_client client{};
  client.on_ws_close([](std::string_view reason) {
    std::cout << "web socket close " << reason << std::endl;
  });
  client.on_ws_msg([](resp_data data) {
    if (data.net_err) {
      std::cout << data.net_err.message() << "\n";
      return;
    }
    std::cout << data.resp_body << std::endl;
  });

  bool r = co_await client.async_ws_connect("ws://192.168.16.1:8080/ws_echo");
  if (!r) {
    co_return;
  }

  auto data = co_await client.async_send_ws("test2fdsaf", true, opcode::binary);
  //auto data = co_await client.async_send_ws_chuncked(tmp, 3145728, false, opcode::binary);
  if (data.eof) {
    std::cout << "data complete" << std::endl;
  }

  auto result = co_await client.async_send_ws_close("ws close");
  std::cout << "close socket!\n";
}


int main() {
  async_simple::coro::syncAwait(test_websocket());
  return 0;
}