forked from jojojames/PhxSocketCPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhxPush.h
145 lines (122 loc) · 3.72 KB
/
PhxPush.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* \file PhxPush.h
* \brief A class to represent a singular message/event pushed to a Phoenix
* Channel.
*
* Detailed description
*
*/
#ifndef PhxPush_H
#define PhxPush_H
#include "PhxTypes.h"
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <vector>
class PhxChannel;
class PhxPush : public std::enable_shared_from_this<PhxPush> {
private:
/*!< The Phoenix Channel messages are pushed to. */
std::shared_ptr<PhxChannel> channel;
/*!< The event name the server listens on. */
std::string event;
/*!< Name of event for the message. */
std::string refEvent;
/*!< Holds the payload that will be sent to the server. */
nlohmann::json payload;
/*!< The callback to trigger if event is not returned from server. */
After afterHook;
/*!< The interval to wait before triggering afterHook. */
int afterInterval;
/*!<
* recHooks contains a list of tuples where Item 1 is the Status
* and Item 2 is the callback.
*/
std::vector<std::tuple<std::string, OnMessage>> recHooks;
/*!< The response from server if server responded to sent message. */
nlohmann::json receivedResp;
/*!< Flag determining whether or not the message was sent through Sockets.
*/
bool sent;
/*!< Mutex used when setting this->shouldContinueAfterCallback. */
std::mutex afterTimerMutex;
/*!< Flag that determines if After callback will be triggered. */
bool shouldContinueAfterCallback;
/**
* \brief Stops listening for this event.
*
* \return void
*/
void cancelRefEvent();
/**
* \brief Cancels After callback from possibly triggering.
*
* \return void
*/
void cancelAfter();
/**
* \brief Starts the timer until After Callback is triggered.
*
* \return void
*/
void startAfter();
/**
* \brief Central function that kicks off OnMessage callbacks.
*
* \param payload Payload to match against.
* \return void
*/
void matchReceive(nlohmann::json payload);
public:
/**
* \brief Sets the payload that this class will push out through
* Websockets.
*
* \param payload
* \return void
*/
void setPayload(nlohmann::json payload);
/**
* \brief Constructor
*
* \param channel The Phoenix Channel to send to.
* \param event The Phoenix Event to post to.
* \param payload The Payload to send.
* \return PhxPush
*/
PhxPush(std::shared_ptr<PhxChannel> channel,
const std::string& event,
nlohmann::json payload);
/**
* \brief Sends Phoenix Formatted message with payload through Websockets.
*
* \return void
*/
void send();
/**
* \brief Adds a callback to be triggered for status.
*
* Adds a callback to be triggered when message matching status is posted.
*
* \param status The status that callback should respond to.
* \param callback The callback triggered when status message is posted.
* \return std::shared_ptr<PhxPush>
*/
std::shared_ptr<PhxPush> onReceive(
const std::string& status, OnMessage callback);
/**
* \brief Adds a callback to be triggered if event doesn't come back.
*
* Adds a callback to be triggered after ms if event is not `replied back`
* to.
* If PhxPush receives a message with matching event, callback will not
* be called.
*
* \param ms Milliseconds to wait before triggering callback.
* \param callback Callback to be triggered after ms has passed.
* \return std::shared_ptr<PhxPush>
*/
std::shared_ptr<PhxPush> after(int ms, After callback);
};
#endif // PhxPush_H