Chat Room Ajax reads messages

Source: Internet
Author: User

Read message: After logging on, read the message after logging on. When obtaining the message for the first time, obtain the ID of the last row. When the message is read again, use the SQL statement to judge> the value of the last row ID .......

Obtain the message after Logon:

Create on the page

<Asp: hiddenfield id = "datenow" runat = "server"/>

If (! Ispostback)
{
Datenow. value = datetime. Now. tostring (); assign the logon time value to datenow during the first running;
}

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"/>

The msgs [msgs. count-1]. id. tostring () in JS, get var valueid = Result [result. length-1]. substring (result [result. length-1]. lastindexof (',') + 1)

Then assign the value to document. getelementbyid ("lengthid"). value = valueid;

 

Page to get messages;

Page JS Code:

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 (','); // here, an ID is added at the end of the returned value to intercept ', and the content before' is the message.

VaR valueid = Result [result. length-1]. substring (result [result. length-1]. lastindexof (',') + 1); // The ID is obtained here.
$ ("Lengthid"). value = valueid;
// $ ("Lid"). value = valueid; test the last Id
If (HED <= valueid)
{
VaR newfontelem = Document. createelement ("font ");
Newfontelem. innerhtml = valuess + "<br/>"
$ ("Chat"). appendchild (newfontelem );
$ ("Chat"). scrolltop = $ ("chat"). scrollheight; // pull the scroll bar to the bottom of each received message
}
}
}
}
Function getstrijng ()
{
VaR YY = $ ("lengthid"). value;

VaR xx = $ ("datenow"). value;

JS. getkmsg (XX, YY, getobjectlistcallbacks );

SetTimeout ('getstrijng () ', 1000 );
}

Page code:

<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 code:

Add

[System. Web. Script. Services. scriptservice] // you must add this sentence. Otherwise, JS cannot be found.
[WebService (namespace = "http://tempuri.org/")]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
Public class JS: system. Web. Services. WebService
{

Public JS ()
{

// If you use the designed component, uncomment the following line
// Initializecomponent ();
}
[Webmethod]
Public list <string> getkmsg (string XX, string YY)
{
If (yy! = "") // This is the name of the BLL layer executed when there is no last Id
{
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 (); // The key statement is mentioned above.
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 + "to" + toname + "to say [" + 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
{

// This is the message after the First Login
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); // The key statement has been output above
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 + "to" + toname + "to say [" + 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;
}
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.