Chat System - Passing On the Message

Introduction: This tutorial is the tenth in my Java Network Programming using KryoNet series, or fifth in creating a chat client and server system, in which we are going to be sending the message to all other clients. Previous: In the previous tutorial we created a way to automatically remove clients that disconnect through a new disconnect listener on the server. The System: We are going to give the user a GUI to interact with the system - to send messages and see the currently connected members of the chat. When a client connects, add them to a list. Send incoming messages to everyone within the client list except the sender - or send them one back saying that it is received, as confirmation. This Tutorial: We can already send messages from one client to the server, but now we need a way for the messages received by the server to be sent to all the other clients connected to the same chat room - the chat room is the server for this tutorial, although we could have multiple chat rooms on one server. Receive Calling: Once the server receives a new message packet we want to call a method to send it to all the other clients connected. So on the packet message received listener within the server, we are going to run a method we are yet to create in our clientHandler class named CirculateMessage and parse it the required information gathered so far...
  1. public void received(Connection connection, Object object) {
  2.         if (object instanceof Packet) {
  3.                 if (object instanceof Packet1Connect) {
  4.                         Packet1Connect con = (Packet1Connect) object;
  5.                         System.out.println("[CONNECTED] " + con.name);
  6.                         clientHandler.addClient(new CustomClient(con.name, connection));
  7.                 }else if(object instanceof Packet2Message) {
  8.                         Packet2Message mes = (Packet2Message) object;
  9.                         System.out.println("[RECEIVED] [" + clientHandler.getClient(connection).getUsername() + "] " + mes.message);
  10.                         clientHandler.circulateMessage(connection, mes);
  11.                 }
  12.         }
  13. }
-As you can see, we now have the circulateMessage method call parsing the message packet received (which contains the message, already contained a packet which we can send) and the connection the packet was received from (the client's connection).- The Method: Next we need to create the method. In the ClientHandler class, we create it as a public void since it needs to be used by another class (public) and it does not need to return any values (void)...
  1. public void circulateMessage(Connection connection, Packet2Message packet) {
  2.         for (CustomClient c : this.clients) {
  3.                 if (c.getConnection().getID() != connection.getID())
  4.                         c.getConnection().sendTCP(packet);
  5.         }
  6. }
The above code simply iterates through each of the clients within the clients list and compares their unique connection ID with the ID of the connection that sent the packet originally. If they do not match, the packet is passed on. Update!: Ensure you add your client listener above sending our PacketConnect packet in order to avoid any communication problems with the server.

Add new comment