forked from niv/pusher-websocket-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpushcpp.cpp
More file actions
76 lines (66 loc) · 1.66 KB
/
pushcpp.cpp
File metadata and controls
76 lines (66 loc) · 1.66 KB
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
#include "pushcpp_internal.h"
pushcpp::pushcpp(const string &appKey,
std::function<void(const ConnectionEvent ce, const std::string&)> ch,
std::function<void(const int, const std::string&)> eh,
std::function<void(const PingEvent p)> pe,
const std::string &cluster)
: request_connection_(false) {
this->m_connectionEventHandler = ch;
this->m_errorEventHandler = eh;
this->m_pingEventHandler = pe;
stringstream str;
str << "ws://ws";
if (!cluster.empty()) {
str << "-";
str << cluster;
}
str << ".pusher.com:80/app/";
str << appKey;
str << "?client=pushcpp&version=1.0&protocol=5";
m_url = str.str();
}
void pushcpp::connect()
{
request_connection_ = true;
m_wantDisconnect = false;
}
bool pushcpp::connected() const
{
return
this->m_websocket != NULL && (
((WebSocket::pointer) this->m_websocket)->
getReadyState() == WebSocket::OPEN
);
}
void pushcpp::disconnect(bool wait)
{
m_wantDisconnect = true;
}
bool pushcpp::sendRaw(const string &raw)
{
WebSocket::pointer ws = (WebSocket::pointer) this->m_websocket;
if (ws != NULL && ws->getReadyState() == WebSocket::OPEN) {
DEBUG("send: %s", raw.c_str());
ws->send(raw);
return true;
}
return false;
}
bool pushcpp::send(
const string &channel,
const string &event,
const string &data
)
{
json_t *json = json_object();
json_object_set_new(json, "event", json_string(event.c_str()));
if (channel != "")
json_object_set_new(json, "channel", json_string(channel.c_str()));
json_object_set_new(json, "data", json_string(data.c_str()));
char *dumped = json_dumps(json, 0);
assert(dumped);
json_decref(json);
bool ret = sendRaw(dumped);
free(dumped);
return ret;
}