diff --git a/examples/cpp_client_example/CMakeLists.txt b/examples/cpp_client_example/CMakeLists.txt index 597a7f9..7452d3a 100644 --- a/examples/cpp_client_example/CMakeLists.txt +++ b/examples/cpp_client_example/CMakeLists.txt @@ -1,33 +1,29 @@ -cmake_minimum_required(VERSION 3.1) -project(SocketIOClientExample) +cmake_minimum_required(VERSION 3.12) +project(MySocketIOApp) -set(CMAKE_CXX_STANDARD 11) +# Include ExternalProject module +include(ExternalProject) -# Set the path to the Socket.IO Client C++ library -set(SOCKET_IO_CLIENT_CPP_PATH /path/to/socket.io-client-cpp) - -# Add the Socket.IO Client C++ library include directory -include_directories(${SOCKET_IO_CLIENT_CPP_PATH}/src) - -# Add the Socket.IO Client C++ library source files -set(SOCKET_IO_CLIENT_CPP_SOURCE_FILES - ${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_packet.h - ${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_packet.cpp - ${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_packet_op.h - ${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_packet_op.cpp - ${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_packet_socket.h - ${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_packet_socket.cpp - ${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_streambuf.h - ${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_streambuf.cpp - ${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_socket.h - ${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_socket.cpp - ${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_socket_impl.h - ${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_socket_impl.cpp - ${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_client_impl.h - ${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_client_impl.cpp - ${SOCKET_IO_CLIENT_CPP_PATH}/src/sio_client.h - ${SOCKET_IO_CLIENT_CPP_PATH}/src/sio_client.cpp +# Set up the external project +ExternalProject_Add( + socketio-client-cpp + PREFIX ${CMAKE_BINARY_DIR}/external + GIT_REPOSITORY https://github.com/socketio/socket.io-client-cpp.git + GIT_TAG master # You can change this to a specific release/tag if needed + CMAKE_ARGS -DCMAKE_INSTALL_PREFIX= ) -# Add the executable target -add_executable(SocketIOClientExample main.cpp ${SOCKET_IO_CLIENT_CPP_SOURCE_FILES}) +# Add your project's executable or library here +add_executable(MySocketIOApp main.cpp) + +# Include the socket.io-client-cpp header files +target_include_directories(MySocketIOApp PRIVATE + ${CMAKE_BINARY_DIR}/external/include + ${CMAKE_BINARY_DIR}/external/include/socket.io-client-cpp # Add this line +) +# This is required for googletest +find_package(sioclient) +# Link your project with the socket.io-client-cpp library +target_include_directories(MySocketIOApp PRIVATE ${CMAKE_BINARY_DIR}/external/include) +target_link_directories(MySocketIOApp PRIVATE ${CMAKE_BINARY_DIR}/external/lib) +target_link_libraries(MySocketIOApp PRIVATE sioclient) diff --git a/examples/cpp_client_example/build.py b/examples/cpp_client_example/build.py deleted file mode 100644 index 0d9b60a..0000000 --- a/examples/cpp_client_example/build.py +++ /dev/null @@ -1,48 +0,0 @@ -import os -import subprocess -import platform - -repo_dir = 'socket.io-client-cpp' - -# Check if the repository directory exists -if os.path.exists(repo_dir): - # If it exists, perform a git pull to update the repository - os.chdir(repo_dir) - subprocess.run(['git', 'pull']) -else: - # If it doesn't exist, clone the Socket.IO Client C++ library - subprocess.run(['git', 'clone', 'https://github.com/socketio/socket.io-client-cpp.git', repo_dir]) - - # Build the Socket.IO Client C++ library - os.chdir(repo_dir) - -if platform.system() == 'Windows': - os.makedirs('build', exist_ok=True) - os.chdir('build') - - # Detect if running on Windows Command Prompt (cmd) or PowerShell - shell = os.environ.get('SHELL') - if shell and 'powershell' in shell.lower(): - subprocess.run(['cmake', '..']) - subprocess.run(['cmake', '--build', '.', '--config', 'Release']) - else: - # Use the Visual Studio build tools - subprocess.run(['cmake', '-G', 'Visual Studio 16 2019', '..']) - subprocess.run(['cmake', '--build', '.', '--config', 'Release']) - -else: - subprocess.run(['mkdir', 'build']) - os.chdir('build') - subprocess.run(['cmake', '..']) - subprocess.run(['make']) - -# Compile the C++ code -os.chdir('../..') -if platform.system() == 'Windows': - subprocess.run(['cl', '/EHsc', '/Isocket.io-client-cpp/src', - 'main.cpp', 'socket.io-client-cpp/build/Release/sioclient.lib', '/FeSocketIOClientExample.exe']) -else: - subprocess.run(['g++', '-std=c++11', '-Isocket.io-client-cpp/src', - 'main.cpp', 'socket.io-client-cpp/build/libsioclient.a', '-o', 'SocketIOClientExample']) - -print('Compilation completed successfully.') diff --git a/examples/cpp_client_example/main.cpp b/examples/cpp_client_example/main.cpp index 0920f6f..f3475a5 100644 --- a/examples/cpp_client_example/main.cpp +++ b/examples/cpp_client_example/main.cpp @@ -1,51 +1,110 @@ #include #include -#include +#include +#include +#include +#include + +using namespace sio; + class SocketIOClient { public: - SocketIOClient(const std::string& serverUrl) : client_(sio::socket::create()) + SocketIOClient(const std::string& serverUrl) : connected_(false) { - client_->set_open_listener([&]() { - onConnected(); - }); + // Set up event listeners + setupEventListeners(); - client_->set_close_listener([&](sio::client::close_reason const& reason) { - onDisconnected(); - }); - - client_->set_fail_listener([&]() { - onConnectionFailed(); - }); - - client_->set_reconnect_listener([&](unsigned int reconnectionAttempts, unsigned int delay) { - onReconnecting(reconnectionAttempts, delay); - }); - - client_->set_socket_close_listener([&]() { - onSocketClosed(); - }); - - client_->connect(serverUrl); + // Connect to the server + client_.connect(serverUrl); } void generateText(const std::string& prompt) { - sio::message::list messages; - messages.push(sio::string_message::create(prompt)); - messages.push(sio::int_message::create(-1)); - messages.push(sio::int_message::create(1024)); - client_->socket()->emit("generate_text", messages); + if (connected_) { + } else { + std::cerr << "Not connected to the server. Cannot generate text." << std::endl; + } } void cancelGeneration() { - client_->socket()->emit("cancel_generation"); + if (connected_) { + client_.socket()->emit("cancel_generation"); + } else { + std::cerr << "Not connected to the server. Cannot cancel generation." << std::endl; + } + } + + // Getter for client_ + const sio::client& getClient() const { + return client_; + } + void closeConnection() { + client_.close(); // Ou utilisez une autre méthode de fermeture selon la bibliothèque sio + } + +private: + client client_; + bool connected_; + + void setupEventListeners() + { + client_.set_open_listener([&]() { + onConnected(); + }); + + client_.set_close_listener([&](sio::client::close_reason const& reason) { + onDisconnected(); + }); + + client_.set_fail_listener([&]() { + onConnectionFailed(); + }); + + client_.set_reconnect_listener([&](unsigned int reconnectionAttempts, unsigned int delay) { + onReconnecting(reconnectionAttempts, delay); + }); + + client_.set_socket_close_listener((const sio::client::socket_listener &)[&]() { + onSocketClosed(); + }); + + // Event handler for receiving generated text chunks + client_.socket()->on("text_chunk", [&](const sio::event& event) { + const std::string chunk = event.get_message()->get_string(); + std::cout << "Received chunk: " << chunk << std::endl; + // Append the chunk to the output or perform any other required actions + // ... + }); + + // Event handler for receiving generated text + client_.socket()->on("text_generated", [&](const sio::event& event) { + const std::string text = event.get_message()->get_string(); + std::cout << "Text generated: " << text << std::endl; + // Toggle button visibility or perform any other required actions + // ... + }); + + // Event handler for error during text generation + client_.socket()->on("buzzy", [&](const sio::event& event) { + const std::string error = event.get_message()->get_string(); + std::cerr << "Server is busy. Wait for your turn. Error: " << error << std::endl; + // Handle the error or perform any other required actions + // ... + }); + + // Event handler for generation cancellation + client_.socket()->on("generation_canceled", [&](const sio::event& event) { + // Toggle button visibility or perform any other required actions + // ... + }); } void onConnected() { std::cout << "Connected to the LoLLMs server" << std::endl; + connected_ = true; // Perform actions upon successful connection // ... } @@ -53,6 +112,7 @@ public: void onDisconnected() { std::cout << "Disconnected from the server" << std::endl; + connected_ = false; // Perform actions upon disconnection // ... } @@ -74,48 +134,26 @@ public: void onSocketClosed() { std::cout << "Socket closed" << std::endl; + connected_ = false; // Perform actions upon socket closure // ... } + + -private: - sio::client client_; }; int main() { // Create a SocketIOClient instance and connect to the server SocketIOClient client("http://localhost:9601"); - - // Event handler for receiving generated text chunks - client.client_->socket()->on("text_chunk", [&](const sio::event& event) { - const std::string chunk = event.get_message()->get_string(); - std::cout << "Received chunk: " << chunk << std::endl; - // Append the chunk to the output or perform any other required actions - // ... - }); - - // Event handler for receiving generated text - client.client_->socket()->on("text_generated", [&](const sio::event& event) { - const std::string text = event.get_message()->get_string(); - std::cout << "Text generated: " << text << std::endl; - // Toggle button visibility or perform any other required actions - // ... - }); - - // Event handler for error during text generation - client.client_->socket()->on("buzzy", [&](const sio::event& event) { - const std::string error = event.get_message()->get_string(); - std::cerr << "Server is busy. Wait for your turn. Error: " << error << std::endl; - // Handle the error or perform any other required actions - // ... - }); - - // Event handler for generation cancellation - client.client_->socket()->on("generation_canceled", [&](const sio::event& event) { - // Toggle button visibility or perform any other required actions - // ... - }); + std::cout<<"Created"<sync_close(); + // Run the event loop to keep the connection alive + while (true) + { + // You can add some logic here to break the loop when needed + // For example, when the user wants to exit the program + } + std::cout<<"Done"<