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" 22 #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 );
74 sio::packet packet(
"/", msg );
75 manager.encode( packet, [&](
bool isBinary, std::shared_ptr<const std::string>
const & json) {
83 std::string result = ss.str();
84 std::size_t indexList = result.find(
'[' );
85 std::size_t indexObject = result.find(
'{' );
86 std::size_t indexString = result.find(
'"' );
87 std::size_t index = indexList;
88 if ( indexObject != std::string::npos && indexObject < index )
90 if ( indexString != std::string::npos && indexString < index )
93 if ( index == std::string::npos ) {
94 std::cerr <<
"Error decoding json object" << std::endl <<
" Body: " << result << std::endl;
97 return result.substr(index);
116 std::string serverURL =
bridges.getServerURL();
119 std::cerr <<
"connecting SocketIO with " << serverURL <<
"\n";
122 client.connect(serverURL);
133 std::lock_guard< std::mutex > guard( _lock );
135 key_listeners.push_back(
p);
139 std::lock_guard< std::mutex > guard( _lock );
141 std::string jsonmsg = getJSON(
e.get_message());
144 std::cerr <<
"forwardKeyUp:" <<
e.get_nsp() <<
" " <<
e.get_name() <<
" : " << jsonmsg <<
"\n";
146 for (
auto& ptr : key_listeners)
151 std::lock_guard< std::mutex > guard( _lock );
153 std::string jsonmsg = getJSON(
e.get_message());
156 std::cerr <<
"forwardKeyDown:" <<
e.get_nsp() <<
" " <<
e.get_name() <<
" : " << jsonmsg <<
"\n";
158 for (
auto& ptr : key_listeners)
159 ptr->keydown(jsonmsg);
164 client.clear_con_listeners();
169 current_socket = client.socket();
173 current_socket->on(
"announcement", std::bind(&
SocketConnection::on_announcement,
this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
182 std::cerr <<
"Sending credentials\n";
184 std::string user =
bridges.getUserName();
185 std::string apikey =
bridges.getApiKey();
187 auto assID =
bridges.getAssignment();
189 current_socket->emit(
"credentials",
190 "{\"user\":\"" + user
191 +
"\",\"apikey\":\"" + apikey
192 +
"\",\"assignment\":\"" + std::to_string(assID)
195 std::cerr <<
"Credentials sent\n";
199 std::cout<<
"reconnect..."<<std::endl;
204 std::lock_guard< std::mutex > guard( _lock );
206 std::cout<<
"sockopen on namespace "<<nsp<<std::endl;
212 std::lock_guard< std::mutex > guard( _lock );
214 std::cout <<
"connected!!\n";
217 connect_finish =
true;
221 void on_close(sio::client::close_reason
const& reason) {
222 std::cout <<
"sio closed\n";
227 std::cout <<
"socket errror\n";
232 std::cout <<
"sio failed\n";
236 void on_announcement (std::string
const& name, sio::message::ptr
const& data,
bool isAck, sio::message::list &ack_resp) {
238 std::cerr <<
"announcement something\n";
244 if (!connect_finish) {
252 if (debug && debugVerbose)
253 std::cerr <<
"Sending GameGrid\n";
256 current_socket->emit(
"gamegrid:recv", gridjson);
void on_connected()
Definition: SocketConnection.h:211
This is a class in BRIDGES for representing an (n x n)game grid.
Definition: GameGrid.h:400
void forwardKeyUp(sio::event &e)
Definition: SocketConnection.h:138
void reconfigure_socket()
Definition: SocketConnection.h:168
virtual void keydown(std::string JSONmessage)=0
void sendCredentials()
Definition: SocketConnection.h:179
void forwardKeyDown(sio::event &e)
Definition: SocketConnection.h:150
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:48
This class contains methods to connect and transmit a user's data structure representation to the Bri...
Definition: Bridges.h:42
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
void registerKeyListener(KeypressListener *p)
Definition: SocketConnection.h:132
void wait_on_connection()
Definition: SocketConnection.h:241
virtual void keyup(std::string JSONmessage)=0
void on_socketopen(const std::string &nsp)
Definition: SocketConnection.h:203
SocketConnection(bridges::Bridges &b)
Definition: SocketConnection.h:104
void on_announcement(std::string const &name, sio::message::ptr const &data, bool isAck, sio::message::list &ack_resp)
Definition: SocketConnection.h:236
void on_socketerror(sio::message::ptr const &message)
Definition: SocketConnection.h:226
void sendGameGrid(const GameGrid &gg)
Definition: SocketConnection.h:251
void on_fail()
Definition: SocketConnection.h:231
void on_close(sio::client::close_reason const &reason)
Definition: SocketConnection.h:221
void on_reconnect(unsigned, unsigned)
Definition: SocketConnection.h:198
This is meant to be an internal class, not something that the library user will use....
Definition: SocketConnection.h:34
~SocketConnection()
Definition: SocketConnection.h:163