I created a simple chat server that writes to all clients that is connected (I posted just the core code for simplicity)
public class server extends Thread {
private Socket clientSocket;
private static ArrayList<Socket> sockets = new ArrayList<Socket>();
public server(Socket clientSocket) {
this.clientSocket = clientSocket;
sockets.add(clientSocket);
}
public void run() {
while (true) {
try {
for(Socket s: sockets) {
//write something
//the for loop will send it to every socket in the array
}
} catch (Exception e) {
//catch it
}
}
}
}
Now I want to be more specific in what client I want to send the message to, just like how a real-world chat application will have different chat rooms.
So if Client1 connects to the server, he will want to start a chat group named "Apple". And then when Client2 and Client3 connects, they can choose to join the group "Apple". Simultaneously, Client 4 will connect to the server and create another chat group called "Banana" where other clients can join in and talk there.
My understanding is that I need to somehow identify each client that the server accepts (I have no idea how to implement this). Then do I somehow put them all into their own array based on their group chat name?
I've been searching for the past week sample codes that allow more than 1 group chat simultaneously but everything I've seen just caters towards 1 only.
The simplest solution is to use and hashmap with key = name of the room and value = a list of users in the chat. In practice:
HashMap<String, ArrayList<Socket>> rooms = new HashMap<>();
When a new user connects, it shoud send a message with the name of the room he want joint and after that you can check if the room already exist or not:
ArrayList<Socket> clientsList = rooms.get(roomName);
if(clientsList == null) {
clientsList = new ArrayList<Socket>();
rooms.put(roomName, clientsList);
}
clientsList.add(socket);
Another good idea could be to create a Client class that contains the Socket along with other information (e.g the username).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With