2019-04-09 06:52:20 +02:00
|
|
|
/*
|
|
|
|
* ws_cobra_subscribe.cpp
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
|
|
|
#include <atomic>
|
2019-04-21 20:20:17 +02:00
|
|
|
#include <ixcobra/IXCobraConnection.h>
|
2019-04-09 06:52:20 +02:00
|
|
|
|
2019-08-02 00:22:24 +02:00
|
|
|
#include <spdlog/spdlog.h>
|
|
|
|
|
2019-04-09 06:52:20 +02:00
|
|
|
namespace ix
|
|
|
|
{
|
|
|
|
int ws_cobra_subscribe_main(const std::string& appkey,
|
|
|
|
const std::string& endpoint,
|
|
|
|
const std::string& rolename,
|
|
|
|
const std::string& rolesecret,
|
2019-08-02 00:22:24 +02:00
|
|
|
const std::string& channel,
|
|
|
|
const std::string& filter,
|
|
|
|
bool quiet)
|
2019-04-09 06:52:20 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
ix::CobraConnection conn;
|
|
|
|
conn.configure(appkey, endpoint,
|
|
|
|
rolename, rolesecret,
|
|
|
|
ix::WebSocketPerMessageDeflateOptions(true));
|
|
|
|
conn.connect();
|
|
|
|
|
|
|
|
Json::FastWriter jsonWriter;
|
|
|
|
|
2019-08-02 00:22:24 +02:00
|
|
|
// Display incoming messages
|
|
|
|
std::atomic<int> msgPerSeconds(0);
|
|
|
|
std::atomic<int> msgCount(0);
|
|
|
|
|
|
|
|
auto timer = [&msgPerSeconds, &msgCount]
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
std::cout << "#messages " << msgCount << " "
|
|
|
|
<< "msg/s " << msgPerSeconds
|
|
|
|
<< std::endl;
|
|
|
|
|
|
|
|
msgPerSeconds = 0;
|
|
|
|
auto duration = std::chrono::seconds(1);
|
|
|
|
std::this_thread::sleep_for(duration);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::thread t(timer);
|
|
|
|
|
2019-04-09 06:52:20 +02:00
|
|
|
conn.setEventCallback(
|
2019-08-02 00:22:24 +02:00
|
|
|
[&conn, &channel, &jsonWriter, &filter, &msgCount, &msgPerSeconds, &quiet]
|
2019-04-09 06:52:20 +02:00
|
|
|
(ix::CobraConnectionEventType eventType,
|
|
|
|
const std::string& errMsg,
|
|
|
|
const ix::WebSocketHttpHeaders& headers,
|
|
|
|
const std::string& subscriptionId)
|
|
|
|
{
|
|
|
|
if (eventType == ix::CobraConnection_EventType_Open)
|
|
|
|
{
|
2019-08-02 00:22:24 +02:00
|
|
|
spdlog::info("Subscriber connected");
|
2019-04-19 18:48:46 +02:00
|
|
|
|
|
|
|
for (auto it : headers)
|
|
|
|
{
|
2019-08-02 00:22:24 +02:00
|
|
|
spdlog::info("{}: {}", it.first, it.second);
|
2019-04-19 18:48:46 +02:00
|
|
|
}
|
2019-04-09 06:52:20 +02:00
|
|
|
}
|
|
|
|
else if (eventType == ix::CobraConnection_EventType_Authenticated)
|
|
|
|
{
|
2019-08-02 00:22:24 +02:00
|
|
|
spdlog::info("Subscriber authenticated");
|
|
|
|
conn.subscribe(channel, filter,
|
|
|
|
[&jsonWriter, &quiet,
|
|
|
|
&msgPerSeconds, &msgCount](const Json::Value& msg)
|
2019-04-09 06:52:20 +02:00
|
|
|
{
|
2019-08-02 00:22:24 +02:00
|
|
|
if (!quiet)
|
|
|
|
{
|
|
|
|
std::cout << jsonWriter.write(msg) << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
msgPerSeconds++;
|
|
|
|
msgCount++;
|
2019-04-09 06:52:20 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else if (eventType == ix::CobraConnection_EventType_Subscribed)
|
|
|
|
{
|
2019-08-02 00:22:24 +02:00
|
|
|
spdlog::info("Subscriber: subscribed to channel {}", subscriptionId);
|
2019-04-09 06:52:20 +02:00
|
|
|
}
|
|
|
|
else if (eventType == ix::CobraConnection_EventType_UnSubscribed)
|
|
|
|
{
|
2019-08-02 00:22:24 +02:00
|
|
|
spdlog::info("Subscriber: unsubscribed from channel {}", subscriptionId);
|
2019-04-09 06:52:20 +02:00
|
|
|
}
|
|
|
|
else if (eventType == ix::CobraConnection_EventType_Error)
|
|
|
|
{
|
2019-08-02 00:22:24 +02:00
|
|
|
spdlog::error("Subscriber: error {}", errMsg);
|
2019-04-09 06:52:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
std::chrono::duration<double, std::milli> duration(10);
|
|
|
|
std::this_thread::sleep_for(duration);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|