这节是进入聊天界面显示,第一次进入页面时不显示消息,当切换用户时就显示离线消息
1.切换时需要保存前者信息在数据库,之后取出后者离线信息显示在界面
2.当退出界面时,保存当前的离线信息,然后退回到登录页面。
1.显示好友列表,需要遍历数据库
public String GetFriendList(HttpSession session){
String result="";
String me=(String) session.getAttribute("LOGINUSER");
//String me="xiaolu";
String command="select * from chat_"+me+";";
ResultSet rs = new SqlHelper.MyHelper().query(command);
try {
result +="<ul id=\"Friends\" class=\"nav nav-list bs-docs-sidenav affix\">";
while(rs.next()){
result +="<li id="+rs.getString(1)+" class=\"\"><a href=\"#\">"+rs.getString(1)+"</a></li>";
}
result +="</ul>";
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(result==""){
result="好友列表为空,快去添加好友吧";
}
return result;
}
这样就动态的显示出好友列表
切换时显示离线聊天记录本
退出保存聊天记录
$("#Button2").click(function() { //按钮点击事件
$.ajax({
type: "POST",
url: "/webChat/goChat",
data: "action=Logout&d=" + new Date(),
success: function(data) {
if (data) {
window.location.href="Login.html";
}
else {
alert("退出失败!");
return false;
}
}
});
})
else if("Logout".equals(strAction)){
boolean res3=Logout(session,friendName);
out.println(res3);
out.close();
}
//登出
public boolean Logout(HttpSession session,String you){
//get oldMessage
String oldMessage="";
String me=(String) session.getAttribute("LOGINUSER");
//String me="xiaolu";
//String you="jack";
String table1="chat_"+me;
String table2="chat_"+you;
//table1 和table2聊天内容相同
//
if(null==session.getAttribute("LOGINUSER")){
return false;
}
String name = session.getAttribute("LOGINUSER").toString();
session.removeAttribute("LOGINUSER");
return false;
}