/* * ws_cobra_publish.cpp * Author: Benjamin Sergeant * Copyright (c) 2019 Machine Zone, Inc. All rights reserved. */ #include #include #include #include #include #include #include #include #include namespace ix { int ws_cobra_publish_main(const std::string& appkey, const std::string& endpoint, const std::string& rolename, const std::string& rolesecret, const std::string& channel, const std::string& path, const ix::SocketTLSOptions& tlsOptions) { std::ifstream f(path); std::string str((std::istreambuf_iterator(f)), std::istreambuf_iterator()); Json::Value data; Json::Reader reader; if (!reader.parse(str, data)) { spdlog::info("Input file is not a JSON file"); return 1; } ix::CobraConnection conn; conn.configure(appkey, endpoint, rolename, rolesecret, ix::WebSocketPerMessageDeflateOptions(true), tlsOptions); // Display incoming messages std::atomic authenticated(false); std::atomic messageAcked(false); conn.setEventCallback([&conn, &channel, &data, &authenticated, &messageAcked]( ix::CobraConnectionEventType eventType, const std::string& errMsg, const ix::WebSocketHttpHeaders& headers, const std::string& subscriptionId, CobraConnection::MsgId msgId) { if (eventType == ix::CobraConnection_EventType_Open) { spdlog::info("Publisher connected"); for (auto it : headers) { spdlog::info("{}: {}", it.first, it.second); } } else if (eventType == ix::CobraConnection_EventType_Authenticated) { spdlog::info("Publisher authenticated"); authenticated = true; Json::Value channels; channels[0] = channel; auto msgId = conn.publish(channels, data); spdlog::info("Published msg {}", msgId); } else if (eventType == ix::CobraConnection_EventType_Subscribed) { spdlog::info("Publisher: subscribed to channel {}", subscriptionId); } else if (eventType == ix::CobraConnection_EventType_UnSubscribed) { spdlog::info("Publisher: unsubscribed from channel {}", subscriptionId); } else if (eventType == ix::CobraConnection_EventType_Error) { spdlog::error("Publisher: error {}", errMsg); } else if (eventType == ix::CobraConnection_EventType_Published) { spdlog::info("Published message id {} acked", msgId); messageAcked = true; } else if (eventType == ix::CobraConnection_EventType_Pong) { spdlog::info("Received websocket pong"); } }); conn.connect(); while (!authenticated) ; while (!messageAcked) ; return 0; } } // namespace ix