diff --git a/TCPServer.cpp b/TCPServer.cpp index 116f63a..189c61c 100644 --- a/TCPServer.cpp +++ b/TCPServer.cpp @@ -163,7 +163,7 @@ void TCPServer::handleMessage(const std::string& message, int clientSocket) } if (tokens[2] == "get pos") { std::string toSend = "strat;all;set pos;" + std::to_string(this->robotPose.pos.x) + "," + std::to_string(this->robotPose.pos.y) + "," + std::to_string(this->robotPose.theta * 100) + "\n"; - this->broadcastMessage(toSend); + this->sendToClient(toSend, clientSocket); } if (tokens[2] == "spawn") { // TODO change that to handle spawn point @@ -401,3 +401,16 @@ void TCPServer::askArduinoPos() { usleep(500'000); } } + +void TCPServer::sendToClient(const char *message, int clientSocket) { + for (int client : clientSockets) { + if (client == clientSocket) { + send(client, message, strlen(message), 0); + break; + } + } +} + +void TCPServer::sendToClient(std::string &message, int clientSocket) { + this->sendToClient(message.c_str(), clientSocket); +} \ No newline at end of file diff --git a/TCPServer.h b/TCPServer.h index 100b1c0..94f31e4 100644 --- a/TCPServer.h +++ b/TCPServer.h @@ -70,6 +70,9 @@ public: void broadcastMessage(const char* message, int senderSocket = -1); // Modified method signature void broadcastMessage(std::string &message, int senderSocket = -1); // Modified method signature + void sendToClient(const char* message, int clientSocket); // New method to send message to a specific client + void sendToClient(std::string &message, int clientSocket); // New method to send message to a specific client + void handleMessage(const std::string& message, int clientSocket = -1); void clientDisconnected(int clientSocket); // New method to handle client disconnection