コード例 #1
0
  /*
   *  to broadcast a message to all Clients
   */
  private synchronized void broadcast(String message) {
    // add HH:mm:ss and \n to the message
    String time = sdf.format(new Date());
    String messageLf = time + " " + message + "\n";
    // display message on console or GUI
    if (sg == null) System.out.print(messageLf);
    else sg.appendRoom(messageLf); // append in the room window

    // 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 (!ct.writeMsg(messageLf)) {
        al.remove(i);
        display("Disconnected Client " + ct.username + " removed from list.");
      }
    }
  }
コード例 #2
0
 /*
  * Display an event (not a message) to the console or the GUI
  */
 private void display(String msg) {
   String time = sdf.format(new Date()) + " " + msg;
   if (sg == null) System.out.println(time);
   else sg.appendEvent(time + "\n");
 }