/** main.cpp* Author: Benjamin Sergeant* Copyright (c) 2020 Machine Zone, Inc. All rights reserved.** Super simple standalone example. See ws folder, unittest and doc/usage.md for more.** On macOS* $ mkdir -p build ; cd build ; cmake -DUSE_TLS=1 .. ; make -j ; make install* $ clang++ --std=c++14 --stdlib=libc++ main.cpp -lixwebsocket -lz -framework Security -framework Foundation* $ ./a.out** Or use cmake -DBUILD_DEMO=ON option for other platform*/#include <ixwebsocket/IXNetSystem.h>#include <ixwebsocket/IXWebSocket.h>#include <ixwebsocket/IXUserAgent.h>#include <iostream>int main(){// Required on Windowsix::initNetSystem();// Our websocket objectix::WebSocket webSocket;// Connect to a server with encryption// See https://machinezone.github.io/IXWebSocket/usage/#tls-support-and-configurationstd::string url("wss://echo.websocket.org");webSocket.setUrl(url);std::cout << ix::userAgent() << std::endl;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 receivedwebSocket.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 properlystd::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 messageswebSocket.start();// Send a message to the server (default to TEXT mode)webSocket.send("hello world");// Display a promptstd::cout << "> " << std::flush;std::string text;// Read text from the console and send messages in text mode.// Exit with Ctrl-D on Unix or Ctrl-Z on Windows.while (std::getline(std::cin, text)){webSocket.send(text);std::cout << "> " << std::flush;}return 0;}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。