public void sendMessage(String receivers, String msg, String sender) { String messageString = "MESSAGE#" + sender + "#" + msg; String[] receiversarray = receivers.split(","); if (receiversarray.length == 1) { String receiver = receiversarray[0]; if (receiver.equals("*")) { for (Handler handler : handlers.values()) { handler.sendMessage(messageString); Logger.getLogger(EchoServer.class.getName()) .log( Level.INFO, handler.getUsername() + " Sent a message to all: " + messageString, new Object[] {handler.getUsername()}); } } else { Handler handler = (Handler) handlers.get(receiver); handler.sendMessage(messageString); Logger.getLogger(EchoServer.class.getName()) .log( Level.INFO, handler.getUsername() + " Sent a message: " + messageString + " to: " + receiver, new Object[] {handler.getUsername()}); } } }
public void closeClient(String user) { Handler client = (Handler) handlers.get(user); client.sendStop(); try { client.close(); } catch (IOException ex) { Logger.getLogger(EchoServer.class.getName()).log(Level.SEVERE, null, ex); } handlers.remove(user); sendOnlineUsersMsg(); Logger.getLogger(EchoServer.class.getName()) .log(Level.INFO, "Closed a client", new Object[] {client.getUsername()}); }