聊天业务类 主要业务 源代码如下:
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;
}
}