Setting up IXWebSocket 11.4.5 in Visual Studio 2022 using CMake
- Install IXWebSocket using this guide: How to install IXWebSocket 11.4.5 using CMake and Visual Studio 2022 for Desktop
- Run Visual Studio
- Click on the "Create a new project" button:
- Click on the "CMake Project" and click on the "Next" button:
- Type a project name, set a project location, check the "Place solution and project in the same directory" checkbox, and click on the "Create" button:
- Wait a few seconds for configuration
- Run the project by click on the empty green triangle (or Ctrl+F5):
- The program prints "Hello CMake" to the console:
- Double click on the "CMakeLists.txt" file:
- Copy and paste the following code to the "CMakeLists.txt" file with your paths:
| target_include_directories(hello-ixwebsocket PRIVATE E:/libs/ixwebsocket-11.4.5-msvc/win/debug/include) |
| |
| target_link_directories(hello-ixwebsocket PRIVATE E:/libs/ixwebsocket-11.4.5-msvc/win/debug/lib) |
| target_link_directories(hello-ixwebsocket PRIVATE E:/libs/mbedtls-2.28.9-msvc/win/debug/lib) |
| target_link_directories(hello-ixwebsocket PRIVATE E:/libs/zlib-1.3.1-msvc/win/debug/lib) |
| |
| target_link_libraries(hello-ixwebsocket PRIVATE ixwebsocket mbedx509 ws2_32 mbedtls mbedcrypto crypt32 zlibstaticd wsock32) |
Double click on the "hello-ixwebsocket.cpp" file:
Replace the "hello-ixwebsocket.cpp" file content with the following one:
| #include <ixwebsocket/IXNetSystem.h> |
| #include <ixwebsocket/IXWebSocket.h> |
| #include <ixwebsocket/IXUserAgent.h> |
| |
| #include <iostream> |
| |
| |
| ix::WebSocket webSocket; |
| |
| int main() |
| { |
| |
| ix::initNetSystem(); |
| |
| std::string url("wss://testing-websockets-using-emscripten-sdl3-and-opengl-es-20.glitch.me"); |
| webSocket.setUrl(url); |
| |
| std::cout << "Connecting to " << url << "..." << std::endl; |
| |
| |
| |
| webSocket.setOnMessageCallback([](const ix::WebSocketMessagePtr& msg) |
| { |
| if (msg->type == ix::WebSocketMessageType::Message) |
| { |
| std::cout << "received message: " << msg->str << std::endl; |
| std::cout << "> " << std::flush; |
| } |
| else if (msg->type == ix::WebSocketMessageType::Open) |
| { |
| std::cout << "Connection established" << std::endl; |
| std::cout << "> " << std::flush; |
| } |
| else if (msg->type == ix::WebSocketMessageType::Error) |
| { |
| |
| std::cout << "Connection error: " << msg->errorInfo.reason << std::endl; |
| |
| } |
| } |
| ); |
| |
| |
| webSocket.start(); |
| |
| |
| webSocket.send("hello world"); |
| |
| getchar(); |
| return 0; |
| } |
Press Ctrl+F5 to run the application
Wait 15-20 seconds and you will see a JSON answer from the Glitch server:
| Connecting to wss://testing-websockets-using-emscripten-sdl3-and-opengl-es-20.glitch.me... |
| Connection established |
| > received message: {"action":"scGravity","data":"{\"message\":\"hello from server\"}"} |
| > |