if(message[0].equals("open")){//if message is an open session message, add the key value pair of the socket details (a unique identifier of each session) and a string array containing the username and the current chat
response="Session started";//set response to confirm the session has started
}
elseif(message[0].equals("close")){//if message is an open session message, add the key value pair of the socket details (a unique identifier of each session) and a string array containing the username and the current chat
if(chats.containsKey(sessions.get(sd)[1])){//if the user is in a chat
if(chats.get(sessions.get(sd)[1]).size()==1){//if the chat contains only 1 member, delete it
chats.remove(sessions.get(sd)[1]);
}
else{//else remove the user from the chat and inform all members on the chat
chats.get(sessions.get(sd)[1]).remove(sd);
sendMessage(sessions.get(sd)[0]+" has left the chat",sessions.get(sd)[1]);
}
}
sessions.remove(sd);
response="Session ended";//set response to confirm the session has ended
}
elseif(message[0].equals("join")){//if the message is a join chat message
sessions.get(sd)[1]=message[2];//set the chat in the session
if(chats.containsKey(message[2])){//if the chat already exists, add the socket details to the chat members list and send a message to all chat members that the new user has joined
sendMessage(sessions.get(sd)[0]+" has joined the chat",message[2]);
chats.get(message[2]).add(sd);
response="Joined chat "+message[2];//set response to confirm the user has joined the chat
}
else{//else we need to create a new chat, so add a new key value pair of the chat name and list of users and add the socket details of the current user