package com.javaeye.yourgame.action;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.xml.registry.infomodel.User;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.proxy.ScriptProxy;
import org.hibernate.criterion.DetachedCriteria;
import com.javaeye.yourgame.core.BaseAction;
import com.javaeye.yourgame.core.Page;
import com.javaeye.yourgame.po.Message;
import com.javaeye.yourgame.service.IMessageService;
@SuppressWarnings("serial")
public class MessageAction extends BaseAction {
public static Map<String, TreeNode> onlineUsers = new TreeMap<String, TreeNode>();
private Map<String, Object> infos = new HashMap<String, Object>();
public static Map<Long, Message> messages = new TreeMap<Long, Message>();
private Message msg;
private Page page;
private IMessageService messageService;
private boolean success;
private User user;
/**
* 发送消息,在这个方法里面当聊天记录等于5条的时候自动保存到数据库,然后清空记录
*
*/
@SuppressWarnings("unchecked")
public String addMessage(String text, String sender) throws Exception {
if (text != null) {
msg = new Message();
msg.setDate(new Date());
msg.setSender(sender);
msg.setText(text);
messages.put(msg.getId(), msg);
if (messages.size() >= 5) {
List list = new ArrayList();
list.addAll(messages.values());
messageService.saveMessages(list);
messages.clear();
}
WebContext wctx = WebContextFactory.get();
String currentPage = wctx.getCurrentPage();
Collection<ScriptSession> sessions = wctx.getScriptSessionsByPage(currentPage);
ScriptProxy s = new ScriptProxy(sessions);
s.addFunctionCall("receiveMessages", msg);
}
return NONE;
}
public String findChatHistory() throws ParseException {
DetachedCriteria dc = DetachedCriteria.forClass(Message.class);
page.setResult(dc);
page = messageService.findByCriteria(page);
return SUCCESS;
}
public String addUser() {
TreeNode tn = new TreeNode();
tn.setId(java.util.UUID.randomUUID().toString());
tn.setText(getRequest().getParameter("userName"));
onlineUsers.put(tn.getId(), tn); // 用户下线,则从map中移除
this.setSuccess(true);
return SUCCESS;
}
public String getOnlineUsers() {
this.jsonString = "[";
for (Iterator it = onlineUsers.entrySet().iterator(); it.hasNext();) {
Map.Entry en = (Map.Entry) it.next();
TreeNode tn = (TreeNode) en.getValue();
this.jsonString += "{id:'" + tn.getId() + "'";
this.jsonString += ",leaf:true,iconCls:'icon-user',";
this.jsonString += "text:'" + tn.getText() + "'}";
if (it.hasNext()) {
this.jsonString += ",";
}
}
this.jsonString += "]";
return SUCCESS;
}
public boolean isSuccess() {
return success;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public void setSuccess(boolean success) {
this.success = success;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
public Map<String, Object> getInfos() {
return infos;
}
public void setInfos(Map<String, Object> infos) {
this.infos = infos;
}
public void setMessageService(IMessageService messageService) {
this.messageService = messageService;
}
public static class TreeNode {
public String id;
public String text;
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
public String getText() {
return this.text;
}
public void setText(String text) {
this.text = text;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
老早就说要分享这份代码,知道现在才有事件整理给各位朋友. 这个程序是模仿QQ实现的群聊功能,基本类似聊天室.聊天记录每5条保存到数据库,具体请各位朋友细看. 技术:ExtJS 3.2 DWR 3.0 Struts 2.16 Spring 2.0 Hibernate 3.2 MySql 5.0 原文地址: http://yourgame.javaeye.com/blog/681537 火狐浏览器,谷歌浏览器测试通过. 讨论Q_群:91986229(群3) 76202406(群2) 62150370(群1)一起讨论 (请不要一号多群)
资源推荐
资源详情
资源评论
收起资源包目录
ExtJS 3.2 聊天室程序(类QQ群) (311个子文件)
MessageAction.class 6KB
MessageDao.class 3KB
Page.class 3KB
MessageService.class 2KB
BaseAction.class 2KB
Message.class 2KB
MessageDao$1.class 1KB
HibernateUtils.class 1KB
MessageAction$TreeNode.class 853B
ExportDB.class 791B
IMessageDao.class 576B
IMessageService.class 417B
.classpath 3KB
ext-all.css 132KB
tool-sprites.gif 4KB
btn.gif 4KB
tip-sprite.gif 4KB
error-tip-corners.gif 4KB
blue-loading.gif 3KB
large-loading.gif 3KB
tools-sprites-trans.gif 3KB
group-cs.gif 2KB
search-trigger.gif 2KB
tabs-sprite.gif 2KB
loading-balls.gif 2KB
tb-sprite.gif 2KB
checkbox.gif 2KB
clear-trigger.gif 2KB
trigger.gif 2KB
trigger-square.gif 2KB
radio.gif 2KB
icon-error.gif 2KB
tb-xl-btn-sprite.gif 2KB
icon-question.gif 2KB
date-trigger.gif 2KB
icon-info.gif 2KB
e-handle.gif 2KB
trigger-tpl.gif 1KB
icon-warning.gif 1KB
sort-hd.gif 1KB
gradient-bg.gif 1KB
gradient-bg.gif 1KB
corners-sprite.gif 1KB
tab-btm-left-bg.gif 1KB
tab-btm-inactive-right-bg.gif 1KB
white-corners-sprite.gif 1KB
s-handle.gif 1KB
scroll-right.gif 1KB
scroll-left.gif 1KB
grid3-hd-btn.gif 1KB
tb-btn-sprite.gif 1KB
wait.gif 1KB
scroller-bg.gif 1KB
hd-sprite.gif 1KB
bg.gif 1KB
row-check-sprite.gif 1KB
e-handle-dark.gif 1KB
s-handle-dark.gif 1KB
pick-button.gif 1KB
drop-yes.gif 1016B
drop-yes.gif 1016B
corners-blue.gif 1010B
corners.gif 1005B
drop-add.gif 1001B
drop-add.gif 1001B
exclamation.gif 996B
user.gif 987B
calendar.gif 979B
refresh.gif 977B
close.gif 972B
tool-sprite-tpl.gif 971B
hmenu-unlock.gif 971B
columns.gif 962B
warning.gif 960B
checked.gif 959B
folder-open.gif 956B
group-expand-sprite.gif 955B
hmenu-lock.gif 955B
row-expand-sprite.gif 955B
folder.gif 952B
tip-anchor-sprite.gif 951B
drop-no.gif 949B
drop-no.gif 949B
drop-no.gif 947B
leaf.gif 945B
unchecked.gif 941B
s-arrow-o.gif 937B
s-arrow.gif 937B
s-arrow-b.gif 937B
hmenu-asc.gif 931B
hmenu-desc.gif 930B
page-first-disabled.gif 925B
page-first.gif 925B
page-last-disabled.gif 923B
page-last.gif 923B
btn-arrow.gif 919B
group-by.gif 917B
btn-arrow-light.gif 916B
drop-over.gif 911B
drop-under.gif 911B
共 311 条
- 1
- 2
- 3
- 4
zhoujuan520
- 粉丝: 7
- 资源: 25
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页