Пример #1
0
  /*
   *  to broadcast a private message to all Clients
   */
  private synchronized void broadcastOnlyOne(String message, ChatMessage cm) {

    // add HH:mm:ss and \n to the message
    String time = sdf.format(new Date());
    String messageLf = time + " " + message + "\r\n";
    System.out.print(messageLf);
    serverGUI.serverTextArea.append(messageLf);

    // we loop in reverse order in case we would have to remove a Client
    // because it has disconnected
    for (int i = al.size(); --i >= 0; ) {
      ClientThread ct = al.get(i);
      // try to write to the Client if it fails remove it from the list
      if (cm.getType() == ChatMessage.ESTABLISHCONNECTION
          || cm.getType() == ChatMessage.ESTABLISHCONNECTIONANDPLAY) {
        if (cm.getTO().equalsIgnoreCase(ct.username) && !ct.writeMsg(cm)) {
          al.remove(i);
          display("Disconnected Client " + ct.username + " removed from list.");
          break;
        }
      } else {
        if (cm.getTO().equalsIgnoreCase(ct.username) && !ct.writeMsg(message)) {
          al.remove(i);
          display("Disconnected Client " + ct.username + " removed from list.");
          break;
        }
      }
    }
  }