-
Notifications
You must be signed in to change notification settings - Fork 0
/
lp.cpp
84 lines (79 loc) · 1.59 KB
/
lp.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
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
// longpoll updates
#include "common.h"
#include <iostream>
string server, key, ts;
string old_ts = "";
void lp::getServer()
{
json longpollinfo = vk::send("messages.getLongPollServer");
server = longpollinfo["response"]["server"];
key = longpollinfo["response"]["key"];
ts = to_string((int)longpollinfo["response"]["ts"]);
}
void lp::loop()
{
getServer();
while(true)
{
table params = {
{ "key", key },
{ "ts", ts },
{ "wait", "90" },//25
{ "mode", "2" },
{ "version", "2" },
{ "act", "a_check" }
};
json data = json::parse(net::send("https://" + server, params));
if(!data["ts"].is_null()) ts = to_string((int)data["ts"]);
if (!data["failed"].is_null())
{
if(!lp::errors(data)) break;
continue;
}
else if(old_ts != ts)
{
lp::updates(data["updates"]);
}
old_ts = ts;
other::sleep(50);
}
}
bool lp::errors(json lp_data)
{
switch ((int)lp_data["failed"])
{
case 1:
cout << "LongPoll: " << "Updating ts..." << endl;
break;
case 2:
cout << "LongPoll: " << "Key is out of date" << endl;
lp::getServer();
break;
case 3:
cout << "LongPoll: " << "Information about the user is lost" << endl;
lp::getServer();
break;
case 4:
cout << "LongPoll: " << "Invalid api version" << endl;
return false;
break;
default:
cout << "LongPoll: " << "Reconnect" << endl;
lp::getServer();
break;
}
return true;
}
void lp::updates(json updates)
{
for(auto update : updates)
{
if(update[0].is_null() || update[1].is_null())continue;
switch((int)update[0])
{
case 4: // messange
message::load(update);
break;
}
}
}