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) && !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; }

聊天业控制类 源代码如下:

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; }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值