短消息即時提醒 asp.net 簡單實現

很多的sns網站都提供了短消息功能。而且,如果我們在線的話會很快的收到好友的短消息。
這裏介紹一種客戶端的方法,簡單實現。

主要的表:
user
    :Uid UName Password 三個字段
Message
    :Mid, SenderId, ReceiverId, State, Detail(SenderId和 ReceiverId)都是外鍵且對應user表中的Uid。


主要的思路很簡單:用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">
    <title>無標題頁</title>
    <!-- 兩個js腳本文件-->
    <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;">
        您有<input type="text" value="0" id="messageCount"/><a href="ShowMessage.aspx">條短消息</a>
    </div>
    </form>
</body>
</html>

js代碼:這裏用到了Jquery框架,不再贅述,網上有很多的資料。
GetMessageCount.js
//------GetMessageCount.js Begin----------------------
if(!GetMessageCount){
    var GetMessageCount = {};
}

$(document).ready(
    function(){
        GetMessageCount.FindMessage();
    }
);

GetMessageCount.FindMessage = function(){
        $.ajax({
           //處理ajax請求
           url:'FindNewMessage.ashx',
           // 當前用戶的ID,這裏圖省事就省略了,直接寫死爲 1,
           //實際使用過程中可以從session中獲取 。。。。
           data:{Uid:1},
           cache: false,
           //回調函數返回未讀短信數目
           success: function(response)
           {
              $('#messageCount').val(response);
           },
           error:function(data)
           {
               alert("加載失敗");
           }
       });
       //每隔5 秒遞歸調用一次,刷新未讀短信數目
       window.setTimeout(GetMessageCount.FindMessage,5000);
}
//------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
{
    /// <summary>
    /// $codebehindclassname$ 的摘要說明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class FindNewMessage : IHttpHandler
    {

        public void Proce***equest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
              //就這一句代碼,獲取未讀短信的數量,返回頁面
              //後臺的sql代碼就省略了
            int count = SqlHelp.SqlHelp.GetUnreadMessageCount(Convert.ToInt32(context.Request["Uid"]));
            //返回頁面
            context.Response.Write(count);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

//----------------'FindNewMessage.ashx End
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章