Skip to content

Instantly share code, notes, and snippets.

@z3t0
Created July 28, 2016 00:50
Show Gist options
  • Select an option

  • Save z3t0/47268a10283282d0369e49ea6759da4f to your computer and use it in GitHub Desktop.

Select an option

Save z3t0/47268a10283282d0369e49ea6759da4f to your computer and use it in GitHub Desktop.
std::vector<Player> players (MAX_CONNECTIONS);
// pull out the type of messages sent by our config
typedef server::message_ptr message_ptr;
// Define a callback to handle incoming messages
void on_message(server* s, websocketpp::connection_hdl hdl, message_ptr msg) {
std::cout << "on_message called with hdl: " << hdl.lock().get()
<< " and message: " << msg->get_payload()
<< std::endl;
// Craete player if there are no players
if (players.size() == 0){
// First PLayer
players.push_back(Player(hdl, "#6666ff"));
}
else {
// Check if player exists
for (int i = 0; i < players.size(); i++) {
if (players[i].GetConnection() == hdl) {
std:: cout << "player exits: " << hdl.lock().get();
}
}
}
// check for a special command to instruct the server to stop listening so
// it can be cleanly exited.
if (msg->get_payload() == "stop-listening") {
s->stop_listening();
return;
}
try {
s->send(hdl, msg->get_payload(), msg->get_opcode());
} catch (const websocketpp::lib::error_code& e) {
std::cout << "Echo failed because: " << e
<< "(" << e.message() << ")" << std::endl;
}
}
#ifndef PLAYER_H
#define PLAYER_H
#include <iostream>
#include <websocketpp/server.hpp>
#include "game.hpp"
class Player {
private:
Position pos;
std::string color;
websocketpp::connection_hdl con;
public:
Position GetPosition() {
return pos;
}
void SetPosition(Position pos)
{
this->pos = pos;
}
websocketpp::connection_hdl GetConnection()
{
return con;
}
Player();
Player(websocketpp::connection_hdl, std::string);
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment