A simple chat application
We have the basic framework in place to handle connections, so let's build something with it! How about a neat little console-based chat program? Let's start with the server by creating a separate project and a new file called Server_Main.cpp
. The first thing we're going to need is a packet handler:
void Handler(sf::IpAddress& l_ip, const PortNumber& l_port, const PacketID& l_id, sf::Packet& l_packet, Server* l_server) { ClientID id = l_server->GetClientID(l_ip, l_port); if (id >= 0){ if ((PacketType)l_id == PacketType::Disconnect){ l_server->RemoveClient(l_ip, l_port); sf::Packet p; StampPacket(PacketType::Message, p); std::string message; message = "Client left! " + l_ip.toString() + ":" + std::to_string(l_port); p << message; l_server->Broadcast(p, id); } else if ((PacketType)l_id == PacketType::Message){ std::string receivedMessage...