读取消息:登录后读取登录后的消息,在首次获取消息的时候,得到最后一行的ID再次读消息的时候SQL语句去判断>最后行ID的值…….
登录后获取消息:
在页面创建
<asp:HiddenField ID="DATEnow" runat="server" />
if(!IsPostBack)
{
DATEnow.Value= DateTime.Now.ToString(); 首次运行创建登录时间赋值给DATEnow;
}
select f.Name as 'Formusername',t.Name as 'ToUsername',m.id,m.Fromuserid,m.TouserId,m.Message,m.recordTime,m.messageState,m.readflag from Message as m inner join Users as f on f.Id=m.FromUserId inner join Users as t on t.Id=m.ToUserId where m.recordtime>@getNowdate
<asp:HiddenField ID="lengthid" runat="server" />
WEBserver 中返回最后行的MSgs[MSgs.Count - 1].Id.ToString() 在js中得到var valueID=result[result.length-1].substring(result[result.length-1].lastIndexOf(',')+1)
再赋值给 document.getElementById("lengthid").value=valueID;
页面获取消息;
页面JS代码:
function $(j){return document.getElementById(j);}
function GetObjectListCallBacks(result)
{
if(result.length!=0){
for(var i=0;i<result.length;i++)
{
var valuesss= result[i];
var Hed=$("lengthid").value;
var valuess= result[i].substring(0, result[i].lastIndexOf(',')); //这里是我返回值中最后面加了个ID用截取 ‘,’前的内容 就消息
var valueID=result[result.length-1].substring(result[result.length-1].lastIndexOf(',')+1);///这里得到的是ID
$("lengthid").value=valueID;
//$("Lid").value=valueID;测试最后个ID
if(Hed<=valueID)
{
var newFontElem = document.createElement("font");
newFontElem.innerHTML =valuess+"<br/>"
$("Chat").appendChild(newFontElem);
$("Chat").scrollTop= $("Chat").scrollHeight;//每次收到消息吧滚动条拉到最下面
}
}
}
}
function getSTrijng()
{
var YY=$("lengthid").value;
var XX =$("DATEnow").value;
JS.getKMSg(XX,YY,GetObjectListCallBacks);
setTimeout('getSTrijng()', 1000);
}
页面代码:
<form id="form1" runat="server">
<div>
<asp:HiddenField ID="DATEnow" runat="server" />
<asp:HiddenField ID="lengthid" runat="server" />
<input type="text" id="Lid" />
<div id="Chat">
</div>
</div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="JS.asmx" />
</Services>
</asp:ScriptManager>
</form>
WEBserver代码:
在wenserver 中加
[System.Web.Script.Services.ScriptService] //必须加上这句 不然JS会无法找到的
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class JS : System.Web.Services.WebService
{
public JS()
{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public List<String> getKMSg(String XX, String YY)
{
if (YY != "") //这个是没有最后个ID的时候执行的BLL层的东东
{
List<string> listStr = new List<string>();
StringBuilder geTMsglist = new StringBuilder();
List<Message> MSgs = new List<Message>();
MSgs = new BLL.MsgCtergor().readMSg(5, 9999, DateTime.Parse(XX), int.Parse(YY.ToString()));//关键语句再上面已经说出
if (MSgs.Count > 0)
{
for (int i = 0; i < MSgs.Count; i++)
{
String formname = MSgs[i].Formusername.ToString();
String Toname = MSgs[i].ToUsername.ToString();
String SEnddate = MSgs[i].RecordTime.ToString();
String Sendtext = " " + formname + "对" + Toname + "说[" + SEnddate + "]<br />";
String tEXS = MSgs[i].Messag.ToString();
Sendtext += "<B>" + tEXS + "</B>," + MSgs[MSgs.Count - 1].Id.ToString() + "";
geTMsglist.Append(Sendtext);
listStr.Add(geTMsglist.ToString());
}
}
return listStr;
}
else
{
//这个是首次登录后的消息
List<string> listStr = new List<string>();
StringBuilder geTMsglist = new StringBuilder();
List<Message> MSgs = new List<Message>();
MSgs = new BLL.MsgCtergor().readMSg(0, 9999, DateTime.Parse(XX), 9999);//关键语句再上面已经说出
if (MSgs.Count > 0)
{
for (int i = 0; i < MSgs.Count; i++)
{
String formname = MSgs[i].Formusername.ToString();
String Toname = MSgs[i].ToUsername.ToString();
String SEnddate = MSgs[i].RecordTime.ToString();
String Sendtext = " " + formname + "对" + Toname + "说[" + SEnddate + "]<br />";
String tEXS = MSgs[i].Messag.ToString();
Sendtext += "<B>" + tEXS + "</B>," + MSgs[MSgs.Count - 1].Id.ToString() + "";
geTMsglist.Append(Sendtext);
listStr.Add(geTMsglist.ToString());
}
}
return listStr;
}
}
}