日期:2014-05-16  浏览次数:20437 次

DWR 实现单向聊天【三:业务层、控制层、bean】 功能登录显示登录用户【无数据库操作】
一:聊天业务类 主要业务 源代码如下:
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) &amp;&amp; !sid.equals(session.getId())) 
return true;
//在别处登陆 
else if (username.equals("") &amp;&amp; sid.equals(session.getId())) { map.remove(sid); 
break;
} 
} 
// 保存用户 
ScriptSession ss = WebContextFactory.get().getScriptSession(); 
if (username.length() &gt; 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; } 

聊天业控制类 源代码如下:
 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() { 
// Collection<scriptsession> cs = 
// ServerContextFactory.get(getServletContext()).getAllScriptSessions(); 
// for (ScriptSession cc : cs) { 
// String username = (String)cc.getAttribute("username"); 
// if (username==null) // cc.invalidate(); 
// } 
// System.out.println(cs); 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(S
tring 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; } 
}