|
| 1 | +/* |
| 2 | + * ws_echo_client.cpp |
| 3 | + * Author: Benjamin Sergeant |
| 4 | + * Copyright (c) 2020 Machine Zone, Inc. All rights reserved. |
| 5 | + */ |
| 6 | + |
| 7 | +#include <iostream> |
| 8 | +#include <ixcore/utils/IXCoreLogger.h> |
| 9 | +#include <ixwebsocket/IXNetSystem.h> |
| 10 | +#include <ixwebsocket/IXSetThreadName.h> |
| 11 | +#include <ixwebsocket/IXWebSocket.h> |
| 12 | +#include <spdlog/spdlog.h> |
| 13 | +#include <sstream> |
| 14 | +#include <thread> |
| 15 | + |
| 16 | +namespace ix |
| 17 | +{ |
| 18 | + int ws_echo_client(const std::string& url, |
| 19 | + bool disablePerMessageDeflate, |
| 20 | + bool binaryMode, |
| 21 | + const ix::SocketTLSOptions& tlsOptions, |
| 22 | + const std::string& subprotocol, |
| 23 | + int pingIntervalSecs, |
| 24 | + const std::string& sendMsg) |
| 25 | + { |
| 26 | + // Our websocket object |
| 27 | + ix::WebSocket webSocket; |
| 28 | + |
| 29 | + webSocket.setUrl(url); |
| 30 | + webSocket.setTLSOptions(tlsOptions); |
| 31 | + webSocket.setPingInterval(pingIntervalSecs); |
| 32 | + |
| 33 | + if (disablePerMessageDeflate) |
| 34 | + { |
| 35 | + webSocket.disablePerMessageDeflate(); |
| 36 | + } |
| 37 | + |
| 38 | + if (!subprotocol.empty()) |
| 39 | + { |
| 40 | + webSocket.addSubProtocol(subprotocol); |
| 41 | + } |
| 42 | + |
| 43 | + std::atomic<uint64_t> receivedCount(0); |
| 44 | + uint64_t receivedCountTotal(0); |
| 45 | + uint64_t receivedCountPerSecs(0); |
| 46 | + |
| 47 | + // Setup a callback to be fired (in a background thread, watch out for race conditions !) |
| 48 | + // when a message or an event (open, close, error) is received |
| 49 | + webSocket.setOnMessageCallback( |
| 50 | + [&webSocket, &receivedCount, &sendMsg, binaryMode](const ix::WebSocketMessagePtr& msg) { |
| 51 | + if (msg->type == ix::WebSocketMessageType::Message) |
| 52 | + { |
| 53 | + webSocket.send(msg->str, msg->binary); |
| 54 | + receivedCount++; |
| 55 | + } |
| 56 | + else if (msg->type == ix::WebSocketMessageType::Open) |
| 57 | + { |
| 58 | + spdlog::info("ws_echo_client: connected"); |
| 59 | + spdlog::info("Uri: {}", msg->openInfo.uri); |
| 60 | + spdlog::info("Headers:"); |
| 61 | + for (auto it : msg->openInfo.headers) |
| 62 | + { |
| 63 | + spdlog::info("{}: {}", it.first, it.second); |
| 64 | + } |
| 65 | + |
| 66 | + webSocket.send(sendMsg, binaryMode); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + auto timer = [&receivedCount, &receivedCountTotal, &receivedCountPerSecs] { |
| 71 | + setThreadName("Timer"); |
| 72 | + while (true) |
| 73 | + { |
| 74 | + // |
| 75 | + // We cannot write to sentCount and receivedCount |
| 76 | + // as those are used externally, so we need to introduce |
| 77 | + // our own counters |
| 78 | + // |
| 79 | + std::stringstream ss; |
| 80 | + ss << "messages received: " << receivedCountPerSecs << " per second " |
| 81 | + << receivedCountTotal << " total"; |
| 82 | + |
| 83 | + CoreLogger::info(ss.str()); |
| 84 | + |
| 85 | + receivedCountPerSecs = receivedCount - receivedCountTotal; |
| 86 | + receivedCountTotal += receivedCountPerSecs; |
| 87 | + |
| 88 | + auto duration = std::chrono::seconds(1); |
| 89 | + std::this_thread::sleep_for(duration); |
| 90 | + } |
| 91 | + }; |
| 92 | + |
| 93 | + std::thread t1(timer); |
| 94 | + |
| 95 | + // Now that our callback is setup, we can start our background thread and receive messages |
| 96 | + std::cout << "Connecting to " << url << "..." << std::endl; |
| 97 | + webSocket.start(); |
| 98 | + |
| 99 | + // Send a message to the server (default to TEXT mode) |
| 100 | + webSocket.send("hello world"); |
| 101 | + |
| 102 | + while (true) |
| 103 | + { |
| 104 | + std::string text; |
| 105 | + std::cout << "> " << std::flush; |
| 106 | + std::getline(std::cin, text); |
| 107 | + |
| 108 | + webSocket.send(text); |
| 109 | + } |
| 110 | + |
| 111 | + return 0; |
| 112 | + } |
| 113 | +} // namespace ix |
0 commit comments