这节实现的是信息发送
1.编写文字,点击按钮发送,将发送文字拼接成html并且和前面的html结合显示在前端
2.不是发送一下就保存一下聊天记录,而是切换好友或者是退出聊天时才保存聊天信息
$("#Button1").click(function() { //按钮点击事件
var $content = $("#txtContent"); //发送内容
if ($content.val() != "") {
SendContent($content.val());
}
else {
alert("发送不能为空!");
$content.focus();
return false;
}
})
function SendContent(content) {
$.ajax({
type: "POST",
url: "/webChat/goChat",
data: "action=SendContent&d=" + new Date() + "&content=" + content,
success: function(data) {
//alert(data);
if (data==1) {
GetMessageList(<span style="color:#FF0000;">active_friend</span>);//是一个全局变量
$("#txtContent").val("");
}else {
GetMessageList();
alert("请先登录!");
window.location.href="/webChat/WebContent/Login.html";
//window.location.href="/webChat/WebContent/ChatMain.html";
}
}
});
}
function GetMessageList(friendName) {
$.ajax({
type: "POST",
url: "/webChat/goChat",
data: "action=ChatList&d=" + new Date()+ "&friendName=" +friendName,
success: function(data) {
$("#divContent").html(data);
}
});
AutoUpdContent(); //执行定时获取函数
}
else if("SendContent".equals(strAction)){
Boolean res2=AddSendContent(strContent,session);
String result="";
if(res2){
result="1";
}else{
result="2";
}
out.println(result);
out.close();
//发送信息
public Boolean AddSendContent(String strContent,HttpSession session){
String user=(String) session.getAttribute("LOGINUSER");
if(null==user){
return false;
}
String strSendConent = user + " 于 " + new java.util.Date(System.currentTimeMillis()) + " 说: " + strContent;
if (strSendConentList.size() == 0)
{
strSendConentList = new ArrayList<String>();
}
strSendConentList.add(strSendConent);
return true;
}
//获取聊天内容
public String AllChatList(HttpSession session,String friendName){
//add old Message
String oldMessage=getOldMessage(session,friendName);
String result="";
if (oldMessage.equals("null") && strSendConentList.size() == 0)
{
oldMessage="";
}
else
{
Iterator<String> it=strSendConentList.iterator();
while(it.hasNext()){
result += it.next() + "</br>";
}
}
result= result.replace("<:", "<img src='Face/");
result=result.replace(":>", ".gif '/>");
result=oldMessage+result;
return result;
}