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>
// Our websocket object
ix::WebSocket webSocket;
int main()
{
// Required on Windows
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;
// Setup a callback to be fired (in a background thread, watch out for race conditions !)
// when a message or an event (open, close, error) is received
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)
{
// Maybe SSL is not configured properly
std::cout << "Connection error: " << msg->errorInfo.reason << std::endl;
// std::cout << "> " << std::flush;
}
}
);
// Now that our callback is setup, we can start our background thread and receive messages
webSocket.start();
// Send a message to the server (default to TEXT mode)
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\"}"}
>