聊天业务类 主要业务 源代码如下:
package com.dwr;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.HttpSession;
import org.directwebremoting.Browser;
import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.ScriptSessions;
import org.directwebremoting.WebContextFactory;
import com.vo.User;
public class ChatDwr {
public ChatDwr() {
System.out.println("实例化");
}
// sessionId和User对象
public static Map<String, User> map = new HashMap<String, User>();
// 登陆
public boolean login(String username) {
// session
HttpSession session = WebContextFactory.get().getSession();
// 键同,值不同(注销,put)
// 键不同,值同(则在别处登陆,提示)
// 键不同,值不同(新用户,put)
//activeReverseAjaxEnable
for (Entry<String, User> user : map.entrySet()) {
String sid = user.getKey();
String name = user.getValue().getName();
if (name.equalsIgnoreCase(username) && !sid.equals(session.getId()))
return true;//在别处登陆
else if (username.equals("") && sid.equals(session.getId())) {
map.remove(sid);
break;
}
}
// 保存用户
ScriptSession ss = WebContextFactory.get().getScriptSession();
if (username.length() > 0) {
map.put(session.getId(), new User(ss.getId(),username));
ss.setAttribute("username", username);
}
// 添加数据到用户列表
Browser.withAllSessions(new Runnable() {
public void run() {
ScriptSessions.addFunctionCall("addUser", map.values());
}
});
session.setAttribute("username", username);
return false;
}
// 发送消息
public boolean sendMsg(
final String toId, final String toName, final String msg) {
final HttpSession session = WebContextFactory.get().getSession();
final String from = (String) session.getAttribute("username");
if (from==null || from.equals(toName))
return false;
Browser.withSession(toId, new Runnable() {
public void run() {
ScriptBuffer buff = new ScriptBuffer();
buff.appendCall("recv", from, toName, msg, new Date());
ScriptSession ss = Browser.getTargetSessions().iterator()
.next();
ss.addScript(buff);
}
});
return true;
}
}
--------------------------------------------------------------- 聊天业控制类 源代码如下:
package com.servlets;
import java.io.IOException;
import java.util.Collection;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.directwebremoting.Browser;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.ScriptSessions;
import org.directwebremoting.ServerContextFactory;
import org.directwebremoting.WebContextFactory;
import com.dwr.ChatDwr;
public class ExitGameServlet extends HttpServlet {
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
final HttpSession session = request.getSession(false);
if (session != null) {
Browser.withAllSessions(new Runnable() {
public void run() {
System.out.println("来了");
ChatDwr.map.remove(session.getId());
session.invalidate();
ScriptSessions.addFunctionCall("addUser", ChatDwr.map
.values());
}
});
}
}
}
------------------------------------------------------------ 聊天vo类 源代码如下:
package com.vo;
public class User {
private String id; //scriptSessionId
private String name;
private String sid; //sessionId
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User(String id, String name) {
super();
this.id = id;
this.name = name;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public User(String id, String name, String sid) {
super();
this.id = id;
this.name = name;
this.sid = sid;
}
}
分享到:
相关推荐
DWR操作数据库模拟实现Google搜索效果
所有技术:;j2ee dwr servlet js 实现在线聊天功能,无刷新,逆推信息。
spring3 dwr3 聊天 dwr server push spring3+dwr3实现聊天功能 使用DWR SERVER PUSH 实现聊天功能
java+dwr框架实现聊天室,
纯JSP+DWR实现三级联动下拉选择菜单 实现无刷新联动 DWR判断用户是否存在 ajax二级联动菜单 DWR操作数据库模拟实现Google搜索效果
dwr实现的聊天室,采用服务器控制客户端刷新,显示用户连接信息,是深入学习dwr的必看教程! 源码分为一个java文件和一个html文件,还有一个dwr配置文件,根据自己的路径进行修改。
DWR 在线即时聊天系统,实现了对指定用户发送消息,和即时显示功能.rarDWR 在线即时聊天系统,实现了对指定用户发送消息,和即时显示功能.rarDWR 在线即时聊天系统,实现了对指定用户发送消息,和即时显示功能.rarDWR 在线...
DWR框架推模式实现的聊天室DWR框架推模式实现的聊天室DWR框架推模式实现的聊天室
DWR在线即时聊天系统,实现了对指定用户发送消息,和即时显示功能 java jsp页面
DWR案例DWR学习案例详解(数据库)DWR学习案例详解(数据库)DWR学习案例详解(数据库)
用数据库缓存池和dwr实现的jsp程序数据库缓存池和dwr实现的jsp程序
使用myeclipse和tomcat工具,DWR实现的简单用户登录小实例,使用mysql数据库,数据库名login,只有一张表,表名user,数据项username和userpass,压缩包中有生成表的sql语句。此系统实现用户登录,和所有用户查询...
我不能把公司的代码贴出来,所以我自己写了一个DEMO,已经实现了对指定用户发送消息的功能,并且即时显示在指定的用户界面上面.当然你可以修改这些,让它弹出来显示,或者像QQ一样在你页面的某个角落让消息的图标闪动:)
dwr中文文档dwr中文文档dwr中文文档dwr中文文档dwr中文文档dwr中文文档dwr中文文档
在springMVC配置项目下,实现简单的DWR反转即时聊天
使用dwr框架的服务器推技术也称为反向ajax或comet,将项目发布到ajax中输入http://localhost:8080/dwr即可聊天 我实现的时群聊
DWR框架的实现DWR框架的实现DWR框架的实现DWR框架的实现DWR框架的实现
用dwr3实现推送功能,文件就是该实现代码
dwr实现ajax功能ajax+dwr.........................
DWR中文文档DWR中文文档DWR中文文档DWR中文文档DWR中文文档