uWebSockets icon indicating copy to clipboard operation
uWebSockets copied to clipboard

Problem with provided default example inside documentation

Open jakubhaumer opened this issue 1 year ago • 4 comments

I pulled out the provided example inside the uWebSockets documentation which goes:

`#include <App.h>
#include <utility>
#include <iostream>

struct UserData {
    // Define any data you want to associate with each WebSocket connection
};

int main() {
    uWS::App().ws<UserData>("/*", uWS::WebSocketBehavior<UserData>{
        /* Settings */
        .compression = uWS::SHARED_COMPRESSOR,
        .maxPayloadLength = 16 * 1024,
        .idleTimeout = 10,
        /* Handlers */
        .upgrade = [](auto *res, auto *req, auto *context) {
            // Handle upgrade request
        },
        .open = [](auto *ws) {
            // Handle connection opened
        },
        .message = [](auto *ws, std::string_view message, uWS::OpCode opCode) {
            ws->send(message, opCode);
        },
        .drain = [](auto *ws) {
            // Handle drain event
        },
        .ping = [](auto *ws) {
            // Handle ping event
        },
        .pong = [](auto *ws) {
            // Handle pong event
        },
        .close = [](auto *ws, int code, std::string_view message) {
            // Handle connection closed
        }
    }).run();
}
`

And im unable to compile that code. Im having problem with second argument inside the .ws() method. I cannot do rvalue nor lvalue. Using std::move() is not working, initializing right inside the method does not work too. Explicit usage of the type does not work too. Here is my cmake --build . --verbose:

`Change Dir: '/Users/me/documents/Programming/test/multiprocessing/realtime_build'

Run Build Command(s): /opt/homebrew/Cellar/cmake/3.30.3/bin/cmake -E env VERBOSE=1 /usr/bin/make -f Makefile
/opt/homebrew/Cellar/cmake/3.30.3/bin/cmake -S/Users/me/documents/Programming/test/multiprocessing/realtime_fetcher -B/Users/me/documents/Programming/test/multiprocessing/realtime_build --check-build-system CMakeFiles/Makefile.cmake 0
/opt/homebrew/Cellar/cmake/3.30.3/bin/cmake -E cmake_progress_start /Users/me/documents/Programming/test/multiprocessing/realtime_build/CMakeFiles /Users/me/documents/Programming/test/multiprocessing/realtime_build//CMakeFiles/progress.marks
/Library/Developer/CommandLineTools/usr/bin/make  -f CMakeFiles/Makefile2 all
/Library/Developer/CommandLineTools/usr/bin/make  -f CMakeFiles/realtime_fetcher.dir/build.make CMakeFiles/realtime_fetcher.dir/depend
cd /Users/me/documents/Programming/test/multiprocessing/realtime_build && /opt/homebrew/Cellar/cmake/3.30.3/bin/cmake -E cmake_depends "Unix Makefiles" /Users/me/documents/Programming/test/multiprocessing/realtime_fetcher /Users/me/documents/Programming/test/multiprocessing/realtime_fetcher /Users/me/documents/Programming/test/multiprocessing/realtime_build /Users/me/documents/Programming/test/multiprocessing/realtime_build /Users/me/documents/Programming/test/multiprocessing/realtime_build/CMakeFiles/realtime_fetcher.dir/DependInfo.cmake "--color="
Dependencies file "CMakeFiles/realtime_fetcher.dir/Users/me/documents/Programming/test/multiprocessing/src/realtime_fetcher.cpp.o.d" is newer than depends file "/Users/me/documents/Programming/test/multiprocessing/realtime_build/CMakeFiles/realtime_fetcher.dir/compiler_depend.internal".
Consolidate compiler generated dependencies of target realtime_fetcher
/Library/Developer/CommandLineTools/usr/bin/make  -f CMakeFiles/realtime_fetcher.dir/build.make CMakeFiles/realtime_fetcher.dir/build
[ 50%] Building CXX object CMakeFiles/realtime_fetcher.dir/Users/me/documents/Programming/test/multiprocessing/src/realtime_fetcher.cpp.o
/Library/Developer/CommandLineTools/usr/bin/c++  -I/Users/me/documents/Programming/test/multiprocessing/realtime_fetcher/../include -I/Users/me/documents/Programming/test/multiprocessing/realtime_fetcher/../include/openssl-3.3.2/include -I/Users/me/documents/Programming/test/multiprocessing/realtime_fetcher/../include/rapidjson-1.1.0/include -I/Users/me/documents/Programming/test/multiprocessing/realtime_fetcher/../include/uWebSockets/src -I/Users/me/documents/Programming/test/multiprocessing/realtime_fetcher/../include/uSockets -I/Users/me/documents/Programming/test/multiprocessing/realtime_fetcher/../include/zlib -I/Users/me/documents/Programming/test/multiprocessing/realtime_fetcher/../include/zlib/build -isystem /opt/homebrew/include -std=gnu++20 -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX14.4.sdk -MD -MT CMakeFiles/realtime_fetcher.dir/Users/me/documents/Programming/test/multiprocessing/src/realtime_fetcher.cpp.o -MF CMakeFiles/realtime_fetcher.dir/Users/me/documents/Programming/test/multiprocessing/src/realtime_fetcher.cpp.o.d -o CMakeFiles/realtime_fetcher.dir/Users/me/documents/Programming/test/multiprocessing/src/realtime_fetcher.cpp.o -c /Users/me/documents/Programming/test/multiprocessing/src/realtime_fetcher.cpp
/Users/me/documents/Programming/test/multiprocessing/src/realtime_fetcher.cpp:82:14: error: no matching member function for call to 'ws'
  uWS::App().ws<UserData>("/*", {
  ~~~~~~~~~~~^~~~~~~~~~~~
/Users/me/documents/Programming/test/multiprocessing/realtime_fetcher/../include/uWebSockets/src/App.h:272:32: note: candidate function template not viable: cannot convert initializer list argument to 'WebSocketBehavior<UserData>'
    BuilderPatternReturnType &&ws(std::string pattern, WebSocketBehavior<UserData> &&behavior) {
                               ^
1 error generated.
make[2]: *** [CMakeFiles/realtime_fetcher.dir/Users/me/documents/Programming/test/multiprocessing/src/realtime_fetcher.cpp.o] Error 1
make[1]: *** [CMakeFiles/realtime_fetcher.dir/all] Error 2
make: *** [all] Error 2`

jakubhaumer avatar Sep 20 '24 18:09 jakubhaumer

You compile the examples by hitting "make".

uNetworkingAB avatar Sep 20 '24 21:09 uNetworkingAB

@uNetworkingAB ,

Please add std::string_view parameters to ping and pong in the documentation.

Thanks.

Jacob-Burckhardt avatar Sep 21 '24 00:09 Jacob-Burckhardt

You compile the examples by hitting "make".

But its not inside example, its inside my program.

jakubhaumer avatar Sep 21 '24 06:09 jakubhaumer

The issue has been resolved using this:

#include <App.h>

struct UserData {

};

int main() {
   uWS::App().ws<UserData>("/*", {
    /* Settings */
    .compression = uWS::SHARED_COMPRESSOR,
    .maxPayloadLength = 16 * 1024,
    .idleTimeout = 10,
    /* Handlers */
    .upgrade = [](auto *res, auto *req, auto *context) {
        /* You may read from req only here, and COPY whatever you need into your PerSocketData.
         * See UpgradeSync and UpgradeAsync examples. */
    },
    .open = [](auto *ws) {

    },
    .message = [](auto *ws, std::string_view message, uWS::OpCode opCode) {
        ws->send(message, opCode);
    },
    .drain = [](auto *ws) {
        /* Check getBufferedAmount here */
    },
    .ping = [](auto *ws, std::string_view) {

    },
    .pong = [](auto *ws, std::string_view) {

    },
    .close = [](auto *ws, int code, std::string_view message) {

    }
    }).connect("ws://localhost:9001", nullptr).run();

jakubhaumer avatar Sep 22 '24 07:09 jakubhaumer