1 #ifndef SOCKET_CONNECTION_H
2 #define SOCKET_CONNECTION_H
8 #define BOOST_DATE_TIME_NO_LIB
9 #define BOOST_REGEX_NO_LIB
10 #define ASIO_STANDALONE
11 #define _WEBSOCKETPP_CPP11_STL_
12 #define _WEBSOCKETPP_CPP11_FUNCTIONAL_
15 #include "sio_client.h"
16 #include "internal/sio_packet.h"
23 #include <condition_variable>
36 virtual void keyup(std::string JSONmessage) = 0;
37 virtual void keydown(std::string JSONmessage) = 0;
50 bool debugVerbose =
false;
52 sio::socket::ptr current_socket;
58 std::condition_variable_any _cond;
59 bool connect_finish =
false;
61 std::list<KeypressListener*> key_listeners;
66 std::string getJSON( sio::message::ptr msg ) {
68 static sio::packet_manager manager;
69 static std::mutex packetLock;
70 std::lock_guard< std::mutex > guard( packetLock );
73 sio::packet packet(
"/", msg );
74 manager.encode( packet, [&](
bool isBinary, std::shared_ptr<const std::string>
const & json) {
82 std::string result = ss.str();
83 std::size_t indexList = result.find(
'[' );
84 std::size_t indexObject = result.find(
'{' );
85 std::size_t indexString = result.find(
'"' );
86 std::size_t index = indexList;
87 if ( indexObject != std::string::npos && indexObject < index )
89 if ( indexString != std::string::npos && indexString < index )
92 if ( index == std::string::npos ) {
93 std::cerr <<
"Error decoding json object" << std::endl <<
" Body: " << result << std::endl;
96 return result.substr(index);
113 std::string serverURL =
bridges.getServerURL();
116 std::cerr <<
"connecting SocketIO with " << serverURL <<
"\n";
119 client.connect(serverURL);
130 std::lock_guard< std::mutex > guard( _lock );
132 key_listeners.push_back(
p);
136 std::lock_guard< std::mutex > guard( _lock );
138 std::string jsonmsg = getJSON(
e.get_message());
141 std::cerr <<
"forwardKeyUp:" <<
e.get_nsp() <<
" " <<
e.get_name() <<
" : " << jsonmsg <<
"\n";
143 for (
auto& ptr : key_listeners)
148 std::lock_guard< std::mutex > guard( _lock );
150 std::string jsonmsg = getJSON(
e.get_message());
153 std::cerr <<
"forwardKeyDown:" <<
e.get_nsp() <<
" " <<
e.get_name() <<
" : " << jsonmsg <<
"\n";
155 for (
auto& ptr : key_listeners)
156 ptr->keydown(jsonmsg);
160 client.clear_con_listeners();
165 current_socket = client.socket();
169 current_socket->on(
"announcement", std::bind(&
SocketConnection::on_announcement,
this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
178 std::cerr <<
"Sending credentials\n";
180 std::string user =
bridges.getUserName();
181 std::string apikey =
bridges.getApiKey();
183 auto assID =
bridges.getAssignment();
185 current_socket->emit(
"credentials",
186 "{\"user\":\"" + user
187 +
"\",\"apikey\":\"" + apikey
188 +
"\",\"assignment\":\"" + std::to_string(assID)
191 std::cerr <<
"Credentials sent\n";
195 std::cout <<
"reconnect..." << std::endl;
200 std::lock_guard< std::mutex > guard( _lock );
202 std::cout <<
"sockopen on namespace " << nsp << std::endl;
208 std::lock_guard< std::mutex > guard( _lock );
210 std::cout <<
"connected!!\n";
213 connect_finish =
true;
216 void on_close(sio::client::close_reason
const& reason) {
217 std::cout <<
"sio closed\n";
222 std::cout <<
"socket errror\n";
227 std::cout <<
"sio failed\n";
231 void on_announcement (std::string
const& name, sio::message::ptr
const& data,
bool isAck, sio::message::list &ack_resp) {
233 std::cerr <<
"announcement something\n";
239 if (!connect_finish) {
247 if (debug && debugVerbose)
248 std::cerr <<
"Sending GameGrid\n";
251 current_socket->emit(
"gamegrid:recv", gridjson);
255 if (debug && debugVerbose)
256 std::cerr <<
"Sending Scene\n";
257 std::string scenejson =
"{" +
s.getDataStructureRepresentation();
259 current_socket->emit(
"scene:recv", scenejson);
This class contains methods to connect and transmit a user's data structure representation to the Bri...
Definition: Bridges.h:50
This is a class in BRIDGES for representing an (n x n)game grid.
Definition: GameGrid.h:398
virtual const string getDataStructureRepresentation() const override
Definition: GameGrid.h:522
This is meant to be an internal class, not something that the library user will use....
Definition: SocketConnection.h:34
virtual void keyup(std::string JSONmessage)=0
virtual void keydown(std::string JSONmessage)=0
This is meant to be an internal class, not something that the library user will use.
Definition: SocketConnection.h:48
void sendSceneDataToServer(const Scene &s)
Definition: SocketConnection.h:254
void reconfigure_socket()
Definition: SocketConnection.h:164
void sendCredentials()
Definition: SocketConnection.h:175
void forwardKeyDown(sio::event &e)
Definition: SocketConnection.h:147
void on_fail()
Definition: SocketConnection.h:226
void on_connected()
Definition: SocketConnection.h:207
SocketConnection(bridges::Bridges &b)
Definition: SocketConnection.h:103
void registerKeyListener(KeypressListener *p)
Definition: SocketConnection.h:129
void on_announcement(std::string const &name, sio::message::ptr const &data, bool isAck, sio::message::list &ack_resp)
Definition: SocketConnection.h:231
void on_socketopen(const std::string &nsp)
Definition: SocketConnection.h:199
void wait_on_connection()
Definition: SocketConnection.h:236
void on_close(sio::client::close_reason const &reason)
Definition: SocketConnection.h:216
void on_reconnect(unsigned, unsigned)
Definition: SocketConnection.h:194
void sendDataToServer(const GameGrid &gg)
Definition: SocketConnection.h:246
~SocketConnection()
Definition: SocketConnection.h:159
void on_socketerror(sio::message::ptr const &message)
Definition: SocketConnection.h:221
void forwardKeyUp(sio::event &e)
Definition: SocketConnection.h:135
Support for drawing Bar charts.
Definition: alltypes.h:4