forked from niv/pusher-websocket-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpushcpp_eventloop.cpp
52 lines (38 loc) · 1.13 KB
/
pushcpp_eventloop.cpp
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
#include "pushcpp_internal.h"
void pushcpp::EventThread()
{
while (true) {
/* attempt to connect */
DEBUG("polling thread started");
while (
this->m_websocket == NULL ||
((WebSocket::pointer)this->m_websocket)->
getReadyState() == WebSocket::CLOSED
) {
DEBUG("Attempting to connect!");
if (this->m_websocket != NULL)
delete((WebSocket::pointer) this->m_websocket);
this->m_websocket = (void*) WebSocket::from_url(m_url);
}
WebSocket::pointer ws = (WebSocket::pointer) this->m_websocket;
DEBUG("connected, (re)subscribing to channels");
while (ws->getReadyState() != WebSocket::CLOSED) {
ws->poll(100);
ws->dispatch([this, ws](const string & msg) {
WS_Dispatch(msg);
});
if (m_wantDisconnect)
ws->close();
}
DEBUG("Lost connection, readyState: %d", ws->getReadyState());
this->m_socketId = "";
for (auto it = m_channelData.begin(); it != m_channelData.end(); it++)
it->second.clear();
if (m_connectionEventHandler)
m_connectionEventHandler(ConnectionEvent::DISCONNECTED);
if (m_wantDisconnect)
break;
}
m_wantDisconnect = false;
DEBUG("thread was stopped");
}