Created
July 28, 2016 00:50
-
-
Save z3t0/47268a10283282d0369e49ea6759da4f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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