不少的sns網站都提供了短消息功能。並且,若是咱們在線的話會很快的收到好友的短消息。
這裏介紹一種客戶端的方法,簡單實現。
主要的表:
user
Message
主要的思路很簡單:用js每隔五秒鐘發送一次ajax請求,獲取當前用戶在Message表中State爲未讀取(這裏約定爲數字1)且ReceverId爲當前用戶ID的Message 記錄的數量。
頁面的代碼:
<%@ 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">
</head>
<body>
</body>
</html>
js代碼:這裏用到了Jquery框架,再也不贅述,網上有不少的資料。
GetMessageCount.js
//------GetMessageCount.js Begin----------------------
if(!GetMessageCount){
}
$(document).ready(
);
GetMessageCount.FindMessage = function(){
}
//------GetMessageCount.js End----------------------
到了這裏,貼出處理ajax請求頁面的代碼,很是簡單
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
{