I'm ussing uwebsockets to create ws server.

main.cpp:

int main()
{
  struct UserData {

  };

  uWS::App().ws<UserData>("/*", {

      /* Just a few of the available handlers */
      .open = [](auto *ws) {
          /* MQTT syntax */
          ws->subscribe("sensors/+/house");
      },
      .message = [](auto *ws, std::string_view message, uWS::OpCode opCode) {
          ws->send(message, opCode);
      }

  }).listen(9001, [](auto *listenSocket) {

      if (listenSocket) {
          std::cout << "Listening on port " << 9001 << std::endl;
      }

  }).run();
  return 0;
}

the error message while building:

……main.cpp:22:4: note:   cannot convert '{<lambda closure object>main()::<lambda(auto:11*)>{}, <lambda closure object>main()::<lambda(auto:12*, std::string_view, uWS::OpCode)>{}}' (type '<brace-enclosed initializer list>') to type 'uWS::TemplatedApp<false>::WebSocketBehavior<main()::UserData>&&'
   }).listen(9001, [](auto *listenSocket) {
    ^

env:

OS: Windows10 64bit

IDE: QtCreator

compiler: MinGW 8.1.0 32bit

c++std: 17

uwebsockets: 19.2.0

1

There are 1 answers

0
SinoDawn On BEST ANSWER

It is because the compiler cannot convert 'brace-enclosed initializer list' to T&&. It can be solved by declaring the struct and converting it to Rvalue reference.

  struct UserData {

  };

  uWS::TemplatedApp<false>::WebSocketBehavior<UserData> wsb = {
    /* Just a few of the available handlers */
    .open = [](auto *ws) {
      /* MQTT syntax */
      ws->subscribe("sensors/+/house");
    },
    .message = [](auto *ws, std::string_view message, uWS::OpCode opCode) {
      ws->send(message, opCode);
    }
  };

  uWS::App().ws<UserData>("/*", std::move(wsb)).listen(9001, [](auto *listenSocket) {
      if (listenSocket) {
          std::cout << "Listening on port " << 9001 << std::endl;
      }
  }).run();