-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppServer.h
85 lines (66 loc) · 2.06 KB
/
AppServer.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
//
// Created by root on 16-6-15.
//
#ifndef SPIN_APPSERVER_H
#define SPIN_APPSERVER_H
#include <set>
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include <thread>
typedef websocketpp::server<websocketpp::config::asio> server;
using websocketpp::connection_hdl;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
class AppServer {
public:
//std::string jsonObject;
connection_hdl newestHDL;
bool hdlReady = false;
void sendLoop(std::string &jsonObject)
{
std::cout << "Sending from websocket loop" << std::endl;
this->m_server.send(this->newestHDL, jsonObject, websocketpp::frame::opcode::TEXT);
sleep(2);
}
//std::thread serverT;
AppServer(){
m_server.init_asio();
m_server.set_open_handler(bind(&AppServer::on_open,this,::_1));
m_server.set_close_handler(bind(&AppServer::on_close,this,::_1));
m_server.set_message_handler(bind(&AppServer::on_message,this,::_1,::_2));
//serverT = std::thread(&sendLoop, this);
}
void on_open(connection_hdl hdl) {
//this->m_server.send(hdl, jsonObject, websocketpp::frame::opcode::TEXT);
this->newestHDL = hdl;
this->hdlReady = true;
}
void on_close(connection_hdl hdl) {
m_connections.erase(hdl);
}
void on_message(connection_hdl hdl, server::message_ptr msg) {
for (auto it : m_connections) {
m_server.send(it,msg);
}
}
void run(uint16_t port) {
m_server.listen(port);
m_server.start_accept();
m_server.run();
}
// void setJsonData(std::string input) {
// this->jsonObject = input;
// std::cout << "jsonObject: " << jsonObject << std::endl;
// }
~AppServer() {
//serverT.join();
m_server.stop_listening();
m_server.stop();
}
private:
typedef std::set<connection_hdl,std::owner_less<connection_hdl>> con_list;
server m_server;
con_list m_connections;
};
#endif //SPIN_APPSERVER_H