-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNetwork.h
63 lines (55 loc) · 1.25 KB
/
Network.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
#pragma once
#include "HttpClient.h"
#include "easywsclient.hpp"
#include <assert.h>
namespace NetworkServer {
static easywsclient::WebSocket::pointer ws = NULL;
queue<string> MessageCache;
void handle_message(const string& message)
{
MessageCache.push(message);
}
static void LinkWS(string link) {
ws = easywsclient::WebSocket::from_url(link);
assert(ws);
}
static void WSSendMessage(string Message) {
ws->send(Message);
}
static void WSHandle() {
if (ws != NULL and ws->getReadyState() != easywsclient::WebSocket::CLOSED) {
ws->poll();
ws->dispatch(handle_message);
}
}
static bool WSState() {
if (ws != NULL) {
if (
ws->getReadyState() == easywsclient::WebSocket::CONNECTING or
ws->getReadyState() == easywsclient::WebSocket::OPEN
)
return true;
}
return false;
}
string GetHttpData(string url) {
HttpClient session;
HttpRequest req;
req.method = "GET";
req.url = url;
HttpResponse res;
int ret = session.Send(req, &res);
string retString = "";
if (ret != 0) {
retString = HttpClient::strerror(ret);
}
else {
retString = res.body;
}
return retString;
}
bool DownloadFile(string url, string file) {
HttpClient session;
return session.dl_curl_get_req(url, file);
}
}