Many SNS websites provide short message functions. In addition, if we are online, we will soon receive short messages from friends.
This section describes a client Method for simple implementation.
Main Tables:
User
: Uid uname password three fields
Message
: Mid, senderid, receiverid, state, detail (senderid and receiverid) are both foreign keys and correspond to the uid in the User table.
The main idea is simple: use JavaScript to send an Ajax request every five seconds to get the current user's state in the message table as unread (Number 1 is agreed here) and the receverid is the number of message records of the current user ID.
PageCode:
<% @ Page Language = "C #" codebehind = "default. aspx. cs" inherits = "midemo. _ default" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
<! -- Two JS script files -->
<SCRIPT type = "text/JavaScript" src = "sqlhelp/jquery-1.3.2.js"> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "sqlhelp/getmessagecount. js"> </SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div style = "border-color: green; border-style: solid; margin-top: 100px; margin-left: 300px; width: 300px; Height: 50px; text-align: center; ">
You have <input type = "text" value = "0" id = "messagecount"/> <a href = "showmessage. aspx"> Short Messages </a>
</Div>
</Form>
</Body>
</Html>
JS Code: The jquery framework is used here. I will not repeat it here. There is a lot of information on the Internet.
Getmessagecount. js
// ------ Getmessagecount. js begin ----------------------
If (! Getmessagecount ){
VaR getmessagecount = {};
}
$ (Document). Ready (
Function (){
Getmessagecount. findmessage ();
}
);
Getmessagecount. findmessage = function (){
$. Ajax ({
// Process Ajax requests
URL: 'findnewmessage. ashx ',
// The ID of the current user, which is omitted in the figure below. The number is 1,
// It can be obtained from the session in actual use ....
Data: {uid: 1 },
Cache: false,
// The number of unread messages returned by the callback function
Success: function (response)
{
$ ('# Messagecount'). Val (response );
},
Error: function (data)
{
Alert ("loading failed ");
}
});
// Call it recursively every 5 seconds to refresh the number of unread messages
Window. setTimeout (getmessagecount. findmessage, 5000 );
}
// ------ Getmessagecount. js end ----------------------
Here, the code for processing the Ajax request page is very simple.
Findnewmessage. ashx
// ---------------- 'Findnewmessage. ashx begin
Using system;
Using system. collections;
Using system. Data;
Using system. LINQ;
Using system. Web;
Using system. Web. Services;
Using system. Web. Services. Protocols;
Using system. xml. LINQ;
namespace midemo
{< br> //
/ // $ codebehindclassname $ abstract description
///
[webService (namespace = "http://tempuri.org/")]
[webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
public class findnewmessage: ihttphandler
{< br>