// 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(); }
/* * 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; }
// 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(); }