- 浏览: 60154 次
- 性别:
- 来自: 上海
文章分类
最新评论
昨天本人是在是闲着无聊写了一个聊天系统,感觉不是很差,希望大家多多交流
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()); } } } }
多多交流.
发表评论
-
实现JAVA中String.trim()方法
2010-10-12 21:54 1521实现一个简单的String.trim(String s)方法 ... -
Concurrency 小结
2010-06-27 22:01 191.并发编程让我们可以将程序划分为多个分离的,独立运行的任务。 ... -
JAVA中注解随笔
2009-02-19 15:45 1592package _20.annotation; impo ... -
java性能优化
2008-06-30 20:07 969最近看了一些关于java性能优化方面的知识,作为个人总结,写下 ... -
stream classdesc serialVersionUID = 76026088018680
2008-04-09 15:49 1807java.io.InvalidClassException: ...
相关推荐
自己做的!呵呵使用的是jbulider,包括公聊与私聊
基于server/socket编写的,能实现多人群聊和单人私聊。
网络编程作业 实现了聊天室的公聊和私聊功能 有用户在线列表 有客户端 服务器端
包括有公聊,私聊,连接服务器等等功能!没有连接数据库,往后可以添加连接数据库的功能!
服务器和客户端程序能运行实现公聊、私聊。 线程处理。
程序实现了聊天室的基本功能,通过IP链接服务器,能够与在线的人进行私聊
自己写的,在linux下开发的聊天系统,公聊,私聊,多线程
还有私聊,群聊,离开聊天室都有提示, 另外有显示在线总人数和账号的公告栏! 下载解压导入就能运行,无需加载其他jar包, 绝对是个值得学习的好项目, 开源从分享开始,希望大家多分享自己的代码。。。。
java 聊天程序,拥有私聊,公聊,截图,传送文件等功能,同时拥有《总结与反思文档》,《聊天室需求说明书 》,《上机备忘录》,《设计说明书》和《项目计划书规范》。
在上篇文章给大家介绍使用Angular和Nodejs、socket.io搭建聊天室及多人聊天室,本文继续介绍Node.js中使用socket创建私聊和公聊聊天室,具体详情请看下文吧。 nodejs的应用中,关于socket应该是比较出彩的了,socket...
主要介绍了使用socket实现网络聊天室和私聊功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
(每次只能发送一个表情符) 10、 用户重登聊天室的功能。 11、 用户改变聊天室皮肤的功能。 12、 聊天室显示在线人数的功能,并列出用户列表。 13、 用户查看过去公聊和私聊记录的功能。 服务端功能: 1、 账号管理...
java实现swing的网络通信私聊,公聊,上传文件,仿qq
jsp实验--设计聊天室,在聊天室中,需要通过JSP内置对象application来实现公聊,用session实现私聊。 聊天室的设计包括:用户进行登录,选择聊天室,进行聊天,退出聊天室。 在聊天室中,用户只需输入一个用户名就...
linux聊天室 实现了 具名注册,具名/匿名登录,公聊,私聊 另课程设计报告也一并完成。 客户/服务器模式
一个聊天室程序,分为服务器和客户端,用MFC实现,可以同时打开多个客户端通信
asp简单聊天室 功能齐全,有公聊和私聊功能 选头像登陆
该聊天室可以实现公聊和私聊的功能,基于tcp的实现,用到多线程