Refined example ensuring proper cleanup etc

This commit is contained in:
eternal 2024-05-12 14:06:28 -07:00 committed by GitHub
parent c27f5a94bd
commit 104f76d00d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -27,8 +27,7 @@ It is been used on big mobile video game titles sending and receiving tons of me
#include <ixwebsocket/IXUserAgent.h> #include <ixwebsocket/IXUserAgent.h>
#include <iostream> #include <iostream>
int main() int main() {
{
// Required on Windows // Required on Windows
ix::initNetSystem(); ix::initNetSystem();
@ -37,34 +36,36 @@ int main()
// Connect to a server with encryption // Connect to a server with encryption
// See https://machinezone.github.io/IXWebSocket/usage/#tls-support-and-configuration // See https://machinezone.github.io/IXWebSocket/usage/#tls-support-and-configuration
// https://github.com/machinezone/IXWebSocket/issues/386#issuecomment-1105235227 (self signed certificates) // https://github.com/machinezone/IXWebSocket/issues/386#issuecomment-1105235227 (self
// signed certificates)
std::string url("wss://echo.websocket.org"); std::string url("wss://echo.websocket.org");
webSocket.setUrl(url); webSocket.setUrl(url);
// Set TLS options (if needed)
ix::SocketTLSOptions tlsOptions{};
// Disables CA verification for demonstration purposes. Use cautiously!
tlsOptions.caFile = "NONE"; // No CA file used
webSocket.setTLSOptions(tlsOptions);
std::cout << "Connecting to " << url << "..." << std::endl; std::cout << "Connecting to " << url << "..." << std::endl;
// Setup a callback to be fired (in a background thread, watch out for race conditions !) // Setup a callback to be fired when a message or an event (open, close, error) is received
// when a message or an event (open, close, error) is received // This all happens in a background thread, so watch out for race conditions!
webSocket.setOnMessageCallback([](const ix::WebSocketMessagePtr& msg) webSocket.setOnMessageCallback([](const ix::WebSocketMessagePtr& msg) {
{ if (msg->type == ix::WebSocketMessageType::Message) {
if (msg->type == ix::WebSocketMessageType::Message) std::cout << "Received message: " << msg->str << std::endl;
{
std::cout << "received message: " << msg->str << std::endl;
std::cout << "> " << std::flush; std::cout << "> " << std::flush;
} } else if (msg->type == ix::WebSocketMessageType::Open) {
else if (msg->type == ix::WebSocketMessageType::Open) std::cout << "Connection established!" << std::endl;
{
std::cout << "Connection established" << std::endl;
std::cout << "> " << std::flush; std::cout << "> " << std::flush;
} } else if (msg->type == ix::WebSocketMessageType::Error) {
else if (msg->type == ix::WebSocketMessageType::Error)
{
// Maybe SSL is not configured properly // Maybe SSL is not configured properly
std::cout << "Connection error: " << msg->errorInfo.reason << std::endl; std::cout << "Connection error: " << msg->errorInfo.reason << std::endl;
std::cout << "> " << std::flush; std::cout << "> " << std::flush;
} }
} });
);
// Now that our callback is setup, we can start our background thread and receive messages // Now that our callback is setup, we can start our background thread and receive messages
webSocket.start(); webSocket.start();
@ -75,15 +76,23 @@ int main()
// Display a prompt // Display a prompt
std::cout << "> " << std::flush; std::cout << "> " << std::flush;
std::string text; std::string input{};
// Read text from the console and send messages in text mode. // Read text from the console and send messages in text mode.
// Exit with Ctrl-D on Unix or Ctrl-Z on Windows. while (getline(std::cin, input)) {
while (std::getline(std::cin, text)) if (input == "quit") {
{ std::cout << "Closing connection!" << std::endl;
webSocket.send(text); break;
std::cout << "> " << std::flush;
} }
webSocket.send(input);
}
// Ensure proper termination and cleanup
webSocket.stop();
ix::uninitNetSystem();
return 0; return 0;
} }
``` ```