示例#1
0
文件: Server.java 项目: naory159/Ex4
  /*
   *  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;
        }
      }
    }
  }
示例#2
0
文件: Server.java 项目: naory159/Ex4
    // what will run forever
    public void run() {
      // to loop until LOGOUT
      boolean keepGoing = true;
      while (keepGoing) {
        // read a String (which is an object)
        try {
          cm = (ChatMessage) sInput.readObject();
        } catch (IOException e) {
          display(username + " Exception reading Streams: " + e);
          break;
        } catch (ClassNotFoundException e2) {
          break;
        }
        // the messaage part of the ChatMessage
        String message = cm.getMessage();

        // Switch on the type of message receive
        switch (cm.getType()) {
          case ChatMessage.MESSAGE:
            broadcast(username + ": " + message);
            break;
          case ChatMessage.LOGOUT:
            display(username + " disconnected with a LOGOUT message.");
            keepGoing = false;
            break;
          case ChatMessage.WHOISIN:
            writeMsg("List of the users connected at " + sdf.format(new Date()) + "\n");
            // scan al the users connected
            for (int i = 0; i < al.size(); ++i) {
              ClientThread ct = al.get(i);
              writeMsg((i + 1) + ") " + ct.username + " since " + ct.date);
            }
            break;
          case ChatMessage.TO:
            broadcastOnlyOne(username + ": " + message, cm);
            break;
          case ChatMessage.SENDFILE:
            sendFile(username + ": " + message, cm);
            break;
          case ChatMessage.PROCEED:
            proceedSendFile();
            break;
          case ChatMessage.SENDMEDIAFILE:
            String fileList = getFileList();
            writeMsg(new ChatMessage(ChatMessage.SENDMEDIAFILE, fileList));
            break;
          case ChatMessage.ESTABLISHCONNECTIONANDPLAY:
            sendMediaFile(username + ": " + message, cm);
            break;
        }
      }
      // remove myself from the arrayList containing the list of the
      // connected Clients
      remove(id);
      close();
    }
示例#3
0
文件: Server.java 项目: naory159/Ex4
 /*
  * Write a String to the Client output stream
  */
 private boolean writeMsg(ChatMessage cm) {
   // if Client is still connected send the message to it
   if (!socket.isConnected()) {
     close();
     return false;
   }
   // write the message to the stream
   try {
     boolean sendChatMessage = cm.getType() == ChatMessage.ESTABLISHCONNECTION;
     boolean sendMediaFileMessage = cm.getType() == ChatMessage.ESTABLISHCONNECTIONANDPLAY;
     if (sendChatMessage || sendMediaFileMessage) sOutput.writeObject(cm);
     else sOutput.writeObject(cm.getMessage());
   }
   // if an error occurs, do not abort just inform the user
   catch (IOException e) {
     display("Error sending message to " + username);
     display(e.toString());
   }
   return true;
 }
 @OnMessage
 public void handleChatMessage(ChatMessage message) {
   switch (message.getType()) {
     case NewUserMessage.USERNAME_MESSAGE:
       this.processNewUser((NewUserMessage) message);
       break;
     case ChatMessage.CHAT_DATA_MESSAGE:
       this.processChatUpdate((ChatUpdateMessage) message);
       break;
     case ChatMessage.SIGNOFF_REQUEST:
       this.processSignoffRequest((UserSignoffMessage) message);
   }
 }
    // what will run forever
    public void run() {
      // to loop until LOGOUT
      boolean keepGoing = true;
      while (keepGoing) {
        // read a String (which is an object)
        try {
          cm = (ChatMessage) sInput.readObject();
        } catch (IOException e) {
          display(username + " Exception reading Streams: " + e);
          break;
        } catch (ClassNotFoundException e2) {
          break;
        }
        // the messaage part of the ChatMessage
        String message = cm.getMessage();

        // Switch on the type of message receive
        switch (cm.getType()) {
          case ChatMessage.MESSAGE:
            broadcast(username + ": " + message);
            break;
          case ChatMessage.LOGOUT:
            display(username + " LOGOUT dari chat.");
            keepGoing = false;
            break;
          case ChatMessage.WHOISIN:
            writeMsg("Daftar Client yang terkoneksi pada " + sdf.format(new Date()) + "\n");
            // scan al the users connected
            for (int i = 0; i < al.size(); ++i) {
              ClientThread ct = al.get(i);
              writeMsg((i + 1) + ") " + ct.username + " sejak " + ct.date);
            }
            break;
        }
      }
      // remove myself from the arrayList containing the list of the
      // connected Clients
      remove(id);
      close();
    }