diff --git a/TCPServer.cpp b/TCPServer.cpp index d70c4ae..32ad706 100644 --- a/TCPServer.cpp +++ b/TCPServer.cpp @@ -163,13 +163,13 @@ void TCPServer::handleMessage(const std::string& message, int clientSocket) this->broadcastMessage(message.c_str(), 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); + 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) + "\n"; this->broadcastMessage(toSend.c_str(), clientSocket); } if (tokens[2] == "spawn") { // TODO change that to handle spawn point this->robotPose = {500, 500, -1.57079}; - const 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); + const 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.c_str(), clientSocket); } @@ -210,15 +210,20 @@ void TCPServer::handleMessage(const std::string& message, int clientSocket) void TCPServer::broadcastMessage(const char* message, int senderSocket) { - for (int clientSocket : clientSockets) { - if (clientSocket != senderSocket) { // Exclude the sender's socket - send(clientSocket, message, strlen(message), 0); - } - } + std::string temp = message; + this->broadcastMessage(temp, senderSocket); } -void TCPServer::broadcastMessage(const std::string &message, int senderSocket) { - this->broadcastMessage(message.c_str(), senderSocket); +void TCPServer::broadcastMessage(std::string &message, int senderSocket) { + if (!endsWith(message, "\n")) { + message += "\n"; + } + + for (int clientSocket : clientSockets) { + if (clientSocket != senderSocket) { // Exclude the sender's socket + send(clientSocket, message.c_str(), message.size(), 0); + } + } } void TCPServer::clientDisconnected(const int clientSocket) { diff --git a/TCPServer.h b/TCPServer.h index 3ddc19f..c769e54 100644 --- a/TCPServer.h +++ b/TCPServer.h @@ -68,7 +68,7 @@ public: // Broadcast message to all connected clients void broadcastMessage(const char* message, int senderSocket = -1); // Modified method signature - void broadcastMessage(const std::string& message, int senderSocket = -1); // Modified method signature + void broadcastMessage(std::string &message, int senderSocket = -1); // Modified method signature void handleMessage(const std::string& message, int clientSocket = -1);