Action.java(获得消息的Action) 代码: /** * 进行聊天 */ public String chat(int type, String text, String userId, String otherUserId, String matchId) { switch(type) { case 1: ChatRoomUtil.putMessages(ChatRoomUtil.Type.ALL, text, userId); return ChatRoomUtil.out(ChatRoomUtil.getMessage(ChatRoomUtil.Type.ALL, userId)); case 2: ChatRoomUtil.putMessages(ChatRoomUtil.Type.BLOCK, text, matchId, userId); return ChatRoomUtil.out(ChatRoomUtil.getMessage(ChatRoomUtil.Type.BLOCK, matchId, userId)); case 3: ChatRoomUtil.putMessages(ChatRoomUtil.Type.INDIVIDUAL, text, userId, otherUserId); return ChatRoomUtil.out(ChatRoomUtil.getMessage(ChatRoomUtil.Type.INDIVIDUAL, userId, otherUserId)); } return ""; } public String getChat(int type, String text, String userId, String otherUserId, String matchId) { switch(type) { case 1: return ChatRoomUtil.out(ChatRoomUtil.getMessage(ChatRoomUtil.Type.ALL, userId)); case 2: return ChatRoomUtil.out(ChatRoomUtil.getMessage(ChatRoomUtil.Type.BLOCK, matchId, userId)); case 3: return ChatRoomUtil.out(ChatRoomUtil.getMessage(ChatRoomUtil.Type.INDIVIDUAL, userId, otherUserId)); } return ""; } ChatRoom.java(聊天室的代码) 代码: import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Timer; import java.util.TimerTask; public class ChatRoomUtil { public static enum Type { ALL, BLOCK, INDIVIDUAL } private static List<Message> room = new ArrayList<Message>(); private static Timer timer = new Timer(); static { ChatRoomUtil cu = new ChatRoomUtil(); timer.schedule(cu.new Task(), 10000, 10000); } static class All extends Message { private Type type; public Type getType() { return type; } public void setType(Type type) { this.type = type; } } static class Block extends Message { private String blockId; private Type type; public Type getType() { return type; } public void setType(Type type) { this.type = type; } public String getBlockId() { return blockId; } public void setBlockId(String blockId) { this.blockId = blockId; } } static class Individual extends Message { private String hostId; private String guestId; private Type type; public Type getType() { return type; } public void setType(Type type) { this.type = type; } public String getHostId() { return hostId; } public void setHostId(String hostId) { this.hostId = hostId; } public String getGuestId() { return guestId; } public void setGuestId(String guestId) { this.guestId = guestId; } } static class Message { private long id; private String supplyId; private List<String> users = new ArrayList<String>(); private Date appearTime; private String Text; public List<String> getUsers() { return users; } public void setUsers(List<String> users) { this.users = users; } public Date getAppearTime() { return appearTime; } public void setAppearTime(Date appearTime) { this.appearTime = appearTime; } public String getText() { return Text; } public void setText(String text) { Text = text; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getSupplyId() { return supplyId; } public void setSupplyId(String supplyId) { this.supplyId = supplyId; } } class Task extends TimerTask { @Override public void run() { synchronized (room) { for (int i = 0; i < room.size(); i++) { Calendar cal = Calendar.getInstance(); Calendar ca = Calendar.getInstance(); ca.setTime(room.get(i).getAppearTime()); if(cal.get(Calendar.SECOND) - ca.get(Calendar.SECOND) > 5) { room.remove(room.get(i)); } } } } } public static void putMessages(Type type, String text, String id) { synchronized (room) { All all = new All(); all.setId(System.currentTimeMillis()); all.setType(type); all.setText(text); all.setAppearTime(new Date()); all.setSupplyId(id); room.add(all); } } public static void putMessages(Type type, String text, String id, String userId) { synchronized (room) { switch(type) { case INDIVIDUAL: Individual indiv = new Individual(); indiv.setId(System.currentTimeMillis()); indiv.setType(type); indiv.setText(text); indiv.setAppearTime(new Date()); indiv.setGuestId(userId); indiv.setHostId(id); room.add(indiv); break; case BLOCK: Block block = new Block(); block.setId(System.currentTimeMillis()); block.setSupplyId(userId); block.setBlockId(id); block.setType(type); block.setText(text); block.setAppearTime(new Date()); room.add(block); break; } } } public static List<Message> getMessage(Type type, String id) { synchronized (room) { List<Message> messages = new ArrayList<Message>(); for (Message message : room) { if(message instanceof All) { boolean isOld = false; for (String str : message.getUsers()) { if(str.equals(id)) { isOld = true; } } if(!isOld) { messages.add(message); message.getUsers().add(id); } } } return messages; } } public static List<Message> getMessage(Type type, String id, String userId) { synchronized (room) { List<Message> messages = new ArrayList<Message>(); List<Message> allMessages = getMessage(Type.ALL, id); if(allMessages != null) { messages.addAll(allMessages); } switch(type) { case INDIVIDUAL: for (Message message : room) { if(message instanceof Individual) { boolean isOld = false; Individual indiv = (Individual)message; for (String str : message.getUsers()) { if(str.equals(id)) { isOld = true; } } if(!isOld) { if((indiv.getHostId().equals(id) && indiv.getGuestId().equals(userId)) || ((indiv.getHostId().equals(userId) && indiv.getGuestId().equals(id)))) { messages.add(message); message.getUsers().add(id); } } } } break; case BLOCK: for (Message message : room) { if(message instanceof Block) { boolean isOld = false; if(((Block)message).getBlockId().equals(id)) { for (String str : message.getUsers()) { if(str.equals(userId)) { isOld = true; } } if(!isOld) { messages.add(message); message.getUsers().add(userId); } } } } break; } return messages; } } public static void main(String[] args) { ChatRoomUtil.putMessages(ChatRoomUtil.Type.ALL, "系统公告", "1"); ChatRoomUtil.putMessages(ChatRoomUtil.Type.ALL, "系统公告1", "1"); ChatRoomUtil.putMessages(ChatRoomUtil.Type.ALL, "系统公告2", "1"); ChatRoomUtil.putMessages(Type.INDIVIDUAL, "我小孩", "1", "2"); ChatRoomUtil.putMessages(Type.INDIVIDUAL, "你小孩", "2", "1"); ChatRoomUtil.putMessages(Type.INDIVIDUAL, "你是三1", "1", "3"); ChatRoomUtil.putMessages(Type.INDIVIDUAL, "你是三2", "1", "3"); ChatRoomUtil.putMessages(Type.BLOCK, "你是11-1", "11", "3"); ChatRoomUtil.putMessages(Type.BLOCK, "你是11-2", "11", "3"); ChatRoomUtil.putMessages(Type.BLOCK, "你是四", "11", "4"); while(true) { try { ChatRoomUtil.out1(ChatRoomUtil.getMessage(Type.ALL, "1")); ChatRoomUtil.out1(ChatRoomUtil.getMessage(Type.INDIVIDUAL, "1", "2")); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int i = new Integer(br.readLine()); switch(i) { case 1: ChatRoomUtil.out1(ChatRoomUtil.getMessage(Type.INDIVIDUAL, "1", "3")); System.out.println(room.size() + " roomSize"); break; case 2: ChatRoomUtil.out1(ChatRoomUtil.getMessage(Type.INDIVIDUAL, "2", "1")); System.out.println(room.size() + " roomSize"); break; case 3: ChatRoomUtil.out1(ChatRoomUtil.getMessage(Type.BLOCK, "11", "2")); System.out.println(room.size() + " roomSize"); break; } Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } } } public static String out(List<Message> mess) { if(mess != null) { StringBuffer sb = new StringBuffer(); for (Message message : mess) { sb.append("<div>"); sb.append(message.getText()); sb.append("<br/></div>"); } return sb.toString(); } return ""; } public static void out1(List<Message> mess) { if(mess != null) { for (Message message : mess) { System.out.println(message.getText()); } } } }