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>
37 virtual void keyup(std::string JSONmessage) = 0;
38 virtual void keydown(std::string JSONmessage) = 0;
51 bool debugVerbose =
false;
53 sio::socket::ptr current_socket;
59 std::condition_variable_any _cond;
60 bool connect_finish =
false;
62 std::list<KeypressListener*> key_listeners;
67 std::string getJSON( sio::message::ptr msg ) {
69 static sio::packet_manager manager;
70 static std::mutex packetLock;
71 std::lock_guard< std::mutex > guard( packetLock );
75 sio::packet packet(
"/", msg );
76 manager.encode( packet, [&](
bool isBinary, std::shared_ptr<const std::string>
const & json) {
84 std::string result = ss.str();
85 std::size_t indexList = result.find(
'[' );
86 std::size_t indexObject = result.find(
'{' );
87 std::size_t indexString = result.find(
'"' );
88 std::size_t index = indexList;
89 if ( indexObject != std::string::npos && indexObject < index )
91 if ( indexString != std::string::npos && indexString < index )
94 if ( index == std::string::npos ) {
95 std::cerr <<
"Error decoding json object" << std::endl <<
" Body: " << result << std::endl;
98 return result.substr(index);
117 std::string serverURL =
bridges.getServerURL();
120 std::cerr <<
"connecting SocketIO with " << serverURL <<
"\n";
123 client.connect(serverURL);
134 std::lock_guard< std::mutex > guard( _lock );
136 key_listeners.push_back(
p);
140 std::lock_guard< std::mutex > guard( _lock );
142 std::string jsonmsg = getJSON(
e.get_message());
145 std::cerr <<
"forwardKeyUp:" <<
e.get_nsp() <<
" " <<
e.get_name() <<
" : " << jsonmsg <<
"\n";
147 for (
auto& ptr : key_listeners)
152 std::lock_guard< std::mutex > guard( _lock );
154 std::string jsonmsg = getJSON(
e.get_message());
157 std::cerr <<
"forwardKeyDown:" <<
e.get_nsp() <<
" " <<
e.get_name() <<
" : " << jsonmsg <<
"\n";
159 for (
auto& ptr : key_listeners)
160 ptr->keydown(jsonmsg);
165 client.clear_con_listeners();
170 current_socket = client.socket();
174 current_socket->on(
"announcement", std::bind(&
SocketConnection::on_announcement,
this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
183 std::cerr <<
"Sending credentials\n";
185 std::string user =
bridges.getUserName();
186 std::string apikey =
bridges.getApiKey();
188 auto assID =
bridges.getAssignment();
190 current_socket->emit(
"credentials",
191 "{\"user\":\"" + user
192 +
"\",\"apikey\":\"" + apikey
193 +
"\",\"assignment\":\"" + std::to_string(assID)
196 std::cerr <<
"Credentials sent\n";
200 std::cout<<
"reconnect..."<<std::endl;
205 std::lock_guard< std::mutex > guard( _lock );
207 std::cout<<
"sockopen on namespace "<<nsp<<std::endl;
213 std::lock_guard< std::mutex > guard( _lock );
215 std::cout <<
"connected!!\n";
218 connect_finish =
true;
222 void on_close(sio::client::close_reason
const& reason) {
223 std::cout <<
"sio closed\n";
228 std::cout <<
"socket errror\n";
233 std::cout <<
"sio failed\n";
237 void on_announcement (std::string
const& name, sio::message::ptr
const& data,
bool isAck, sio::message::list &ack_resp) {
239 std::cerr <<
"announcement something\n";
245 if (!connect_finish) {
253 if (debug && debugVerbose)
254 std::cerr <<
"Sending GameGrid\n";
257 current_socket->emit(
"gamegrid:recv", gridjson);
261 if (debug && debugVerbose)
262 std::cerr <<
"Sending Scene\n";
263 std::string scenejson =
"{" +
s.getDataStructureRepresentation();
265 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:42
This is a class in BRIDGES for representing an (n x n)game grid.
Definition: GameGrid.h:400
virtual const string getDataStructureRepresentation() const override
Definition: GameGrid.h:529
This is meant to be an internal class, not something that the library user will use....
Definition: SocketConnection.h:35
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:49
void sendSceneDataToServer(const Scene &s)
Definition: SocketConnection.h:260
void reconfigure_socket()
Definition: SocketConnection.h:169
void sendCredentials()
Definition: SocketConnection.h:180
void forwardKeyDown(sio::event &e)
Definition: SocketConnection.h:151
void on_fail()
Definition: SocketConnection.h:232
void on_connected()
Definition: SocketConnection.h:212
SocketConnection(bridges::Bridges &b)
Definition: SocketConnection.h:105
void registerKeyListener(KeypressListener *p)
Definition: SocketConnection.h:133
void on_announcement(std::string const &name, sio::message::ptr const &data, bool isAck, sio::message::list &ack_resp)
Definition: SocketConnection.h:237
void on_socketopen(const std::string &nsp)
Definition: SocketConnection.h:204
void wait_on_connection()
Definition: SocketConnection.h:242
void on_close(sio::client::close_reason const &reason)
Definition: SocketConnection.h:222
void on_reconnect(unsigned, unsigned)
Definition: SocketConnection.h:199
void sendDataToServer(const GameGrid &gg)
Definition: SocketConnection.h:252
~SocketConnection()
Definition: SocketConnection.h:164
void on_socketerror(sio::message::ptr const &message)
Definition: SocketConnection.h:227
void forwardKeyUp(sio::event &e)
Definition: SocketConnection.h:139
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4