diff --git a/examples/ws_connect/CMakeLists.txt b/examples/ws_connect/CMakeLists.txt new file mode 100644 index 00000000..64c95a79 --- /dev/null +++ b/examples/ws_connect/CMakeLists.txt @@ -0,0 +1,22 @@ +# +# Author: Benjamin Sergeant +# Copyright (c) 2018 Machine Zone, Inc. All rights reserved. +# + +cmake_minimum_required (VERSION 3.4.1) +project (ws_connect) + +set (CMAKE_CXX_STANDARD 11) + +option(USE_TLS "Add TLS support" ON) + +add_subdirectory(${PROJECT_SOURCE_DIR}/../.. ixwebsocket) + +add_executable(ws_connect ws_connect.cpp) + +if (APPLE AND USE_TLS) + target_link_libraries(ws_connect "-framework foundation" "-framework security") +endif() + +target_link_libraries(ws_connect ixwebsocket) +install(TARGETS ws_connect DESTINATION bin) diff --git a/examples/ws_connect/README.md b/examples/ws_connect/README.md new file mode 100644 index 00000000..5b520c46 --- /dev/null +++ b/examples/ws_connect/README.md @@ -0,0 +1,11 @@ +# Building + +1. mkdir build +2. cd build +3. cmake .. +4. make + +## Disable TLS + +* Enable: `cmake -DUSE_TLS=OFF ..` +* Disable: `cmake -DUSE_TLS=ON ..` diff --git a/examples/ws_connect/ws_connect.cpp b/examples/ws_connect/ws_connect.cpp new file mode 100644 index 00000000..7b5f8b33 --- /dev/null +++ b/examples/ws_connect/ws_connect.cpp @@ -0,0 +1,133 @@ +/* + * ws_connect.cpp + * Author: Benjamin Sergeant + * Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved. + */ + +#include +#include +#include +#include + +using namespace ix; + +namespace +{ + void log(const std::string& msg) + { + std::cout << msg << std::endl; + } + + class WebSocketConnect + { + public: + WebSocketConnect(const std::string& _url); + + void subscribe(const std::string& channel); + void start(); + void stop(); + + void sendMessage(const std::string& text); + + private: + std::string _url; + ix::WebSocket _webSocket; + }; + + WebSocketConnect::WebSocketConnect(const std::string& url) : + _url(url) + { + ; + } + + void WebSocketConnect::stop() + { + _webSocket.stop(); + } + + void WebSocketConnect::start() + { + _webSocket.configure(_url); + + std::stringstream ss; + log(std::string("Connecting to url: ") + _url); + + _webSocket.setOnMessageCallback( + [this](ix::WebSocketMessageType messageType, const std::string& str, ix::WebSocketErrorInfo error) + { + std::stringstream ss; + if (messageType == ix::WebSocket_MessageType_Open) + { + log("ws_connect: connected"); + } + else if (messageType == ix::WebSocket_MessageType_Close) + { + log("ws_connect: disconnected"); + } + else if (messageType == ix::WebSocket_MessageType_Message) + { + ss << "ws_connect: received message: " + << str; + log(ss.str()); + } + else if (messageType == ix::WebSocket_MessageType_Error) + { + ss << "Connection error: " << error.reason << std::endl; + ss << "#retries: " << error.retries << std::endl; + ss << "Wait time(ms): " << error.wait_time << std::endl; + ss << "HTTP Status: " << error.http_status << std::endl; + log(ss.str()); + } + else + { + ss << "Invalid ix::WebSocketMessageType"; + log(ss.str()); + } + }); + + _webSocket.start(); + } + + void WebSocketConnect::sendMessage(const std::string& text) + { + _webSocket.send(text); + } + + void interactiveMain(const std::string& url) + { + std::cout << "Type Ctrl-D to exit prompt..." << std::endl; + WebSocketConnect webSocketChat(url); + webSocketChat.start(); + + while (true) + { + std::string text; + std::cout << "> " << std::flush; + std::getline(std::cin, text); + + if (!std::cin) + { + break; + } + + webSocketChat.sendMessage(text); + } + + std::cout << std::endl; + webSocketChat.stop(); + } +} + +int main(int argc, char** argv) +{ + if (argc != 2) + { + std::cerr << "Usage: ws_connect " << std::endl; + return 1; + } + std::string url = argv[1]; + + Socket::init(); + interactiveMain(url); + return 0; +}