Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[coro_http][feat]coro_http_server support websocket #504

Merged
merged 1 commit into from
Nov 27, 2023

Conversation

qicosmos
Copy link
Collaborator

Why

  coro_http::coro_http_server server(1, 9001);
  server.set_http_handler<cinatra::GET>(
      "/ws_echo",
      [](coro_http_request &req,
         coro_http_response &resp) -> async_simple::coro::Lazy<void> {
        assert(req.get_content_type() == content_type::websocket);
        std::string out_str;
        websocket_result result{};
        while (!result.eof) {
          result = co_await req.get_conn()->read_websocket();
          if (result.ec) {
            break;
          }

          if (result.type == ws_frame_type::WS_CLOSE_FRAME) {
            std::cout << "close frame\n";
            break;
          }

          out_str.append(result.data);

          auto ec = co_await req.get_conn()->write_websocket(result.data);
          if (ec) {
            continue;
          }
        }

        std::cout << out_str << "\n";
      });

  server.async_start();

What is changing

Example

@qicosmos qicosmos changed the title coro_http_server support websocket [coro_http][feat]coro_http_server support websocket Nov 27, 2023
@qicosmos qicosmos merged commit 1d6d03a into alibaba:main Nov 27, 2023
30 checks passed
@qicosmos qicosmos deleted the support_websocket branch November 27, 2023 04:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant