c++boost-beastbeast-websockets

boost-beast websocket server that also accept http connections


I need to implement a simple asynchronous websocket server using boost beast that can accept both websocket and standard http connections.

I've tried something like this:

...

// ws is a boost::beast::websocket::stream<boost::asio::ip::tcp::socket>
ws.async_accept_ex(
  [](boost::beast::websocket::response_type& res)
  {
    res.set(boost::beast::http::field::server, "MyServer");
  },
  [self](boost::beast::error_code e)
  { 
    if (e) self->ReadHttp();
    else self->ReadWs();
  }
);
...

void ReadHttp()
{
  auto self(shared_from_this());
  ws.next_layer().async_read_some(
    boost::asio::buffer(data, max_length),
    [self](boost::system::error_code ec, std::size_t length)
    {
      if (!self->ws.next_layer().is_open() || ec==boost::asio::error::eof ||  ec == boost::asio::error::connection_reset)
        // handle disconnection
      else if (ec)
        // handle error
      else
      {
        std::string s(self->data, length);
        cout << "HTTP rx: " << s << endl;
        self->ReadHttp();
      }
    }
  );
}

void ReadWs()
{
  auto self(shared_from_this());
  ws.async_read(
    rxData,
    [self](boost::beast::error_code ec, std::size_t /*length*/)
    {
      if(ec == boost::beast::websocket::error::closed)
        // handle disconnection
      else if ( ec )
        // handle error
      else
      {
        std::string s((std::istreambuf_iterator<char>(&self->rxData)), std::istreambuf_iterator<char>());
        cout << "WS rx: " << s << endl;
        self->rxData.consume(self->rxData.size());
        self->Read();
      }
    }
  );
}

but when an HTTP client connects, the server misses the first message sent. Obviously, this is not the correct approach :-)

Can anyone help me with this? Thanks


Solution

  • The advanced-server and advanced-server-flex examples demonstrate how to build a server that handles normal HTTP requests and that also handles the WebSocket upgrade request:

    https://github.com/boostorg/beast/tree/e23ecc8ac903b303b9d1a9824b97c092cb3c09bd/example/advanced/server

    https://github.com/boostorg/beast/tree/e23ecc8ac903b303b9d1a9824b97c092cb3c09bd/example/advanced/server-flex