39 #include <unordered_map>
43 #include "../exceptions.hpp"
44 #include "../responses/responses.hpp"
45 #include "../userconstants.hpp"
46 #include "../utils.hpp"
48 #include "rapidjson/include/rapidjson/document.h"
49 #include "rapidjson/include/rapidjson/rapidjson.h"
50 #include "rapidjson/include/rapidjson/writer.h"
53 namespace kiteconnect {
56 static_assert(std::numeric_limits<double>::is_iec559,
57 "Requires IEEE 754 floating point!");
60 namespace kc = kiteconnect;
74 std::function<void(
ticker* ws,
const std::vector<kc::tick>& ticks)>
onTicks;
84 std::function<void(
ticker* ws,
int code,
const string& message)>
onError;
126 explicit ticker(
string Key,
139 unsigned int ConnectTimeout = DEFAULT_CONNECT_TIMEOUT,
140 bool EnableReconnect =
false,
141 unsigned int MaxReconnectDelay = DEFAULT_MAX_RECONNECT_DELAY,
142 unsigned int MaxReconnectTries = DEFAULT_MAX_RECONNECT_TRIES);
187 std::chrono::time_point<std::chrono::system_clock>
getLastBeatTime()
const;
202 void subscribe(
const std::vector<int>& instrumentTokens);
210 void unsubscribe(
const std::vector<int>& instrumentTokens);
219 void setMode(
const string& mode,
const std::vector<int>& instrumentTokens);
222 friend class tickerTest_binaryParsingTest_Test;
223 const string connectUrlFmt =
224 "wss://ws.kite.trade/?api_key={0}&access_token={1}";
227 enum class SEGMENTS : int
245 const MODES DEFAULT_MODE = MODES::QUOTE;
246 std::unordered_map<int, MODES> subbedInstruments;
249 uWS::Group<uWS::CLIENT>* group;
251 uWS::WebSocket<uWS::CLIENT>* ws =
nullptr;
252 static constexpr
unsigned int DEFAULT_CONNECT_TIMEOUT = 5;
253 static constexpr
unsigned int DEFAULT_MAX_RECONNECT_DELAY = 60;
254 static constexpr
unsigned int DEFAULT_MAX_RECONNECT_TRIES = 30;
255 const unsigned int connectTimeout = DEFAULT_CONNECT_TIMEOUT;
256 const string pingMessage;
257 const unsigned int pingInterval = 3000;
258 const bool enableReconnect =
false;
259 const unsigned int initReconnectDelay = 2;
260 unsigned int reconnectDelay = initReconnectDelay;
261 const unsigned int maxReconnectDelay = DEFAULT_MAX_RECONNECT_DELAY;
262 unsigned int reconnectTries = 0;
263 const unsigned int maxReconnectTries = DEFAULT_MAX_RECONNECT_TRIES;
264 std::atomic<bool> isReconnecting {
false };
265 std::chrono::time_point<std::chrono::system_clock> lastPongTime;
266 std::chrono::time_point<std::chrono::system_clock> lastBeatTime;
268 void connectInternal();
272 void processTextMessage(
const string& message);
274 template <
typename T>
275 T unpack(
const std::vector<char>& bytes,
size_t start,
size_t end);
277 std::vector<std::vector<char>> splitPackets(
const std::vector<char>& bytes);
279 std::vector<kc::tick> parseBinaryMessage(
char* bytes,
size_t size);
281 void resubInstruments();
283 void assignCallbacks();
void connect()
Connect to the websocket server.
Definition: internal.hpp:81
std::function< void(ticker *ws, const kc::postback &postback)> onOrderUpdate
Called when an order update is received.
Definition: ws.hpp:77
std::function< void(ticker *ws)> onConnect
Called on successful connect.
Definition: ws.hpp:71
ticker wraps around the websocket API provided by KiteConnect and provides a native interface.
Definition: ws.hpp:66
std::function< void(ticker *ws, const string &message)> onMessage
Called when a message is received.
Definition: ws.hpp:80
void run()
Start the client. Should always be called after connect().
Definition: internal.hpp:93
void setAccessToken(const string &token)
Set the access token.
Definition: internal.hpp:77
void subscribe(const std::vector< int > &instrumentTokens)
Subscribe to a list of instrument tokens.
Definition: internal.hpp:99
Represents a postback.
Definition: ws.hpp:79
std::function< void(ticker *ws)> onConnectError
Called when an error occures while trying to connect.
Definition: ws.hpp:87
void setMode(const string &mode, const std::vector< int > &instrumentTokens)
Set the subscription mode for a list of instrument tokens.
Definition: internal.hpp:132
std::function< void(ticker *ws, unsigned int attemptCount)> onTryReconnect
Called when reconnection is being attempted.
Definition: ws.hpp:103
bool isConnected() const
Check if client is connected.
Definition: internal.hpp:86
std::function< void(ticker *ws, int code, const string &message)> onClose
Called when connection is closed.
Definition: ws.hpp:112
std::function< void(ticker *ws)> onReconnectFail
Called when reconnect attempts exceed maximum reconnect attempts set by user i.e.,...
Definition: ws.hpp:109
std::function< void(ticker *ws, int code, const string &message)> onError
Called when connection is closed with an error or websocket server sends an error message.
Definition: ws.hpp:84
void setApiKey(const string &key)
Set the API key.
Definition: internal.hpp:73
string getAccessToken() const
Get access token set at the moment.
Definition: internal.hpp:79
std::chrono::time_point< std::chrono::system_clock > getLastBeatTime() const
Get the last time heartbeat was received. Should be used in conjunction with the isConnected() method...
Definition: internal.hpp:89
string getApiKey() const
Get API key set at the moment.
Definition: internal.hpp:75
ticker(string Key, unsigned int ConnectTimeout=DEFAULT_CONNECT_TIMEOUT, bool EnableReconnect=false, unsigned int MaxReconnectDelay=DEFAULT_MAX_RECONNECT_DELAY, unsigned int MaxReconnectTries=DEFAULT_MAX_RECONNECT_TRIES)
Construct a new kiteWS object.
Definition: internal.hpp:64
std::function< void(ticker *ws, const std::vector< kc::tick > &ticks)> onTicks
Called when ticks are received.
Definition: ws.hpp:74
void stop()
Stop the client. Closes the connection if connected. Should be the last method that is called.
Definition: internal.hpp:95
void unsubscribe(const std::vector< int > &instrumentTokens)
Unsubscribe.
Definition: internal.hpp:115